private Function newStaticInit() { Function f = new Function(Function.Type.StaticInit, null, null).setBody(block( stat(methodCall( expr(typeRef(BridJ.class)), Expression.MemberRefStyle.Dot, "register")))).addModifiers(ModifierType.Static); return f; }
private void fillIn(Signatures signatures, Identifier functionName, Function nativeMethod, NL4JConversion returnType, List<NL4JConversion> paramTypes, List<String> paramNames, Identifier varArgType, String varArgName, boolean isCallback, boolean useRawTypes) { for (int i = 0, n = paramTypes.size(); i < n; i++) { NL4JConversion paramType = paramTypes.get(i); String paramName = paramNames.get(i); nativeMethod.addArg(paramType.annotateTypedType(new Arg(paramName, paramType.getTypeRef(useRawTypes)), useRawTypes));//.getTypedTypeRef()))); } if (varArgType != null) { nativeMethod.addArg(new Arg(varArgName, typeRef(varArgType.clone()))).setVarArg(true); } if (returnType != null) { returnType.annotateTypedType(nativeMethod, useRawTypes); nativeMethod.setValueType(returnType.getTypeRef(useRawTypes)); } String natSig = nativeMethod.computeSignature(SignatureType.JavaStyle); Identifier javaMethodName = signatures == null ? functionName : signatures.findNextMethodName(natSig, functionName); if (!javaMethodName.equals(functionName)) { nativeMethod.setName(javaMethodName); } }
@Override public Boolean adapt(Function value) { List<Modifier> mods = value.getModifiers(); if (ModifierType.__kernel.isContainedBy(mods)) return true; if (value.getValueType() == null) return null; mods = value.getValueType().getModifiers(); return ModifierType.__kernel.isContainedBy(mods); } };
@Override public boolean replaceChild(Element child, Element by) { if (child == getBody()) { setBody((Statement.Block)by); return true; } if (child == getName()) { setName((Identifier)by); return true; } if (replaceChild(args, Arg.class, this, child, by)) return true; if (replaceChild(thrown, TypeRef.class, this, child, by)) return true; if (replaceChild(initializers, FunctionCall.class, this, child, by)) return true; return super.replaceChild(child, by); }
public void visitFunction(Function function) { visitDeclaration(function); visit(function.getArgs()); visit(function.getThrown()); visit(function.getInitializers()); visit(function.getBody()); visit(function.getName()); }
if (result.config.choicesInputFile != null) { for (Function alt : alternativesPair.getValue()) { implementations.addDeclaration(alt.clone()); Function natFunc = new Function(); Element parent = function.getParentElement(); boolean isMethod = parent instanceof Struct; if (isMethod) { Boolean alreadyRetained = Result.getMap(result.retainedRetValFunctions, library).get(functionName.toString()); if (alreadyRetained != null && alreadyRetained) { natFunc.addAnnotation(new Annotation(typeRef(AlreadyRetained.class), expr(alreadyRetained))); boolean isObjectiveC = function.getType() == Type.ObjCMethod; natFunc.setType(Function.Type.JavaMethod); if (result.config.synchronizedMethods && !isCallback && result.config.useJNADirectCalls) { natFunc.addModifiers(ModifierType.Synchronized); natFunc.addModifiers(ModifierType.Public, ModifierType.Static, ModifierType.Native); returnType = function.getValueType(); if (returnType == null) { returnType = new TypeRef.Primitive("int"); returnType.addModifiers(function.getModifiers()); if (function.getName() != null && !modifiedMethodName.equals(function.getName().toString())) {
assert implementations != null; boolean extractingDeclarations = declarations != null && declarations.resolveHolder() != implementations.resolveHolder(); Element parent = function.getParentElement(); MemberVisibility visibility = function.getVisibility(); boolean isPublic = visibility == MemberVisibility.Public || function.hasModifier(ModifierType.Public); boolean isPrivate = visibility == MemberVisibility.Private || function.hasModifier(ModifierType.Private); boolean isProtected = visibility == MemberVisibility.Protected || function.hasModifier(ModifierType.Protected); boolean isInStruct = parent instanceof Struct; if (isInStruct && result.config.skipPrivateMembers && (isPrivate || !isPublic && !isProtected)) { return; boolean isStatic = function.hasModifier(ModifierType.Static); boolean isConstructor = iConstructor != -1; Function nativeMethod = new Function(Type.JavaMethod, ident(functionName), null); nativeMethod.addModifiers(ModifierType.Synchronized); if (function.getName() != null && !isCallback && (!functionName.toString().equals(function.getName().toString()) || result.config.forceNames)) annotateActualName(nativeMethod, function.getName()); annotatedActualName = true; Function rawMethod = nativeMethod.clone(); boolean isObjectiveC = function.getType() == Type.ObjCMethod; Set<String> argNames = new TreeSet<String>();
Function convDecl = new Function(); conv.annotateTypedType(convDecl, useRawTypes); convDecl.setType(Type.JavaMethod); convDecl.addModifiers(ModifierType.Public); convDecl.addAnnotation(new Annotation(result.config.runtime.typeRef(JNAeratorConfig.Runtime.Ann.Length), new OpaqueExpression("{" + StringUtils.implode(conv.arrayLengths, ", ") + "}"))); convDecl.addAnnotation(new Annotation(result.config.runtime.typeRef(JNAeratorConfig.Runtime.Ann.Bits), conv.bits)); convDecl.addAnnotation(new Annotation(result.config.runtime.typeRef(JNAeratorConfig.Runtime.Ann.ByValue))); convDecl.importDetails(e, false); convDecl.importDetails(mutatedType, true); convDecl.moveAllCommentsBefore(); convDecl.deDioxygenizeCommentBefore(); convDecl.setName(ident(name)); convDecl.addAnnotation(new Annotation(result.config.runtime.typeRef(JNAeratorConfig.Runtime.Ann.Field), expr(fieldIndex))); convDecl.setValueType(conv.getTypeRef(useRawTypes)); TypeRef javaType = convDecl.getValueType(); String pointerGetSetMethodSuffix = StringUtils.capitalize(javaType.toString()); Function getter = convDecl.clone();
Function fDirect = original.clone(); for (Arg arg : fDirect.getArgs()) { if (isFakePointerRef(result, arg.getValueType())) { if (iArg == 0 && arg.getValueType().toString().equals(thisFakePtrRefStr)) { boolean returnsFakePointer = isFakePointerRef(result, fDirect.getValueType()); boolean needsDirect = !fakePointersLocations.isEmpty() || returnsFakePointer; if (needsDirect) { if (returnsFakePointer) { finalCall = new Expression.New(fDirect.getValueType(), varRef(indirectRetVarName)); toDirectFakePointer(result, fDirect); ((DeclarationsHolder) original.getParentElement()).addDeclaration(fDirect); if (original.computeSignature(SignatureType.JavaStyle).equals(fDirect.computeSignature(SignatureType.JavaStyle))) { fDirect.setName(ident(original.getName() + "$direct")); directFunctionName = fDirect.getName().toString(); Function f = original.clone(); List<Arg> args = new ArrayList<Arg>(f.getArgs()); f.setModifiers(Collections.EMPTY_LIST); f.addModifiers(ModifierType.Public); if (thisLocation < 0) { f.addModifiers(ModifierType.Static); String functionName = f.getName().toString();
Function f = (Function) d; boolean isVirtual = f.hasModifier(ModifierType.Virtual); boolean isConstructor = f.getName().equals(structName) && (f.getValueType() == null || f.getValueType().toString().equals("void")); if (isConstructor && f.getArgs().isEmpty()) { continue; // default constructor was already generated boolean commentOut = false; if (isVirtual) { method.addAnnotation(new Annotation(result.config.runtime.typeRef(JNAeratorConfig.Runtime.Ann.Virtual), expr(iVirtual))); } else if (method.getValueType() == null) { method.addAnnotation(new Annotation(result.config.runtime.typeRef(JNAeratorConfig.Runtime.Ann.Constructor), expr(iConstructor))); isConstructor = true; if (method.getName().toString().equals("operator")) { commentOut = true; structJavaClass.addDeclaration(new EmptyDeclaration(method.toString())); } else { structJavaClass.addDeclaration(method); Function defaultConstructor = new Function(Type.JavaMethod, ident(structName), null).setBody(block(stat(methodCall("super")))).addModifiers(ModifierType.Public); if (childSignatures.addMethod(defaultConstructor)) { structJavaClass.addDeclaration(defaultConstructor); Function castConstructor = new Function(Type.JavaMethod, ident(structName), null, new Arg(ptrName, typeRef(result.config.runtime.pointerClass))).setBody(block(stat(methodCall("super", varRef(ptrName))))).addModifiers(ModifierType.Public); if (childSignatures.addMethod(castConstructor)) { structJavaClass.addDeclaration(castConstructor);
String name = meth.getName().toString(); if (!name.matches("^init([A-Z].*|)$")) { return null; Function createCopy = meth.clone(); createCopy.setCommentBefore("Factory method"); createCopy.addToCommentBefore("@see #" + meth.computeSignature(SignatureType.JavaStyle)); createCopy.setName(ident("create" + name.substring("init".length()))); createCopy.addModifiers(ModifierType.Public, ModifierType.Static); createCopy.reorganizeModifiers(); Expression[] args = new Expression[meth.getArgs().size()]; int i = 0; for (Arg arg : meth.getArgs()) { args[i++] = varRef(arg.getName()); "alloc"), Expression.MemberRefStyle.Dot, meth.getName().toString(), args); memberRef(expr(typeRef(instanceStruct.getTag())), MemberRefStyle.Dot, "class")); createCopy.setBody(new Block(new Statement.Return(val))); return createCopy;
String sig, Identifier functionName, String library, int iConstructor) { assert implementations == declarations || declarations == null; String methodName = library + "_" + (function.getParentElement() instanceof Struct ? ((Struct) function.getParentElement()).getTag() + "_" : "") + function.getName(); Function method = new Function(Function.Type.CppMethod, ident(methodName), typeRef(v8Ident("Handle", v8Ident("Value")))); method.addArg(new Arg(argsName, new TypeRef.Pointer(typeRef(v8Ident("Arguments")), Declarator.PointerStyle.Reference).addModifiers(ModifierType.Const))); Block body = new Block(); List<Arg> args = function.getArgs(); int argCount = args.size(); body.addStatement( TypeRef retTr = resolveTypeDef(function.getValueType()); NodeType retNodeType = getNodeType(retTr); body.addStatement(new VariablesDeclaration(typeRef(v8Ident("HandleScope")), new Declarator.DirectDeclarator(scopeName))); Expression call = methodCall(function.getName(), params.toArray(new Expression[params.size()])); if (retNodeType == NodeType.Void) { body.addStatement(stat(call)); } else { VariablesDeclaration retDecl = new VariablesDeclaration(function.getValueType().clone(), new Declarator.DirectDeclarator(returnValueName, call)); body.addStatement(retDecl); throw new UnsupportedConversionException(function, "Return type not handled: " + retTr + " (" + retNodeType + ")"); method.setBody(body);
Function emptyConstructor = new Function(Function.Type.JavaMethod, structName.clone(), null).addModifiers(ModifierType.Public); emptyConstructor.setBody(block(stat(methodCall("super")))); addConstructor(structJavaClass, emptyConstructor); Function unionValConstr = new Function(Function.Type.JavaMethod, structName.clone(), null, new Arg(name, tr.clone())); if (!result.config.noComments) { if (!commentBits.isEmpty()) { unionValConstr.addToCommentBefore("@param " + name + " " + StringUtils.implode(commentBits, ", or ")); unionValConstr.addModifiers(ModifierType.Public); unionValConstr.setBody(block( stat(methodCall("super")), tr instanceof TypeRef.ArrayRef ? throwIfArraySizeDifferent(name) : null, if (signatures.add(unionValConstr.computeSignature(SignatureType.JavaStyle))) { structJavaClass.addDeclaration(unionValConstr); Function fieldsConstr = new Function(Function.Type.JavaMethod, structName.clone(), null); fieldsConstr.setBody(new Block()).addModifiers(ModifierType.Public); String name = chooseJavaArgName(vd.getDeclarators().get(0).resolveName(), iArg, names); namesById.put(vd.getId(), name); fieldsConstr.addArg(new Arg(name, vd.getValueType().clone())); iArg++; if (iArg == 1 && isJNAPointer(fieldsConstr.getArgs().get(0).getValueType())) addPointerConstructor = false;
public void visitFunction(Function e) { TypeRef valueType = e.getValueType(); Identifier name = e.getName(); List<Modifier> modifiers = e.getModifiers(); if (e.getType() == null) { append("<no function type>"); return; if (!e.getAnnotations().isEmpty()) { implode(e.getAnnotations(), "\n" + indent).append("\n", indent); switch (e.getType()) { case StaticInit: implode(modifiers, " "); space(!modifiers.isEmpty()); append(e.getBody() == null ? ";" : e.getBody()); break; case CFunction: case CppMethod: case JavaMethod: if (name != null && name.equals("operator") && e.getType() == Function.Type.CppMethod) { append(name); space(); append("(").implode(e.getArgs(), ", ").append(")"); switch (e.getType()) { case JavaMethod: if (!e.getThrown().isEmpty()) {
Identifier methodImplName = method.getName().clone(); Identifier methodName = result.typeConverter.getValidJavaMethodName(f.getName()); method.setName(methodName); List<Expression> args = new ArrayList<Expression>(); boolean isStatic = f.hasModifier(ModifierType.Static); int iArg = 0; for (Arg arg : new ArrayList<Arg>(method.getArgs())) { if (iArg == 0 && !isStatic) { arg.replaceBy(null); method.setBody(block( "void".equals(String.valueOf(method.getValueType())) ? stat(implCall) : new Statement.Return(implCall))); method.addModifiers(ModifierType.Public, isStatic ? ModifierType.Static : null); structJavaClass.addDeclaration(method);
private Function createProxyCopy(Function originalMethod, Function meth) { if (isMethodExcludedFromStaticForwarding(originalMethod)) { return null; } Function proxyCopy = meth.clone(); proxyCopy.addModifiers(ModifierType.Public, ModifierType.Static); proxyCopy.reorganizeModifiers(); Expression[] args = new Expression[meth.getArgs().size()]; int i = 0; for (Arg arg : meth.getArgs()) { args[i++] = varRef(arg.getName()); } Expression val = methodCall(methodCall(null, null, classInstanceGetterName), Expression.MemberRefStyle.Dot, meth.getName().toString(), args); proxyCopy.setBody(new Block( meth.getValueType() == null || "void".equals(meth.getValueType().toString()) ? stat(val) : new Statement.Return(val))); return proxyCopy; } // protected static _class_ _CLASS_ = org.rococoa.Rococoa.createClass("NSURL", _class_.class);
Function createCastMethod(Identifier name, Identifier classId, boolean isStatic) { Function m = new Function(); m.setType(Function.Type.JavaMethod); m.addModifiers(ModifierType.Public); m.setName(ident("as" + (isStatic ? "Static_" : "_") + name)); m.setValueType(typeRef(classId.clone())); m.setBody(block( new Statement.Return( methodCall( expr(typeRef(Rococoa.class)), MemberRefStyle.Dot, "cast", varRef("this"), result.typeConverter.typeLiteral(typeRef(classId.clone())))))); return m; }
methodType = parseAndReconciliateMethods(type, type64); if (tr == null) { tr = methodType.getValueType(); Function f = new Function(cfunction, ident(name), tr); f.addModifiers(ModifierType.Static); TypeRef at = parseType(arg); if (at == null && methodType != null) { if (iArg < methodType.getArgs().size()) { at = methodType.getArgs().get(iArg).getValueType(); } else { at = null; a.setValueType(at); a.setSelector(splitSelector == null || iArg >= splitSelector.length ? null : splitSelector[iArg]); f.addArg(a); iArg++;
function = new Function(); function.setType(Function.Type.ObjCMethod); function.addModifiers(ModifierType.Static); function = mark(function, getLine(tp)); function.setCommentBefore(getCommentBefore(tp.getTokenIndex())); function.setCommentBefore(getCommentBefore(tm.getTokenIndex())); function.setValueType(returnTypeRef); function.setName(new SimpleIdentifier((methodName!=null?methodName.getText():null))); function.setCommentAfter(getCommentAfterOnSameLine(methodName.getTokenIndex(), null)); Arg arg = new Arg((argName1!=null?argName1.getText():null), argType1); arg.setSelector((methodName!=null?methodName.getText():null)); function.addArg(arg); Arg arg = new Arg((argName!=null?argName.getText():null), argType); arg.setSelector((sel!=null?sel.getText():null)); function.addArg(arg); function.addArg(Arg.createVarArgs());
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)); } }