Search in sources :

Example 31 with MethodDeclaration

use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.

the class AnnotationRewriter method createDescriptionMethod.

private MethodDeclaration createDescriptionMethod(TypeElement type, List<AnnotationTypeMemberDeclaration> members, Map<ExecutableElement, VariableElement> fieldElements) {
    ExecutableElement descriptionElement = GeneratedExecutableElement.newMethodWithSelector("description", typeUtil.getJavaString().asType(), type);
    MethodDeclaration descriptionMethod = new MethodDeclaration(descriptionElement);
    descriptionMethod.setHasDeclaration(false);
    Block descriptionBody = new Block();
    descriptionMethod.setBody(descriptionBody);
    StringBuilder description = new StringBuilder();
    StringBuilder fields = new StringBuilder();
    if (!members.isEmpty()) {
        description.append("@\"@" + elementUtil.getBinaryName(type) + "(");
        Iterator<AnnotationTypeMemberDeclaration> iter = members.iterator();
        while (iter.hasNext()) {
            AnnotationTypeMemberDeclaration member = iter.next();
            ExecutableElement memberElement = member.getExecutableElement();
            String propName = NameTable.getAnnotationPropertyName(memberElement);
            String fieldName = nameTable.getVariableShortName(fieldElements.get(memberElement));
            description.append(propName + "=%" + TypeUtil.getObjcFormatSpecifier(memberElement.getReturnType()));
            fields.append(fieldName);
            if (iter.hasNext()) {
                description.append(", ");
                fields.append(", ");
            }
        }
        description.append(")\", " + fields);
        descriptionBody.addStatement(new NativeStatement("return [NSString stringWithFormat:" + description + "];"));
    } else {
        descriptionBody.addStatement(new ReturnStatement(new StringLiteral("@" + elementUtil.getBinaryName(type) + "()", typeUtil)));
    }
    return descriptionMethod;
}
Also used : AnnotationTypeMemberDeclaration(com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) StringLiteral(com.google.devtools.j2objc.ast.StringLiteral) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) Block(com.google.devtools.j2objc.ast.Block)

Example 32 with MethodDeclaration

use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.

the class PackageInfoRewriter method createPrefixMethod.

private MethodDeclaration createPrefixMethod(String prefix, TypeElement type) {
    ExecutableElement element = GeneratedExecutableElement.newMethodWithSelector("__prefix", typeUtil.getJavaString().asType(), type).addModifiers(Modifier.STATIC);
    MethodDeclaration method = new MethodDeclaration(element);
    method.setHasDeclaration(false);
    Block body = new Block();
    method.setBody(body);
    body.addStatement(new ReturnStatement(new StringLiteral(prefix, typeUtil)));
    return method;
}
Also used : StringLiteral(com.google.devtools.j2objc.ast.StringLiteral) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) Block(com.google.devtools.j2objc.ast.Block)

Example 33 with MethodDeclaration

use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.

the class Functionizer method addDisallowedConstructors.

/**
 * Declare any inherited constructors that aren't allowed to be accessed in Java
 * with a NS_UNAVAILABLE macro, so that clang will flag such access from native
 * code as an error.
 */
private void addDisallowedConstructors(TypeDeclaration node) {
    TypeElement typeElement = node.getTypeElement();
    TypeElement superClass = ElementUtil.getSuperclass(typeElement);
    if (ElementUtil.isPrivateInnerType(typeElement) || superClass == null || // superclass is NSObject.
    (!options.emitWrapperMethods() && typeUtil.getObjcClass(superClass) != TypeUtil.NS_OBJECT)) {
        return;
    }
    Map<String, ExecutableElement> inheritedConstructors = new HashMap<>();
    // Add super constructors that have unique parameter lists.
    for (ExecutableElement superC : ElementUtil.getConstructors(superClass)) {
        if (ElementUtil.isPrivate(superC)) {
            // Skip private super constructors since they're already unavailable.
            continue;
        }
        String selector = nameTable.getMethodSelector(superC);
        inheritedConstructors.put(selector, superC);
    }
    // Don't disallow this class' constructors if we're emitting wrapper methods.
    if (options.emitWrapperMethods()) {
        for (ExecutableElement constructor : ElementUtil.getConstructors(typeElement)) {
            inheritedConstructors.remove(nameTable.getMethodSelector(constructor));
        }
    }
    for (Map.Entry<String, ExecutableElement> entry : inheritedConstructors.entrySet()) {
        ExecutableElement oldConstructor = entry.getValue();
        GeneratedExecutableElement newConstructor = GeneratedExecutableElement.newConstructorWithSelector(entry.getKey(), typeElement, typeUtil);
        MethodDeclaration decl = new MethodDeclaration(newConstructor).setUnavailable(true);
        decl.addModifiers(Modifier.ABSTRACT);
        int count = 0;
        for (VariableElement param : oldConstructor.getParameters()) {
            VariableElement newParam = GeneratedVariableElement.newParameter("arg" + count++, param.asType(), newConstructor);
            newConstructor.addParameter(newParam);
            decl.addParameter(new SingleVariableDeclaration(newParam));
        }
        addImplicitParameters(decl, ElementUtil.getDeclaringClass(oldConstructor));
        node.addBodyDeclaration(decl);
    }
}
Also used : GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) VariableElement(javax.lang.model.element.VariableElement) Map(java.util.Map) HashMap(java.util.HashMap)

Example 34 with MethodDeclaration

use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.

the class DeadCodeEliminator method removeDeadMethods.

/**
 * Remove dead methods from a type's body declarations.
 */
private void removeDeadMethods(String clazz, List<BodyDeclaration> declarations) {
    Iterator<BodyDeclaration> declarationsIter = declarations.iterator();
    while (declarationsIter.hasNext()) {
        BodyDeclaration declaration = declarationsIter.next();
        if (declaration instanceof MethodDeclaration) {
            MethodDeclaration method = (MethodDeclaration) declaration;
            ExecutableElement elem = method.getExecutableElement();
            String name = typeUtil.getReferenceName(elem);
            String signature = typeUtil.getReferenceSignature(elem);
            if (deadCodeMap.containsMethod(clazz, name, signature)) {
                if (method.isConstructor()) {
                    deadCodeMap.addConstructorRemovedClass(clazz);
                }
                if (Modifier.isNative(method.getModifiers())) {
                    removeMethodOCNI(method);
                }
                declarationsIter.remove();
            }
        }
    }
}
Also used : MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) ExecutableElement(javax.lang.model.element.ExecutableElement) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration)

Example 35 with MethodDeclaration

use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.

the class DestructorGenerator method getOnDeallocMethodDeclaration.

private static MethodDeclaration getOnDeallocMethodDeclaration(AbstractTypeDeclaration typeDeclaration) {
    List<MethodDeclaration> onDeallocMethodDeclarations = ImmutableList.copyOf(Iterables.filter(Iterables.transform(typeDeclaration.getBodyDeclarations(), bodyDeclaration -> {
        if (!(bodyDeclaration instanceof MethodDeclaration)) {
            return null;
        }
        MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclaration;
        ExecutableElement executableElement = methodDeclaration.getExecutableElement();
        if (!ElementUtil.hasAnnotation(executableElement, OnDealloc.class)) {
            return null;
        }
        if (!executableElement.getModifiers().contains(Modifier.PRIVATE)) {
            ErrorUtil.error(methodDeclaration, "@OnDealloc method must be private.");
            return null;
        }
        if (executableElement.getModifiers().contains(Modifier.STATIC)) {
            ErrorUtil.error(methodDeclaration, "@OnDealloc method must be non-static.");
            return null;
        }
        if (!TypeUtil.isVoid(executableElement.getReturnType())) {
            ErrorUtil.error(methodDeclaration, "@OnDealloc method must return void.");
            return null;
        }
        if (!executableElement.getParameters().isEmpty()) {
            ErrorUtil.error(methodDeclaration, "@OnDealloc method must have no parameters.");
            return null;
        }
        return methodDeclaration;
    }), Predicates.notNull()));
    int size = onDeallocMethodDeclarations.size();
    if (size > 1) {
        ErrorUtil.error("There can be at most one @OnDealloc method.");
        return null;
    }
    return size == 0 ? null : onDeallocMethodDeclarations.get(0);
}
Also used : MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) OnDealloc(com.google.j2objc.annotations.OnDealloc)

Aggregations

MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)41 ExecutableElement (javax.lang.model.element.ExecutableElement)23 Block (com.google.devtools.j2objc.ast.Block)20 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)19 TypeElement (javax.lang.model.element.TypeElement)12 SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)11 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)10 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)9 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)8 NativeStatement (com.google.devtools.j2objc.ast.NativeStatement)8 VariableElement (javax.lang.model.element.VariableElement)8 TypeMirror (javax.lang.model.type.TypeMirror)7 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)6 TreeVisitor (com.google.devtools.j2objc.ast.TreeVisitor)5 ExecutablePair (com.google.devtools.j2objc.types.ExecutablePair)5 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)4 MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)4 SimpleName (com.google.devtools.j2objc.ast.SimpleName)4 Statement (com.google.devtools.j2objc.ast.Statement)4 GeneratedTypeElement (com.google.devtools.j2objc.types.GeneratedTypeElement)4