use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class NameTableTest method testTypeVariableWithTypeVariableBounds.
public void testTypeVariableWithTypeVariableBounds() {
String source = "class A<T> { <E extends T> void foo(E e) {} }";
CompilationUnit unit = translateType("A", source);
NameTable nameTable = unit.getEnv().nameTable();
final ExecutableElement[] methodElement = new ExecutableElement[1];
unit.accept(new TreeVisitor() {
@Override
public void endVisit(MethodDeclaration node) {
ExecutableElement element = node.getExecutableElement();
if (ElementUtil.getName(element).equals("foo")) {
methodElement[0] = element;
}
}
});
assertNotNull(methodElement[0]);
TypeMirror paramType = methodElement[0].getParameters().get(0).asType();
assertEquals("id", nameTable.getObjCType(paramType));
}
use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class EnumRewriter method addValueOfMethod.
private void addValueOfMethod(EnumDeclaration node) {
TypeElement type = node.getTypeElement();
ExecutableElement method = ElementUtil.findMethod(type, "valueOf", "java.lang.String");
assert method != null : "Can't find valueOf method on enum type.";
String typeName = nameTable.getFullName(type);
int numConstants = node.getEnumConstants().size();
VariableElement nameParam = GeneratedVariableElement.newParameter("name", method.getParameters().get(0).asType(), method);
MethodDeclaration methodDecl = new MethodDeclaration(method);
methodDecl.addParameter(new SingleVariableDeclaration(nameParam));
Block body = new Block();
methodDecl.setBody(body);
StringBuilder impl = new StringBuilder();
if (numConstants > 0) {
impl.append(UnicodeUtils.format(" for (int i = 0; i < %s; i++) {\n" + " %s *e = %s_values_[i];\n" + " if ([name isEqual:[e name]]) {\n" + " return e;\n" + " }\n" + " }\n", numConstants, typeName, typeName));
}
impl.append(" @throw create_JavaLangIllegalArgumentException_initWithNSString_(name);\n" + " return nil;");
body.addStatement(new NativeStatement(impl.toString()));
node.addBodyDeclaration(methodDecl);
}
use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class EnumRewriter method addValuesMethod.
private void addValuesMethod(EnumDeclaration node) {
TypeElement type = node.getTypeElement();
ExecutableElement method = ElementUtil.findMethod(type, "values");
assert method != null : "Can't find values method on enum type.";
String typeName = nameTable.getFullName(type);
MethodDeclaration methodDecl = new MethodDeclaration(method);
Block body = new Block();
methodDecl.setBody(body);
body.addStatement(new NativeStatement(UnicodeUtils.format(" return [IOSObjectArray arrayWithObjects:%s_values_ count:%s type:%s_class_()];", typeName, node.getEnumConstants().size(), typeName)));
node.addBodyDeclaration(methodDecl);
}
use of com.google.devtools.j2objc.ast.MethodDeclaration 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 com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class DestructorGenerator method addDeallocMethod.
private void addDeallocMethod(AbstractTypeDeclaration node) {
TypeElement type = node.getTypeElement();
boolean hasFinalize = hasFinalizeMethod(type);
List<Statement> releaseStatements = createReleaseStatements(node);
if (releaseStatements.isEmpty() && !hasFinalize) {
return;
}
ExecutableElement deallocElement = GeneratedExecutableElement.newMethodWithSelector(NameTable.DEALLOC_METHOD, typeUtil.getVoid(), type).addModifiers(Modifier.PUBLIC);
MethodDeclaration deallocDecl = new MethodDeclaration(deallocElement);
deallocDecl.setHasDeclaration(false);
Block block = new Block();
deallocDecl.setBody(block);
List<Statement> stmts = block.getStatements();
if (hasFinalize) {
String clsName = nameTable.getFullName(type);
stmts.add(new NativeStatement("JreCheckFinalize(self, [" + clsName + " class]);"));
}
stmts.addAll(releaseStatements);
if (options.useReferenceCounting()) {
stmts.add(new ExpressionStatement(new SuperMethodInvocation(new ExecutablePair(superDeallocElement))));
}
node.addBodyDeclaration(deallocDecl);
}
Aggregations