Search in sources :

Example 6 with Modifier

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

the class ElementUtil method toModifierSet.

public static Set<Modifier> toModifierSet(int modifiers) {
    Set<Modifier> set = modifierSets.get(modifiers);
    if (set == null) {
        set = EnumSet.noneOf(Modifier.class);
        if ((modifiers & java.lang.reflect.Modifier.PUBLIC) > 0) {
            set.add(Modifier.PUBLIC);
        }
        if ((modifiers & java.lang.reflect.Modifier.PRIVATE) > 0) {
            set.add(Modifier.PRIVATE);
        }
        if ((modifiers & java.lang.reflect.Modifier.PROTECTED) > 0) {
            set.add(Modifier.PROTECTED);
        }
        if ((modifiers & java.lang.reflect.Modifier.STATIC) > 0) {
            set.add(Modifier.STATIC);
        }
        if ((modifiers & java.lang.reflect.Modifier.FINAL) > 0) {
            set.add(Modifier.FINAL);
        }
        if ((modifiers & java.lang.reflect.Modifier.SYNCHRONIZED) > 0) {
            set.add(Modifier.SYNCHRONIZED);
        }
        if ((modifiers & java.lang.reflect.Modifier.VOLATILE) > 0) {
            set.add(Modifier.VOLATILE);
        }
        if ((modifiers & java.lang.reflect.Modifier.TRANSIENT) > 0) {
            set.add(Modifier.TRANSIENT);
        }
        if ((modifiers & java.lang.reflect.Modifier.NATIVE) > 0) {
            set.add(Modifier.NATIVE);
        }
        if ((modifiers & java.lang.reflect.Modifier.ABSTRACT) > 0) {
            set.add(Modifier.ABSTRACT);
        }
        if ((modifiers & java.lang.reflect.Modifier.STRICT) > 0) {
            set.add(Modifier.STRICTFP);
        }
        // added in Java 8.
        if ((modifiers & org.eclipse.jdt.core.dom.Modifier.DEFAULT) > 0) {
            try {
                Modifier m = Modifier.valueOf("DEFAULT");
                set.add(m);
            } catch (IllegalArgumentException e) {
            // Can only add DEFAULT modifier in Java 8.
            }
        }
        modifierSets.put(modifiers, set);
    }
    return set;
}
Also used : Modifier(javax.lang.model.element.Modifier)

Example 7 with Modifier

use of javax.lang.model.element.Modifier in project error-prone by google.

the class UseBinds method matchMethod.

@Override
public Description matchMethod(MethodTree method, VisitorState state) {
    if (!CAN_BE_A_BINDS_METHOD.matches(method, state)) {
        return NO_MATCH;
    }
    JCClassDecl enclosingClass = ASTHelpers.findEnclosingNode(state.getPath(), JCClassDecl.class);
    // Dagger 1 modules don't support @Binds.
    if (!IS_DAGGER_2_MODULE.matches(enclosingClass, state)) {
        return NO_MATCH;
    }
    if (enclosingClass.getExtendsClause() != null) {
        return fixByDelegating();
    }
    for (Tree member : enclosingClass.getMembers()) {
        if (member.getKind().equals(Tree.Kind.METHOD) && !getSymbol(member).isConstructor()) {
            MethodTree siblingMethod = (MethodTree) member;
            Set<Modifier> siblingFlags = siblingMethod.getModifiers().getFlags();
            if (!(siblingFlags.contains(Modifier.STATIC) || siblingFlags.contains(Modifier.ABSTRACT)) && !CAN_BE_A_BINDS_METHOD.matches(siblingMethod, state)) {
                return fixByDelegating();
            }
        }
    }
    return fixByModifyingMethod(state, enclosingClass, method);
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) MethodTree(com.sun.source.tree.MethodTree) ReturnTree(com.sun.source.tree.ReturnTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) Tree(com.sun.source.tree.Tree) BlockTree(com.sun.source.tree.BlockTree) StatementTree(com.sun.source.tree.StatementTree) Modifier(javax.lang.model.element.Modifier)

Example 8 with Modifier

use of javax.lang.model.element.Modifier in project squidb by yahoo.

the class ModelMethodPlugin method checkExecutableElement.

private void checkExecutableElement(ExecutableElement e, List<ExecutableElement> modelMethods, List<ExecutableElement> staticModelMethods, DeclaredTypeName modelClass) {
    Set<Modifier> modifiers = e.getModifiers();
    if (e.getKind() == ElementKind.CONSTRUCTOR) {
        // Don't copy constructors
        return;
    }
    if (!modifiers.contains(Modifier.STATIC)) {
        utils.getMessager().printMessage(Diagnostic.Kind.WARNING, "Model spec objects should never be instantiated, so non-static methods are meaningless. " + "Did you mean to make this a static method?", e);
        return;
    }
    ModelMethod methodAnnotation = e.getAnnotation(ModelMethod.class);
    // Don't assume error if method is private
    if (methodAnnotation == null) {
        if (modifiers.contains(Modifier.PUBLIC)) {
            staticModelMethods.add(e);
        } else if (!modifiers.contains(Modifier.PRIVATE)) {
            utils.getMessager().printMessage(Diagnostic.Kind.WARNING, "This method will not be added to the model definition. " + "Did you mean to annotate this method with @ModelMethod?", e);
        }
    } else {
        List<? extends VariableElement> params = e.getParameters();
        if (params.size() == 0) {
            modelSpec.logError("@ModelMethod methods must have an abstract model as their first argument", e);
        } else {
            VariableElement firstParam = params.get(0);
            TypeMirror paramType = firstParam.asType();
            if (!checkFirstArgType(paramType, modelClass)) {
                modelSpec.logError("@ModelMethod methods must have an abstract model as their first argument", e);
            } else {
                modelMethods.add(e);
            }
        }
    }
}
Also used : ModelMethod(com.yahoo.squidb.annotations.ModelMethod) TypeMirror(javax.lang.model.type.TypeMirror) VariableElement(javax.lang.model.element.VariableElement) Modifier(javax.lang.model.element.Modifier)

Example 9 with Modifier

use of javax.lang.model.element.Modifier in project butterknife by JakeWharton.

the class ButterKnifeProcessor method isInaccessibleViaGeneratedCode.

private boolean isInaccessibleViaGeneratedCode(Class<? extends Annotation> annotationClass, String targetThing, Element element) {
    boolean hasError = false;
    TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
    // Verify method modifiers.
    Set<Modifier> modifiers = element.getModifiers();
    if (modifiers.contains(PRIVATE) || modifiers.contains(STATIC)) {
        error(element, "@%s %s must not be private or static. (%s.%s)", annotationClass.getSimpleName(), targetThing, enclosingElement.getQualifiedName(), element.getSimpleName());
        hasError = true;
    }
    // Verify containing type.
    if (enclosingElement.getKind() != CLASS) {
        error(enclosingElement, "@%s %s may only be contained in classes. (%s.%s)", annotationClass.getSimpleName(), targetThing, enclosingElement.getQualifiedName(), element.getSimpleName());
        hasError = true;
    }
    // Verify containing class visibility is not private.
    if (enclosingElement.getModifiers().contains(PRIVATE)) {
        error(enclosingElement, "@%s %s may not be contained in private classes. (%s.%s)", annotationClass.getSimpleName(), targetThing, enclosingElement.getQualifiedName(), element.getSimpleName());
        hasError = true;
    }
    return hasError;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Modifier(javax.lang.model.element.Modifier)

Example 10 with Modifier

use of javax.lang.model.element.Modifier in project LoganSquare by bluelinelabs.

the class JsonObjectProcessor method addAllNonPrivateAccessors.

private void addAllNonPrivateAccessors(Element element, Elements elements, Types types, JsonObjectHolder objectHolder) {
    List<? extends Element> enclosedElements = element.getEnclosedElements();
    for (Element enclosedElement : enclosedElements) {
        ElementKind enclosedElementKind = enclosedElement.getKind();
        if (enclosedElementKind == ElementKind.FIELD) {
            Set<Modifier> modifiers = enclosedElement.getModifiers();
            if (modifiers.contains(Modifier.PRIVATE) && !modifiers.contains(Modifier.TRANSIENT) && !modifiers.contains(Modifier.STATIC)) {
                String getter = JsonFieldHolder.getGetter(enclosedElement, elements);
                String setter = JsonFieldHolder.getSetter(enclosedElement, elements);
                if (!TextUtils.isEmpty(getter) && !TextUtils.isEmpty(setter)) {
                    createOrUpdateFieldHolder(enclosedElement, elements, types, objectHolder);
                }
            }
        }
    }
}
Also used : ElementKind(javax.lang.model.element.ElementKind) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeParameterElement(javax.lang.model.element.TypeParameterElement) Modifier(javax.lang.model.element.Modifier)

Aggregations

Modifier (javax.lang.model.element.Modifier)21 TypeMirror (javax.lang.model.type.TypeMirror)7 TypeElement (javax.lang.model.element.TypeElement)6 Tree (com.sun.source.tree.Tree)5 AnnotationTree (com.sun.source.tree.AnnotationTree)4 ArrayList (java.util.ArrayList)4 Element (javax.lang.model.element.Element)4 ElementKind (javax.lang.model.element.ElementKind)4 ImmutableList (com.google.common.collect.ImmutableList)3 MethodTree (com.sun.source.tree.MethodTree)3 ModifiersTree (com.sun.source.tree.ModifiersTree)3 VariableTree (com.sun.source.tree.VariableTree)3 Nullable (javax.annotation.Nullable)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 Function (com.google.common.base.Function)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Sets (com.google.common.collect.Sets)2 VisitorState (com.google.errorprone.VisitorState)2 Builder (com.google.errorprone.fixes.SuggestedFix.Builder)2 ErrorProneToken (com.google.errorprone.util.ErrorProneToken)2