use of javax.lang.model.element.VariableElement in project j2objc by google.
the class OuterReferenceResolverTest method testNestedLocalClassesWithNestedCreations.
public void testNestedLocalClassesWithNestedCreations() {
// This test is particularly tricky for OuterReferenceResolver because A captures variable i,
// but that is not known until after A's creation. A's creation occurs within B, which requires
// B to have an outer field in order to access A's capturing field for i. B's creation therefore
// requires the outer field to be passed as an outer argument.
// Because of the cascading effects of the statements in this test and the order in which they
// occur, we would need to do three passes over the code to resolve B's creation successfuly.
resolveSource("Test", "class Test { void test(int i) { class A { " + "void foo() { class B { void bar() { new B(); new A(); } } } " + "int other() { return i; } } } }");
ClassInstanceCreation bCreate = (ClassInstanceCreation) nodesByType.get(Kind.CLASS_INSTANCE_CREATION).get(0);
Expression outerArg = bCreate.getExpression();
assertTrue(outerArg instanceof SimpleName);
VariableElement var = TreeUtil.getVariableElement(outerArg);
assertNotNull(var);
assertEquals("this$0", ElementUtil.getName(var));
}
use of javax.lang.model.element.VariableElement in project j2objc by google.
the class AbstractMethodRewriter method newReturnTypeNarrowingDeclaration.
private MethodDeclaration newReturnTypeNarrowingDeclaration(String selector, ExecutablePair method, TypeElement declaringClass) {
GeneratedExecutableElement element = GeneratedExecutableElement.newMethodWithSelector(selector, method.type().getReturnType(), declaringClass).addModifiers(ElementUtil.getVisibilityModifiers(method.element())).addModifiers(Modifier.ABSTRACT);
MethodDeclaration decl = new MethodDeclaration(element);
if (!declaringClass.getKind().isInterface()) {
unit.setHasIncompleteImplementation();
}
int argCount = 0;
for (TypeMirror paramType : method.type().getParameterTypes()) {
VariableElement param = GeneratedVariableElement.newParameter("arg" + argCount++, paramType, element);
element.addParameter(param);
decl.addParameter(new SingleVariableDeclaration(param));
}
return decl;
}
use of javax.lang.model.element.VariableElement in project j2objc by google.
the class AnnotationRewriter method endVisit.
@Override
public void endVisit(AnnotationTypeDeclaration node) {
TypeElement type = node.getTypeElement();
if (!ElementUtil.isRuntimeAnnotation(type)) {
return;
}
List<AnnotationTypeMemberDeclaration> members = TreeUtil.getAnnotationMembers(node);
List<BodyDeclaration> bodyDecls = node.getBodyDeclarations();
Map<ExecutableElement, VariableElement> fieldElements = createMemberFields(node, members);
addMemberProperties(node, members, fieldElements);
addDefaultAccessors(node, members);
bodyDecls.add(createAnnotationTypeMethod(type));
bodyDecls.add(createDescriptionMethod(type));
addConstructor(node, fieldElements);
}
use of javax.lang.model.element.VariableElement in project j2objc by google.
the class AnnotationRewriter method createMemberFields.
// Create an instance field for each member.
private Map<ExecutableElement, VariableElement> createMemberFields(AnnotationTypeDeclaration node, List<AnnotationTypeMemberDeclaration> members) {
TypeElement type = node.getTypeElement();
Map<ExecutableElement, VariableElement> fieldElements = new HashMap<>();
for (AnnotationTypeMemberDeclaration member : members) {
ExecutableElement memberElement = member.getExecutableElement();
String propName = NameTable.getAnnotationPropertyName(memberElement);
VariableElement field = GeneratedVariableElement.newField(propName, memberElement.getReturnType(), type);
node.addBodyDeclaration(new FieldDeclaration(field, null));
fieldElements.put(memberElement, field);
}
return fieldElements;
}
use of javax.lang.model.element.VariableElement in project j2objc by google.
the class CastResolver method endVisit.
/**
* Adds a cast check to compareTo methods. This helps Comparable types behave
* well in sorted collections which rely on Java's runtime type checking.
*/
@Override
public void endVisit(MethodDeclaration node) {
ExecutableElement element = node.getExecutableElement();
if (!ElementUtil.getName(element).equals("compareTo") || node.getBody() == null) {
return;
}
DeclaredType comparableType = typeUtil.findSupertype(ElementUtil.getDeclaringClass(element).asType(), "java.lang.Comparable");
if (comparableType == null) {
return;
}
List<? extends TypeMirror> typeArguments = comparableType.getTypeArguments();
List<? extends VariableElement> parameters = element.getParameters();
if (typeArguments.size() != 1 || parameters.size() != 1 || !typeArguments.get(0).equals(parameters.get(0).asType())) {
return;
}
VariableElement param = node.getParameter(0).getVariableElement();
FunctionInvocation castCheck = createCastCheck(typeArguments.get(0), new SimpleName(param));
if (castCheck != null) {
node.getBody().addStatement(0, new ExpressionStatement(castCheck));
}
}
Aggregations