Search in sources :

Example 1 with ParameterDeclaration

use of com.strobel.decompiler.languages.java.ast.ParameterDeclaration 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

MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)1 SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)1 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)1 EntityDeclaration (com.strobel.decompiler.languages.java.ast.EntityDeclaration)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