use of com.google.devtools.j2objc.ast.SimpleName 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 com.google.devtools.j2objc.ast.SimpleName in project j2objc by google.
the class TreeConverter method convertExpressionMethodReference.
private static TreeNode convertExpressionMethodReference(org.eclipse.jdt.core.dom.ExpressionMethodReference node) {
IMethodBinding methodBinding = node.resolveMethodBinding();
ExpressionMethodReference newNode = new ExpressionMethodReference();
Expression expression = (Expression) convert(node.getExpression());
boolean consumesFirstParam = !BindingUtil.isStatic(methodBinding) && expression instanceof Name && !ElementUtil.isVariable(((Name) expression).getElement());
convertMethodReference(node, newNode, methodBinding, consumesFirstParam);
return newNode.setName((SimpleName) TreeConverter.convert(node.getName())).setExpression(expression);
}
use of com.google.devtools.j2objc.ast.SimpleName in project j2objc by google.
the class TreeConverter method convertTypeMethodReference.
private static TreeNode convertTypeMethodReference(org.eclipse.jdt.core.dom.TypeMethodReference node) {
TypeMethodReference newNode = new TypeMethodReference();
IMethodBinding methodBinding = node.resolveMethodBinding();
if (node.getType().resolveBinding().isArray()) {
// JDT does not provide the correct method on array types, so we find it from
// java.lang.Object.
IMethodBinding functionalInterface = getFunctionalInterface(node.resolveTypeBinding());
methodBinding = getObjectMethod(node.getAST(), node.getName().getIdentifier(), functionalInterface.getParameterTypes().length - 1);
}
convertMethodReference(node, newNode, methodBinding, !BindingUtil.isStatic(methodBinding));
return newNode.setName((SimpleName) TreeConverter.convert(node.getName())).setType((Type) TreeConverter.convert(node.getType()));
}
use of com.google.devtools.j2objc.ast.SimpleName 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));
}
}
use of com.google.devtools.j2objc.ast.SimpleName in project j2objc by google.
the class TreeConverter method convertSimpleName.
private static TreeNode convertSimpleName(org.eclipse.jdt.core.dom.SimpleName node) {
SimpleName newNode = new SimpleName();
convertName(node, newNode);
boolean isConstructor = node.resolveBinding() instanceof IMethodBinding && ((IMethodBinding) node.resolveBinding()).isConstructor();
return newNode.setIdentifier(isConstructor ? "<init>" : node.getIdentifier()).setTypeMirror(BindingConverter.getType(node.resolveTypeBinding()));
}
Aggregations