Struct implementations = new Struct(); implementations.setType(Struct.Type.JavaClass); implementations.addToCommentBefore("Wrapper for library <b>" + library + "</b>", result.declarationsConverter.getFileCommentContent(result.config.libraryProjectSources.get(library), null)); implementations.addModifiers(ModifierType.Public); implementations.setTag(implementationsSimpleClassName); implementations.addParent(ident(config.runtime.libraryClass, expr(typeRef(implementationsSimpleClassName)))); if (declarationsFullClassName != null) { implementations.addProtocol(declarationsFullClassName.clone()); implementations.addDeclaration(newStaticInit()); implementations.setResolvedJavaIdentifier(implementationsFullClassName); declarations = new Struct(); declarations.setType(Struct.Type.JavaInterface); declarations.addToCommentBefore("Interface for library <b>" + library + "</b>", result.declarationsConverter.getFileCommentContent(result.config.libraryProjectSources.get(library), null)); declarations.addModifiers(ModifierType.Public); declarations.setTag(declarationsSimpleClassName.clone()); declarations.setResolvedJavaIdentifier(declarationsFullClassName); } else { declarations = implementations;
@Override public void visitStruct(Struct struct) { Identifier name = struct.getTag(); if (name != null) { switch (struct.getType()) { case CPPClass: if (!config.runtime.equals(JNAeratorConfig.Runtime.BridJ) && !config.genCPlusPlus) { case CUnion: if (!currentNamespace.isEmpty()) struct.setParentNamespace(currentNamespace.peek().clone()); boolean isFwd = struct.isForwardDeclaration(); if (!isFwd && struct.getDeclarations().isEmpty() && config.treatEmptyStructsAsForwardDecls) { List<SimpleTypeRef> parents = struct.getParents(); Struct p; if (parents.isEmpty() || parents.size() == 1 && ((p = structsByName.get(parents.get(0))) == null || p.isForwardDeclaration())) { isFwd = true; Identifier originalName = struct.getOriginalTag(); if (originalName != null) { structsByName.put(originalName, struct); struct.setResolvedJavaIdentifier(resolvedJavaIdentifier); if (struct.findParentOfType(Struct.class) == null) { getList(structsByLibrary, getLibrary(struct)).add(struct); if (struct.getType() == Struct.Type.CUnion) { unionsFullNames.add(identifier);
if (!e.getAnnotations().isEmpty()) { implode(e.getAnnotations(), "\n" + indent).append("\n", indent); if (e.getType() != null) { switch (e.getType()) { case CPPClass: modifiersStringPrefix(e); append("class ", e.getTag()); if (!e.getParents().isEmpty()) { append(" : ").implode(e.getParents(), ", "); append("union ", e.getTag() == null ? null : " ", e.getTag()); break; case JavaClass: case JavaInterface: modifiersStringPrefix(e); append(e.getType() == Struct.Type.JavaClass ? "class " : "interface ", e.getTag()); if (!e.getParents().isEmpty()) { append(" extends ").implode(e.getParents(), ", "); if (!e.getProtocols().isEmpty()) { append(" implements ").implode(e.getProtocols(), ", "); case ObjCClass: modifiersStringPrefix(e); append(e.isForwardDeclaration() ? "@class " : "@interface ", e.getTag()); if (e.getCategoryName() != null) { append(" (", e.getCategoryName(), ")");
@Override protected void configureCallbackStruct(Struct callbackStruct) { callbackStruct.setType(Struct.Type.JavaClass); callbackStruct.addModifiers(ModifierType.Public, ModifierType.Static, ModifierType.Abstract); }
public static Struct forwardDecl(SimpleIdentifier name, Type type) { Struct s = new Struct(); s.setForwardDeclaration(true); s.setTag(name); s.setType(type); return s; }
Struct publicStaticClass(Identifier name, Identifier parentName, Struct.Type type, Element toCloneCommentsFrom, Identifier... interfaces) { Struct cl = new Struct(); cl.setType(type); cl.setTag(name); if (parentName != null) { cl.setParents(typeRef(parentName)); } if (type == Struct.Type.JavaInterface) { for (Identifier inter : interfaces) { cl.addParent(typeRef(inter)); } } else { for (Identifier inter : interfaces) { cl.addProtocol(typeRef(inter)); } } if (!result.config.noComments) { cl.importComments(toCloneCommentsFrom, getFileCommentContent(toCloneCommentsFrom)); } cl.addModifiers(ModifierType.Public, ModifierType.Static); return cl; }
public Struct generateObjectiveCClass(Struct in, Signatures signatures) throws IOException { boolean isProtocol = in.getType() == Type.ObjCProtocol, isCategory = in.getCategoryName() != null; Struct instanceStruct = new Struct().addModifiers(ModifierType.Public); instanceStruct.setCommentBefore(in.getCommentBefore()); instanceStruct.addToCommentBefore(in.getCommentAfter()); instanceStruct.setTag(isCategory ? ident(in.getCategoryName()) : in.getTag().clone()); if (isProtocol || isCategory) { instanceStruct.setType(Type.JavaInterface); } else { instanceStruct.addModifiers(ModifierType.Abstract).setType(Type.JavaClass); Struct classStruct = new Struct(); classStruct.setTag(ident(classClassName)); classStruct.setType(Struct.Type.JavaClass); classStruct.addModifiers(ModifierType.Public, ModifierType.Static, ModifierType.Abstract); List<SimpleTypeRef> parentsForInstance = new ArrayList<SimpleTypeRef>(in.getParents()); boolean isNSObject = in.getTag().equals(NSObject.class.getSimpleName()); for (Struct catIn : Result.getMap(result.objCCategoriesByTargetType, in.getTag()).values()) { Identifier catId = getFullClassName(catIn); String categName = catIn.getTag() + "_" + sim; if (add(instanceStruct, createCastMethod(ident(categName), catId, false), signatures)) { classStruct.addDeclaration(createCastMethod(ident(categName), ident(catId, classInterfaceNameInCategoriesAndProtocols), true)); instanceStruct.addProtocol(ident(ObjCObject.class));
if (struct.isForwardDeclaration())// && !result.structsByName.get(structName).isForwardDeclaration()) boolean isUnion = struct.getType() == Struct.Type.CUnion; boolean inheritsFromStruct = false; Identifier baseClass = null; int parentFieldsCount = 0; List<String> preComments = new ArrayList<String>(); for (SimpleTypeRef parentName : struct.getParents()) { Struct parent = result.structsByName.get(parentName.getName()); if (parent == null) { for (Declaration d : struct.getDeclarations()) { if (d instanceof Function) { hasMemberFunctions = true; Constant uuid = (Constant) struct.getModifierValue(ModifierType.UUID); if (baseClass == null) { switch (struct.getType()) { case CStruct: case CUnion: structJavaClass.addDeclaration(newStaticInit()); structJavaClass.addAnnotation(new Annotation(result.config.runtime.typeRef(JNAeratorConfig.Runtime.Ann.IID), uuid)); structJavaClass.setResolvedJavaIdentifier(ident(structName)); if (result.config.forceNames) annotateActualName(structJavaClass, structName); addParentNamespaceAnnotation(structJavaClass, struct.getParentNamespace());
struct.setForwardDeclaration(false); struct.setForwardDeclaration(false); if (parentIdentifier != null) struct.addParent(parentIdentifier); struct = new Struct(); struct.setForwardDeclaration(true); struct.setTag(tag); struct.setType( (typeToken!=null?typeToken.getText():null).equals("struct") ? Struct.Type.CStruct : (typeToken!=null?typeToken.getText():null).equals("union") ? Struct.Type.CUnion : if (struct.getType() == Struct.Type.CPPClass) forcedType = Function.Type.CppMethod; for (Declaration d : struct.getDeclarations()) { if (d instanceof Function) ((Function)d).setType(forcedType); struct.addModifiers(modifiers);
struct = mark(new Struct(), getLine(octype)); struct.setType((octype!=null?octype.getText():null).equals("@interface") ? Struct.Type.ObjCClass : Struct.Type.ObjCProtocol ); struct.setTag(new SimpleIdentifier((className!=null?className.getText():null))); struct.addParent(new SimpleIdentifier((parentClass!=null?parentClass.getText():null))); struct.setCategoryName((categoryName!=null?categoryName.getText():null)); if ( state.backtracking==0 ) { struct.addProtocol(new SimpleIdentifier((p1!=null?p1.getText():null))); } if ( state.backtracking==0 ) { struct.addProtocol(new SimpleIdentifier((px!=null?px.getText():null))); } if ( state.backtracking==0 ) { struct.setNextMemberVisibility(Struct.MemberVisibility.Public); } if ( state.backtracking==0 ) { struct.setNextMemberVisibility(Struct.MemberVisibility.Private); } if ( state.backtracking==0 ) { struct.setNextMemberVisibility(Struct.MemberVisibility.Protected); } struct.addDeclaration(functionPointerOrSimpleVarDecl10); if ( state.backtracking==0 ) { struct.setNextMemberVisibility(Struct.MemberVisibility.Public); } struct.addDeclaration(objCMethodDecl11); struct.addDeclaration(objCPropertyDecl12);
Element comel = parent != null && parent instanceof TypeDef ? parent : functionSignature; Struct callbackStruct = new Struct(); configureCallbackStruct(callbackStruct); callbackStruct.setTag(ident(chosenName)); if (!result.config.noComments) { callbackStruct.addToCommentBefore(comel.getCommentBefore(), comel.getCommentAfter(), getFileCommentContent(comel)); for (Declaration d : callbackStruct.getDeclarations()) { if (d instanceof Function) { callbackStruct.addAnnotations(callbackStruct.getAnnotations()); callbackStruct.setAnnotations(null); break;
if (struct.isForwardDeclaration())// && !result.structsByName.get(structName).isForwardDeclaration()) boolean isUnion = struct.getType() == Struct.Type.CUnion; boolean inheritsFromStruct = false; Identifier baseClass = null; if (!onlyFields) { if (!struct.getParents().isEmpty()) { for (SimpleTypeRef parentName : struct.getParents()) { Struct parent = result.structsByName.get(parentName.getName()); if (parent == null) { for (Declaration d : struct.getDeclarations()) { if (d instanceof VariablesDeclaration) { succeeded = convertVariablesDeclaration((VariablesDeclaration) d, childSignatures, structJavaClass, iChild, false, structName, callerLibraryClass, callerLibrary) && succeeded; : new Statement.Return(implCall))); method.addModifiers(ModifierType.Public, isStatic ? ModifierType.Static : null); structJavaClass.addDeclaration(method); byRef.addModifiers(ModifierType.Abstract); byVal.addModifiers(ModifierType.Abstract); structJavaClass.addDeclaration(createNewStructMethod("newByReference", byRef)); structJavaClass.addDeclaration(createNewStructMethod("newByValue", byVal)); structJavaClass.addDeclaration(createNewStructMethod("newInstance", structJavaClass)); structJavaClass.addDeclaration(createNewStructArrayMethod(structJavaClass, isUnion));
public int countFieldsInStruct(Struct s) throws UnsupportedConversionException { int count = 0; for (Declaration declaration : s.getDeclarations()) { if (declaration instanceof VariablesDeclaration) { count += ((VariablesDeclaration) declaration).getDeclarators().size(); } } for (SimpleTypeRef parentName : s.getParents()) { Struct parent = result.structsByName.get(parentName.getName()); if (parent == null) { throw new UnsupportedConversionException(s, "Cannot find parent " + parentName + " of struct " + s); } count += countFieldsInStruct(parent); } return count; }
Struct byVal*/, Struct nativeStruct) throws IOException { List<Declaration> initialMembers = new ArrayList<Declaration>(structJavaClass.getDeclarations()); Set<String> signatures = new TreeSet<String>(); addConstructor(structJavaClass, emptyConstructor); boolean isUnion = nativeStruct.getType() == Struct.Type.CUnion; boolean addPointerConstructor = true; if (isUnion) { structJavaClass.addDeclaration(unionValConstr); orderedFieldNames.toArray(new Expression[orderedFieldNames.size()])); Block getFieldOrderImpl; if (nativeStruct.getParents().isEmpty()) { getFieldOrderImpl = block(new Statement.Return(selfList)); } else { structJavaClass.addDeclaration(getFieldOrder); structJavaClass.addDeclaration(fieldsConstr);
private void parseClasses(String framework, Node signatures, SourceFile sf) throws XPathExpressionException { for (Node classe : XMLUtils.getChildrenByName(signatures, "class")) { Struct cs = parseClasse(classe, Struct.Type.ObjCClass, sf); if (cs == null) { continue; } if (!"NSObject".equals(String.valueOf(cs.getTag().toString()))) { cs.setParents(typeRef(ident("NSObject"))); } // cs.accept(result); } for (Node classe : XMLUtils.getChildrenByName(signatures, "informal_protocol")) { Struct cs = parseClasse(classe, Struct.Type.ObjCClass, sf); if (cs == null) { continue; } cs.setCategoryName(cs.getTag() == null ? null : cs.getTag().toString()); cs.setTag(ident("NSObject")); // cs.accept(result); } }
public void addDeclarations(Collection<? extends Declaration> ds) { if (ds == null) return; for (Declaration d : ds) addDeclaration(d); } public void setDeclarations(List<Declaration> declarations) {
private void addAllocIfMissing(Struct in, String allocName) { Identifier n = in.getTag(); if (n.equals("NSObject") || n.equals("NSClass")) { return; } boolean hasAlloc = false; for (Declaration d : in.getDeclarations()) { if (d instanceof Function) { Function f = (Function) d; if (f.getArgs().isEmpty() && allocName.equals(f.getName())) { hasAlloc = true; break; } } } if (!hasAlloc) { in.addDeclaration(new Function(Function.Type.ObjCMethod, ident(allocName), typeRef(in.getTag())).addModifiers(ModifierType.Static)); } }
struct.addModifiers(ModifierType.Final); struct.importDetails(globals, false); struct.moveAllCommentsBefore(); Identifier instTypeName = ident(name + "_holder"); Struct holderStruct = result.declarationsConverter.publicStaticClass(instTypeName, ident(result.config.runtime.structClass), Struct.Type.JavaClass, null); holderStruct.addModifiers(ModifierType.Final); VariablesDeclaration vd = ((JNADeclarationsConverter) result.declarationsConverter).convertVariablesDeclarationToJNA("value", type, new int[1], callerLibraryName); if (vd.getValueType().toString().equals(result.config.runtime.pointerClass.getName())) { hasOffset = false; } else { holderStruct.addDeclaration(vd); Function pointerConstructor = new Function(Function.Type.JavaMethod, instTypeName, null, new Arg("pointer", new TypeRef.SimpleTypeRef(result.config.runtime.pointerClass.getName()))); new Statement.ExpressionStatement(methodCall("useMemory", varRef("pointer"), expr(0))), new Statement.ExpressionStatement(methodCall("read")))); holderStruct.addDeclaration(pointerConstructor); struct.addDeclaration(decl(holderStruct)); struct.addDeclaration(new VariablesDeclaration(instType, new Declarator.DirectDeclarator(instName.toString())).addModifiers(ModifierType.Private, ModifierType.Static)); Expression.VariableRef instRef = new Expression.VariableRef(instName); Expression ptrExpr = methodCall( struct.addDeclaration(new Function(Function.Type.JavaMethod, ident("get"), instType).setBody(new Statement.Block( new Statement.If( expr(instRef, Expression.BinaryOperator.IsEqual, nullExpr()),
super.visitJavaInterface(struct); if (struct.getParents().contains(ident(Callback.class))) { Function f = null; for (Declaration d : struct.getDeclarations()) { if (d instanceof Function) { f = (Function) d; argDefs.add(n + ": " + vt); String cbClassName = struct.getTag().toString(), scbClassName = cbClassName + "_scala"; String cbClassPath = path.peek().toString(); int ac = argTypes.size();
instanceStruct.setResolvedJavaIdentifier(fullClassName); instanceStruct.addDeclaration(createProxyCopy(f, (Function) decl)); signatures.addMethod((Function) decl); if (classStruct.getType() == Type.JavaClass) { decl.addModifiers(ModifierType.Public, ModifierType.Abstract); decl.reorganizeModifiers(); instanceStruct.addDeclaration(addedF); if (instanceStruct.getType() == Type.JavaClass) { decl.addModifiers(ModifierType.Public, ModifierType.Abstract); decl.reorganizeModifiers();