Search in sources :

Example 81 with ExecutableElement

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

the class EnumRewriter method visit.

@Override
public boolean visit(MethodDeclaration node) {
    ExecutableElement element = node.getExecutableElement();
    TypeElement declaringClass = ElementUtil.getDeclaringClass(element);
    if (!ElementUtil.isConstructor(element) || !ElementUtil.isEnum(declaringClass)) {
        return false;
    }
    node.removeModifiers(Modifier.PUBLIC | Modifier.PROTECTED);
    node.addModifiers(Modifier.PRIVATE);
    return true;
}
Also used : GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 82 with ExecutableElement

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

the class EnumRewriter method addValuesMethod.

private void addValuesMethod(EnumDeclaration node) {
    TypeElement type = node.getTypeElement();
    ExecutableElement method = ElementUtil.findMethod(type, "values");
    assert method != null : "Can't find values method on enum type.";
    String typeName = nameTable.getFullName(type);
    MethodDeclaration methodDecl = new MethodDeclaration(method);
    Block body = new Block();
    methodDecl.setBody(body);
    body.addStatement(new NativeStatement(UnicodeUtils.format("  return [IOSObjectArray arrayWithObjects:%s_values_ count:%s type:%s_class_()];", typeName, node.getEnumConstants().size(), typeName)));
    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) ExecutableElement(javax.lang.model.element.ExecutableElement) Block(com.google.devtools.j2objc.ast.Block)

Example 83 with ExecutableElement

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

the class EnumRewriter method isSimpleEnum.

/**
   * Returns true if an enum doesn't have custom or renamed constructors,
   * vararg constructors or constants with anonymous class extensions.
   */
private boolean isSimpleEnum(EnumDeclaration node) {
    TypeElement type = node.getTypeElement();
    for (EnumConstantDeclaration constant : node.getEnumConstants()) {
        ExecutableElement method = constant.getExecutableElement();
        if (method.getParameters().size() > 0 || method.isVarArgs()) {
            return false;
        }
        if (ElementUtil.hasAnnotation(method, ObjectiveCName.class)) {
            return false;
        }
        TypeElement valueType = ElementUtil.getDeclaringClass(method);
        if (valueType != type) {
            return false;
        }
    }
    return true;
}
Also used : EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 84 with ExecutableElement

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

the class EnumRewriter method addNonArcInitialization.

private void addNonArcInitialization(EnumDeclaration node) {
    TypeElement type = node.getTypeElement();
    int baseTypeCount = 0;
    List<Statement> sizeStatements = new ArrayList<>();
    List<Statement> initStatements = new ArrayList<>();
    TypeMirror voidType = typeUtil.getVoid();
    VariableElement localEnum = GeneratedVariableElement.newLocalVar("e", TypeUtil.ID_TYPE, null);
    int i = 0;
    for (EnumConstantDeclaration constant : node.getEnumConstants()) {
        VariableElement varElement = constant.getVariableElement();
        String varName = ElementUtil.getName(varElement);
        ExecutableElement methodElement = constant.getExecutableElement();
        TypeElement valueType = ElementUtil.getDeclaringClass(methodElement);
        boolean isAnonymous = valueType != type;
        String classExpr = isAnonymous ? "[" + nameTable.getFullName(valueType) + " class]" : "self";
        String sizeName = "objSize" + (isAnonymous ? "_" + varName : "");
        if (isAnonymous) {
            sizeStatements.add(new NativeStatement(UnicodeUtils.format("size_t %s = class_getInstanceSize(%s);", sizeName, classExpr)));
            sizeStatements.add(new NativeStatement(UnicodeUtils.format("allocSize += %s;", sizeName)));
        } else {
            baseTypeCount++;
        }
        initStatements.add(new ExpressionStatement(new CommaExpression(new Assignment(new SimpleName(varElement), new Assignment(new SimpleName(localEnum), new NativeExpression(UnicodeUtils.format("objc_constructInstance(%s, (void *)ptr)", classExpr), type.asType()))), new NativeExpression("ptr += " + sizeName, voidType))));
        String initName = nameTable.getFullFunctionName(methodElement);
        FunctionElement initElement = new FunctionElement(initName, voidType, valueType).addParameters(valueType.asType()).addParameters(ElementUtil.asTypes(methodElement.getParameters()));
        FunctionInvocation initFunc = new FunctionInvocation(initElement, voidType);
        initFunc.addArgument(new SimpleName(localEnum));
        TreeUtil.copyList(constant.getArguments(), initFunc.getArguments());
        initFunc.addArgument(new StringLiteral(varName, typeUtil));
        initFunc.addArgument(new NumberLiteral(i++, typeUtil));
        initStatements.add(new ExpressionStatement(initFunc));
    }
    List<Statement> stmts = node.getClassInitStatements().subList(0, 0);
    if (baseTypeCount == 0) {
        stmts.add(new NativeStatement("size_t allocSize = 0;"));
    } else {
        stmts.add(new NativeStatement("size_t objSize = class_getInstanceSize(self);"));
        stmts.add(new NativeStatement(UnicodeUtils.format("size_t allocSize = %s * objSize;", baseTypeCount)));
    }
    stmts.addAll(sizeStatements);
    stmts.add(new NativeStatement("uintptr_t ptr = (uintptr_t)calloc(allocSize, 1);"));
    stmts.add(new VariableDeclarationStatement(localEnum, null));
    stmts.addAll(initStatements);
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) Assignment(com.google.devtools.j2objc.ast.Assignment) EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) StringLiteral(com.google.devtools.j2objc.ast.StringLiteral) TypeMirror(javax.lang.model.type.TypeMirror) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) NumberLiteral(com.google.devtools.j2objc.ast.NumberLiteral)

Example 85 with ExecutableElement

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

the class Functionizer method canFunctionize.

/**
   * Determines whether an instance method can be functionized.
   */
private boolean canFunctionize(MethodDeclaration node) {
    ExecutableElement m = node.getExecutableElement();
    int modifiers = node.getModifiers();
    // Never functionize these types of methods.
    if (Modifier.isStatic(modifiers) || Modifier.isAbstract(modifiers) || !node.hasDeclaration() || ElementUtil.isAnnotationMember(m)) {
        return false;
    }
    // Don't functionize equals/hash, since they are often called by collections.
    String name = ElementUtil.getName(m);
    if ((name.equals("hashCode") && m.getParameters().isEmpty()) || (name.equals("equals") && m.getParameters().size() == 1)) {
        return false;
    }
    if (!ElementUtil.isPrivate(m) && !ElementUtil.isFinal(m)) {
        return false;
    }
    return !hasSuperMethodInvocation(node);
}
Also used : GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

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