use of javax.lang.model.element.ExecutableElement in project j2objc by google.
the class Functionizer method makeFunction.
/**
* Create an equivalent function declaration for a given method.
*/
private FunctionDeclaration makeFunction(MethodDeclaration method) {
ExecutableElement elem = method.getExecutableElement();
TypeElement declaringClass = ElementUtil.getDeclaringClass(elem);
boolean isInstanceMethod = !ElementUtil.isStatic(elem) && !ElementUtil.isConstructor(elem);
FunctionDeclaration function = new FunctionDeclaration(nameTable.getFullFunctionName(elem), elem.getReturnType());
function.setJniSignature(signatureGenerator.createJniFunctionSignature(elem));
function.setLineNumber(method.getLineNumber());
if (!ElementUtil.isStatic(elem)) {
VariableElement var = GeneratedVariableElement.newParameter(NameTable.SELF_NAME, declaringClass.asType(), null);
function.addParameter(new SingleVariableDeclaration(var));
}
TreeUtil.copyList(method.getParameters(), function.getParameters());
function.setModifiers(method.getModifiers() & Modifier.STATIC);
if (ElementUtil.isPrivate(elem) || (isInstanceMethod && !ElementUtil.isDefault(elem))) {
function.addModifiers(Modifier.PRIVATE);
} else {
function.addModifiers(Modifier.PUBLIC);
}
if (Modifier.isNative(method.getModifiers())) {
function.addModifiers(Modifier.NATIVE);
return function;
}
function.setBody(TreeUtil.remove(method.getBody()));
if (ElementUtil.isStatic(elem)) {
// Add class initialization invocation, since this may be the first use of this class.
String initName = UnicodeUtils.format("%s_initialize", nameTable.getFullName(declaringClass));
TypeMirror voidType = typeUtil.getVoid();
FunctionElement initElement = new FunctionElement(initName, voidType, declaringClass);
FunctionInvocation initCall = new FunctionInvocation(initElement, voidType);
function.getBody().addStatement(0, new ExpressionStatement(initCall));
} else {
FunctionConverter.convert(function);
}
return function;
}
use of javax.lang.model.element.ExecutableElement in project j2objc by google.
the class Functionizer method endVisit.
@Override
public void endVisit(ClassInstanceCreation node) {
ExecutableElement element = node.getExecutableElement();
TypeElement type = ElementUtil.getDeclaringClass(element);
FunctionElement funcElement = newAllocatingConstructorElement(element);
FunctionInvocation invocation = new FunctionInvocation(funcElement, node.getTypeMirror());
invocation.setHasRetainedResult(node.hasRetainedResult() || options.useARC());
List<Expression> args = invocation.getArguments();
Expression outerExpr = node.getExpression();
if (outerExpr != null) {
args.add(TreeUtil.remove(outerExpr));
} else if (captureInfo.needsOuterParam(type)) {
args.add(new ThisExpression(ElementUtil.getDeclaringClass(type).asType()));
}
Expression superOuterArg = node.getSuperOuterArg();
if (superOuterArg != null) {
args.add(TreeUtil.remove(superOuterArg));
}
TreeUtil.moveList(node.getCaptureArgs(), args);
TreeUtil.moveList(node.getArguments(), args);
node.replaceWith(invocation);
assert funcElement.getParameterTypes().size() == args.size();
}
use of javax.lang.model.element.ExecutableElement in project j2objc by google.
the class Functionizer method determineFunctionizableMethods.
/**
* Determines the set of methods to functionize. In addition to a method being
* final we must also find an invocation for that method. Static methods, though,
* are always functionized since there are no dynamic dispatch issues.
*/
private Set<ExecutableElement> determineFunctionizableMethods(final CompilationUnit unit) {
final Set<ExecutableElement> functionizableDeclarations = Sets.newHashSet();
final Set<ExecutableElement> invocations = Sets.newHashSet();
unit.accept(new TreeVisitor() {
@Override
public void endVisit(MethodDeclaration node) {
if (canFunctionize(node)) {
functionizableDeclarations.add(node.getExecutableElement());
}
}
@Override
public void endVisit(MethodInvocation node) {
invocations.add(node.getExecutableElement());
}
});
return Sets.intersection(functionizableDeclarations, invocations);
}
use of javax.lang.model.element.ExecutableElement in project j2objc by google.
the class Functionizer method endVisit.
@Override
public void endVisit(SuperConstructorInvocation node) {
ExecutableElement element = node.getExecutableElement();
AbstractTypeDeclaration typeDecl = TreeUtil.getEnclosingType(node);
TypeElement type = typeDecl.getTypeElement();
FunctionElement funcElement = newFunctionElement(element);
FunctionInvocation invocation = new FunctionInvocation(funcElement, typeUtil.getVoid());
List<Expression> args = invocation.getArguments();
args.add(new ThisExpression(ElementUtil.getDeclaringClass(element).asType()));
if (typeDecl instanceof TypeDeclaration) {
TypeDeclaration typeDeclaration = (TypeDeclaration) typeDecl;
if (captureInfo.needsOuterParam(ElementUtil.getSuperclass(type))) {
Expression outerArg = TreeUtil.remove(node.getExpression());
args.add(outerArg != null ? outerArg : typeDeclaration.getSuperOuter().copy());
}
TreeUtil.moveList(typeDeclaration.getSuperCaptureArgs(), args);
}
TreeUtil.moveList(node.getArguments(), args);
if (ElementUtil.isEnum(type)) {
for (VariableElement param : captureInfo.getImplicitEnumParams()) {
args.add(new SimpleName(param));
}
}
node.replaceWith(new ExpressionStatement(invocation));
assert funcElement.getParameterTypes().size() == args.size();
}
use of javax.lang.model.element.ExecutableElement in project j2objc by google.
the class Functionizer method makeAllocatingConstructor.
/**
* Create a wrapper for a constructor that does the object allocation.
*/
private FunctionDeclaration makeAllocatingConstructor(MethodDeclaration method, boolean releasing) {
assert method.isConstructor();
ExecutableElement element = method.getExecutableElement();
TypeElement declaringClass = ElementUtil.getDeclaringClass(element);
String name = releasing ? nameTable.getReleasingConstructorName(element) : nameTable.getAllocatingConstructorName(element);
FunctionDeclaration function = new FunctionDeclaration(name, declaringClass.asType());
function.setLineNumber(method.getLineNumber());
function.setModifiers(ElementUtil.isPrivate(element) ? Modifier.PRIVATE : Modifier.PUBLIC);
function.setReturnsRetained(!releasing);
TreeUtil.copyList(method.getParameters(), function.getParameters());
Block body = new Block();
function.setBody(body);
StringBuilder sb = new StringBuilder(releasing ? "J2OBJC_CREATE_IMPL(" : "J2OBJC_NEW_IMPL(");
sb.append(nameTable.getFullName(declaringClass));
sb.append(", ").append(nameTable.getFunctionName(element));
for (SingleVariableDeclaration param : function.getParameters()) {
sb.append(", ").append(nameTable.getVariableQualifiedName(param.getVariableElement()));
}
sb.append(")");
body.addStatement(new NativeStatement(sb.toString()));
return function;
}
Aggregations