use of com.google.devtools.j2objc.ast.MethodInvocation 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.MethodInvocation in project j2objc by google.
the class JavaToIOSMethodTranslator method addCopyWithZoneMethod.
private void addCopyWithZoneMethod(TypeDeclaration node) {
// Create copyWithZone: method.
GeneratedExecutableElement copyElement = GeneratedExecutableElement.newMethodWithSelector("copyWithZone:", TypeUtil.ID_TYPE, node.getTypeElement());
MethodDeclaration copyDecl = new MethodDeclaration(copyElement);
copyDecl.setHasDeclaration(false);
// Add NSZone *zone parameter.
VariableElement zoneParam = GeneratedVariableElement.newParameter("zone", NSZONE_TYPE, copyElement);
copyElement.addParameter(zoneParam);
copyDecl.addParameter(new SingleVariableDeclaration(zoneParam));
Block block = new Block();
copyDecl.setBody(block);
ExecutableElement cloneElement = ElementUtil.findMethod(typeUtil.getJavaObject(), "clone");
MethodInvocation invocation = new MethodInvocation(new ExecutablePair(cloneElement), null);
if (options.useReferenceCounting()) {
invocation = new MethodInvocation(new ExecutablePair(RETAIN_METHOD), invocation);
}
block.addStatement(new ReturnStatement(invocation));
node.addBodyDeclaration(copyDecl);
}
use of com.google.devtools.j2objc.ast.MethodInvocation 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.MethodInvocation in project j2objc by google.
the class LogSiteInjector method injectEnclosingClass.
private MethodInvocation injectEnclosingClass(MethodInvocation node, TypeElement cls) {
ExecutableElement injectedMethod = ElementUtil.findMethod(cls, "forInjectedClassName", "java.lang.String");
MethodInvocation injectedInvocation = new MethodInvocation(new ExecutablePair(injectedMethod), node.getExpression().copy());
injectedInvocation.addArgument(enclosingClassLiteral(node));
return injectedInvocation;
}
use of com.google.devtools.j2objc.ast.MethodInvocation in project j2objc by google.
the class LogSiteInjector method endVisit.
@Override
public void endVisit(MethodInvocation node) {
if (node.getExpression() == null) {
// Local and statically imported methods are never injected.
return;
}
ExecutableElement method = node.getExecutableElement();
String methodName = ElementUtil.getName(method);
TypeElement cls = ElementUtil.getDeclaringClass(method);
if (methodName.equals("forEnclosingClass") && isLoggingSubtype(cls, googleLoggerClass)) {
node.replaceWith(injectEnclosingClass(node, cls));
return;
}
if (methodName.equals(LOG_METHOD) && isLoggingSubtype(cls, javaUtilLoggingLoggerClass)) {
node.replaceWith(injectLogMethod(node));
return;
}
if ((methodName.equals(LOG_METHOD) || methodName.equals(LOG_VARARGS_METHOD)) && isLoggingSubtype(cls, loggingApiClass)) {
Expression methodExpr = node.getExpression();
// Check that injectedLogSite() wasn't already injected or in original source.
if (methodExpr.getKind() == TreeNode.Kind.METHOD_INVOCATION && !ElementUtil.getName(((MethodInvocation) methodExpr).getExecutableElement()).equals(WITH_INJECTED_LOG_SITE_METHOD)) {
node.replaceWith(injectLogSite(node));
}
return;
}
if (CONVENIENCE_METHODS.contains(methodName) && isLoggingSubtype(cls, javaUtilLoggingLoggerClass)) {
node.replaceWith(injectConvenienceMethod(methodName, node));
return;
}
}
Aggregations