use of com.google.devtools.j2objc.ast.SimpleName in project j2objc by google.
the class JavaCloneWriter method endVisit.
@Override
public void endVisit(TypeDeclaration node) {
TypeElement type = node.getTypeElement();
VariableElement originalVar = GeneratedVariableElement.newParameter("original", type.asType(), null);
List<Statement> adjustments = getFieldAdjustments(node, originalVar);
if (adjustments.isEmpty()) {
return;
}
TypeMirror voidType = typeUtil.getVoid();
ExecutableElement javaCloneElement = GeneratedExecutableElement.newMethodWithSelector(JAVA_CLONE_METHOD, voidType, type).addParameter(originalVar);
MethodDeclaration declaration = new MethodDeclaration(javaCloneElement);
declaration.setHasDeclaration(false);
node.addBodyDeclaration(declaration);
declaration.addParameter(new SingleVariableDeclaration(originalVar));
Block body = new Block();
declaration.setBody(body);
List<Statement> statements = body.getStatements();
ExecutableElement javaCloneSuperElement = GeneratedExecutableElement.newMethodWithSelector(JAVA_CLONE_METHOD, voidType, typeUtil.getJavaObject());
SuperMethodInvocation superCall = new SuperMethodInvocation(new ExecutablePair(javaCloneSuperElement));
superCall.addArgument(new SimpleName(originalVar));
statements.add(new ExpressionStatement(superCall));
statements.addAll(adjustments);
}
use of com.google.devtools.j2objc.ast.SimpleName in project j2objc by google.
the class JavaToIOSMethodTranslator method visit.
@Override
public boolean visit(ClassInstanceCreation node) {
// translate any embedded method invocations
if (node.getExpression() != null) {
node.getExpression().accept(this);
}
for (Expression e : node.getArguments()) {
e.accept(this);
}
if (node.getAnonymousClassDeclaration() != null) {
node.getAnonymousClassDeclaration().accept(this);
}
ExecutableElement method = node.getExecutableElement();
String key = Mappings.getMethodKey(method, typeUtil);
String selector = Mappings.STRING_CONSTRUCTOR_TO_METHOD_MAPPINGS.get(key);
if (selector != null) {
assert !node.hasRetainedResult();
if (key.equals("java.lang.String.<init>(Ljava/lang/String;)V")) {
// Special case: replace new String(constant) to constant (avoid clang warning).
Expression arg = node.getArgument(0);
if (arg instanceof StringLiteral) {
node.replaceWith(arg.copy());
return false;
}
}
ExecutableElement newElement = GeneratedExecutableElement.newMappedMethod(selector, method);
MethodInvocation newInvocation = new MethodInvocation(new ExecutablePair(newElement), new SimpleName(ElementUtil.getDeclaringClass(method)));
TreeUtil.copyList(node.getArguments(), newInvocation.getArguments());
node.replaceWith(newInvocation);
}
return true;
}
use of com.google.devtools.j2objc.ast.SimpleName in project j2objc by google.
the class TranslationUtil method createAnnotationValue.
public Expression createAnnotationValue(TypeMirror type, AnnotationValue aValue) {
Object value = aValue.getValue();
if (value == null) {
return new NullLiteral(typeUtil.getNull());
} else if (value instanceof VariableElement) {
return new SimpleName((VariableElement) value);
} else if (TypeUtil.isArray(type)) {
assert value instanceof List;
ArrayType arrayType = (ArrayType) type;
@SuppressWarnings("unchecked") List<? extends AnnotationValue> list = (List<? extends AnnotationValue>) value;
List<Expression> generatedValues = new ArrayList<>();
for (AnnotationValue elem : list) {
generatedValues.add(createAnnotationValue(arrayType.getComponentType(), elem));
}
return createObjectArray(generatedValues, arrayType);
} else if (TypeUtil.isAnnotation(type)) {
assert value instanceof AnnotationMirror;
return createAnnotation((AnnotationMirror) value);
} else if (value instanceof TypeMirror) {
return new TypeLiteral((TypeMirror) value, typeUtil);
} else {
// Boolean, Character, Number, String
return TreeUtil.newLiteral(value, typeUtil);
}
}
use of com.google.devtools.j2objc.ast.SimpleName in project j2objc by google.
the class OuterReferenceResolverTest method testAnonymousClassCreatesLocalClassWithCaptures.
public void testAnonymousClassCreatesLocalClassWithCaptures() {
resolveSource("Test", "class Test { Runnable test(final Object o) { " + "class Local { public void foo() { o.toString(); } } " + "return new Runnable() { public void run() { new Local(); } }; } }");
TypeDeclaration runnableNode = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(2);
assertTrue(ElementUtil.isAnonymous(runnableNode.getTypeElement()));
List<VariableElement> innerFields = Lists.newArrayList(captureInfo.getCaptureFields(runnableNode.getTypeElement()));
assertEquals(1, innerFields.size());
assertEquals("val$o", ElementUtil.getName(innerFields.get(0)));
ClassInstanceCreation creationNode = (ClassInstanceCreation) nodesByType.get(Kind.CLASS_INSTANCE_CREATION).get(1);
List<Expression> captureArgs = creationNode.getCaptureArgs();
assertEquals(1, captureArgs.size());
Expression captureArg = captureArgs.get(0);
assertTrue(captureArg instanceof SimpleName);
VariableElement captureVar = TreeUtil.getVariableElement(captureArg);
assertNotNull(captureVar);
assertEquals("val$o", ElementUtil.getName(captureVar));
}
use of com.google.devtools.j2objc.ast.SimpleName in project j2objc by google.
the class OuterReferenceResolverTest method testInheritedOuterMethod.
public void testInheritedOuterMethod() {
resolveSource("Test", "class Test { class A { void foo() {} } class B extends A { " + "class Inner { void test() { foo(); } } } }");
TypeDeclaration aNode = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(1);
TypeDeclaration bNode = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(2);
TypeDeclaration innerNode = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(3);
assertFalse(captureInfo.needsOuterReference(aNode.getTypeElement()));
assertFalse(captureInfo.needsOuterReference(bNode.getTypeElement()));
assertTrue(captureInfo.needsOuterReference(innerNode.getTypeElement()));
// B will need an outer reference to Test so it can initialize its
// superclass A.
Expression bSuperOuter = bNode.getSuperOuter();
assertTrue(bSuperOuter instanceof SimpleName);
assertEquals("outer$", ElementUtil.getName(TreeUtil.getVariableElement(bSuperOuter)));
// foo() call will need to get to B's scope to call the inherited method.
MethodInvocation fooCall = (MethodInvocation) nodesByType.get(Kind.METHOD_INVOCATION).get(0);
Expression expr = fooCall.getExpression();
assertTrue(expr instanceof SimpleName);
VariableElement fooReceiver = TreeUtil.getVariableElement(expr);
assertNotNull(fooReceiver);
assertEquals("Test.B", fooReceiver.asType().toString());
}
Aggregations