use of com.google.devtools.j2objc.ast.Expression in project j2objc by google.
the class Rewriter method rewriteStringConcat.
private void rewriteStringConcat(InfixExpression node) {
// Collect all non-string operands that precede the first string operand.
// If there are multiple such operands, move them into a sub-expression.
List<Expression> nonStringOperands = new ArrayList<>();
TypeMirror nonStringExprType = null;
for (Expression operand : node.getOperands()) {
TypeMirror operandType = operand.getTypeMirror();
if (typeUtil.isString(operandType)) {
break;
}
nonStringOperands.add(operand);
nonStringExprType = getAdditionType(nonStringExprType, operandType);
}
if (nonStringOperands.size() < 2) {
return;
}
InfixExpression nonStringExpr = new InfixExpression(nonStringExprType, InfixExpression.Operator.PLUS);
for (Expression operand : nonStringOperands) {
nonStringExpr.addOperand(TreeUtil.remove(operand));
}
node.addOperand(0, nonStringExpr);
}
use of com.google.devtools.j2objc.ast.Expression 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.Expression 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.Expression 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());
}
use of com.google.devtools.j2objc.ast.Expression 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));
}
Aggregations