Search in sources :

Example 1 with EntityDeclaration

use of com.strobel.decompiler.languages.java.ast.EntityDeclaration in project j2objc by google.

the class ClassFileConverter method convertClassInitializer.

/**
 * The clinit method isn't converted into a member element by javac,
 * so extract it separately from the classfile.
 */
private void convertClassInitializer(AbstractTypeDeclaration typeDecl) {
    EntityDeclaration decl = classFile.getMethod("<clinit>", "()V");
    if (decl == null) {
        // Class doesn't have a static initializer.
        return;
    }
    MethodTranslator translator = new MethodTranslator(parserEnv, translationEnv, null, typeDecl, null);
    Block block = (Block) decl.acceptVisitor(translator, null);
    for (Statement stmt : block.getStatements()) {
        typeDecl.addClassInitStatement(stmt.copy());
    }
}
Also used : EntityDeclaration(com.strobel.decompiler.languages.java.ast.EntityDeclaration) Statement(com.google.devtools.j2objc.ast.Statement) Block(com.google.devtools.j2objc.ast.Block)

Example 2 with EntityDeclaration

use of com.strobel.decompiler.languages.java.ast.EntityDeclaration in project j2objc by google.

the class ClassFileConverter method convertMethodDeclaration.

private TreeNode convertMethodDeclaration(ExecutableElement element, AbstractTypeDeclaration node) {
    MethodDeclaration methodDecl = new MethodDeclaration(element);
    convertBodyDeclaration(methodDecl, element);
    HashMap<String, VariableElement> localVariableTable = new HashMap<>();
    List<SingleVariableDeclaration> parameters = methodDecl.getParameters();
    String name = element.getSimpleName().toString();
    String descriptor = getMethodDescriptor(element);
    if (element.getParameters().size() > 0) {
        Iterator<ParameterDeclaration> paramsIterator = methodDecl.isConstructor() ? classFile.getConstructor(descriptor).getParameters().iterator() : classFile.getMethod(name, descriptor).getParameters().iterator();
        // to work around potential javac8 bug iterating over parameter names.
        for (VariableElement param : element.getParameters()) {
            SingleVariableDeclaration varDecl = (SingleVariableDeclaration) convert(param, methodDecl);
            String nameDef = paramsIterator.next().getName();
            // If element's name doesn't match the ParameterNode's name, use the latter.
            if (!nameDef.equals(param.getSimpleName().toString())) {
                param = GeneratedVariableElement.newParameter(nameDef, param.asType(), param.getEnclosingElement());
                varDecl.setVariableElement(param);
            }
            parameters.add(varDecl);
            localVariableTable.put(param.getSimpleName().toString(), param);
        }
    }
    if (element.isVarArgs()) {
        SingleVariableDeclaration lastParam = parameters.get(parameters.size() - 1);
        TypeMirror paramType = lastParam.getType().getTypeMirror();
        assert paramType.getKind() == TypeKind.ARRAY;
        TypeMirror varArgType = ((ArrayType) paramType).getComponentType();
        lastParam.setType(Type.newType(varArgType));
        lastParam.setIsVarargs(true);
    }
    if (!ElementUtil.isAbstract(element)) {
        EntityDeclaration decl = methodDecl.isConstructor() ? classFile.getConstructor(descriptor) : classFile.getMethod(name, descriptor);
        MethodTranslator translator = new MethodTranslator(parserEnv, translationEnv, element, node, localVariableTable);
        methodDecl.setBody((Block) decl.acceptVisitor(translator, null));
    }
    return methodDecl;
}
Also used : HashMap(java.util.HashMap) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) EntityDeclaration(com.strobel.decompiler.languages.java.ast.EntityDeclaration) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) VariableElement(javax.lang.model.element.VariableElement) ArrayType(javax.lang.model.type.ArrayType) TypeMirror(javax.lang.model.type.TypeMirror) ParameterDeclaration(com.strobel.decompiler.languages.java.ast.ParameterDeclaration)

Aggregations

EntityDeclaration (com.strobel.decompiler.languages.java.ast.EntityDeclaration)2 Block (com.google.devtools.j2objc.ast.Block)1 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)1 SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)1 Statement (com.google.devtools.j2objc.ast.Statement)1 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)1 ParameterDeclaration (com.strobel.decompiler.languages.java.ast.ParameterDeclaration)1 HashMap (java.util.HashMap)1 VariableElement (javax.lang.model.element.VariableElement)1 ArrayType (javax.lang.model.type.ArrayType)1 TypeMirror (javax.lang.model.type.TypeMirror)1