Search in sources :

Example 76 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project j2objc by google.

the class ElementReferenceMapper method endVisit.

@Override
public void endVisit(ConstructorInvocation invocation) {
    ExecutableElement childMethodElement = invocation.getExecutableElement();
    handleChildMethod(childMethodElement);
    MethodDeclaration parentMethodDeclaration = TreeUtil.getEnclosingMethod(invocation);
    if (parentMethodDeclaration == null) {
        staticSet.add(stitchMethodIdentifier(childMethodElement));
        return;
    }
    ExecutableElement parentMethodElement = parentMethodDeclaration.getExecutableElement();
    handleParentMethod(parentMethodElement, childMethodElement);
}
Also used : MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 77 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project j2objc by google.

the class ElementReferenceMapper method endVisit.

@Override
public void endVisit(MethodInvocation method) {
    ExecutableElement childMethodElement = method.getExecutableElement();
    handleChildMethod(childMethodElement);
    MethodDeclaration parentMethodDeclaration = TreeUtil.getEnclosingMethod(method);
    if (parentMethodDeclaration == null) {
        staticSet.add(stitchMethodIdentifier(childMethodElement));
        return;
    }
    ExecutableElement parentMethodElement = parentMethodDeclaration.getExecutableElement();
    handleParentMethod(parentMethodElement, childMethodElement);
}
Also used : MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 78 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project j2objc by google.

the class UnusedCodeTracker method markUsedElements.

/**
   * Add to root set, methods from CodeReferenceMap and also all public methods in input classes.
   * Then, do tree shaker traversal starting from this root set.
   * @param publicRootSet: CodeReferenceMap with public root methods and classes.
   */
//TODO(malvania): Current paradigm: All methods in input CodeReferenceMap are assumed to be 
//  public roots to traverse from.
//Classes in input CodeReferenceMap here allow user to add Dynamically Loaded Classes and keep
//  their public methods in the public root set.
//In the future, when we add support for libraries, we will want to include protected methods
//  of those library classes as well, so we should add "|| ElementUtil.isProtected(method)" after
//  the isPublic check.
public void markUsedElements(CodeReferenceMap publicRootSet) {
    if (publicRootSet == null) {
        markUsedElements();
        return;
    }
    //Add all public methods in publicRootClasses to root set
    for (String clazz : publicRootSet.getReferencedClasses()) {
        ClassReferenceNode classNode = (ClassReferenceNode) elementReferenceMap.get(ElementReferenceMapper.stitchClassIdentifier(clazz));
        assert (classNode != null);
        Iterable<ExecutableElement> methods = ElementUtil.getMethods(classNode.classElement);
        for (ExecutableElement method : methods) {
            if (ElementUtil.isPublic(method)) {
                rootSet.add(ElementReferenceMapper.stitchMethodIdentifier(method, env.typeUtil(), env.elementUtil()));
            }
        }
    }
    //Add input root methods to static set
    for (Table.Cell<String, String, ImmutableSet<String>> cell : publicRootSet.getReferencedMethods().cellSet()) {
        String clazzName = cell.getRowKey();
        String methodName = cell.getColumnKey();
        for (String signature : cell.getValue()) {
            rootSet.add(ElementReferenceMapper.stitchMethodIdentifier(clazzName, methodName, signature));
        }
    }
    markUsedElements(staticSet);
    markUsedElements(rootSet);
}
Also used : Table(com.google.common.collect.Table) ImmutableSet(com.google.common.collect.ImmutableSet) ExecutableElement(javax.lang.model.element.ExecutableElement) ClassReferenceNode(com.google.devtools.treeshaker.ElementReferenceMapper.ClassReferenceNode)

Example 79 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project j2objc by google.

the class NameTableTest method testTypeVariableWithTypeVariableBounds.

public void testTypeVariableWithTypeVariableBounds() {
    String source = "class A<T> { <E extends T> void foo(E e) {} }";
    CompilationUnit unit = translateType("A", source);
    NameTable nameTable = unit.getEnv().nameTable();
    final ExecutableElement[] methodElement = new ExecutableElement[1];
    unit.accept(new TreeVisitor() {

        @Override
        public void endVisit(MethodDeclaration node) {
            ExecutableElement element = node.getExecutableElement();
            if (ElementUtil.getName(element).equals("foo")) {
                methodElement[0] = element;
            }
        }
    });
    assertNotNull(methodElement[0]);
    TypeMirror paramType = methodElement[0].getParameters().get(0).asType();
    assertEquals("id", nameTable.getObjCType(paramType));
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) TreeVisitor(com.google.devtools.j2objc.ast.TreeVisitor) TypeMirror(javax.lang.model.type.TypeMirror) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 80 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project j2objc by google.

the class EnumRewriter method addValueOfMethod.

private void addValueOfMethod(EnumDeclaration node) {
    TypeElement type = node.getTypeElement();
    ExecutableElement method = ElementUtil.findMethod(type, "valueOf", "java.lang.String");
    assert method != null : "Can't find valueOf method on enum type.";
    String typeName = nameTable.getFullName(type);
    int numConstants = node.getEnumConstants().size();
    VariableElement nameParam = GeneratedVariableElement.newParameter("name", method.getParameters().get(0).asType(), method);
    MethodDeclaration methodDecl = new MethodDeclaration(method);
    methodDecl.addParameter(new SingleVariableDeclaration(nameParam));
    Block body = new Block();
    methodDecl.setBody(body);
    StringBuilder impl = new StringBuilder();
    if (numConstants > 0) {
        impl.append(UnicodeUtils.format("  for (int i = 0; i < %s; i++) {\n" + "    %s *e = %s_values_[i];\n" + "    if ([name isEqual:[e name]]) {\n" + "      return e;\n" + "    }\n" + "  }\n", numConstants, typeName, typeName));
    }
    impl.append("  @throw create_JavaLangIllegalArgumentException_initWithNSString_(name);\n" + "  return nil;");
    body.addStatement(new NativeStatement(impl.toString()));
    node.addBodyDeclaration(methodDecl);
}
Also used : NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) ExecutableElement(javax.lang.model.element.ExecutableElement) Block(com.google.devtools.j2objc.ast.Block) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Aggregations

ExecutableElement (javax.lang.model.element.ExecutableElement)345 TypeElement (javax.lang.model.element.TypeElement)158 TypeMirror (javax.lang.model.type.TypeMirror)97 VariableElement (javax.lang.model.element.VariableElement)85 Element (javax.lang.model.element.Element)72 Test (org.junit.Test)41 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)32 DeclaredType (javax.lang.model.type.DeclaredType)31 ArrayList (java.util.ArrayList)26 JBlock (com.helger.jcodemodel.JBlock)25 JInvocation (com.helger.jcodemodel.JInvocation)20 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)18 JVar (com.helger.jcodemodel.JVar)18 Map (java.util.Map)18 HashSet (java.util.HashSet)17 IJExpression (com.helger.jcodemodel.IJExpression)16 ElementValidation (org.androidannotations.ElementValidation)15 Block (com.google.devtools.j2objc.ast.Block)13 JMethod (com.helger.jcodemodel.JMethod)13 Elements (javax.lang.model.util.Elements)13