Java Code Examples for javax.lang.model.element.TypeElement.getModifiers()

The following are Jave code examples for showing how to use getModifiers() of the javax.lang.model.element.TypeElement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: GitHub   File: EventBusAnnotationProcessor.java   Source Code and License Vote up 6 votes
private boolean isVisible(String myPackage, TypeElement typeElement) {
    Set<Modifier> modifiers = typeElement.getModifiers();
    boolean visible;
    if (modifiers.contains(Modifier.PUBLIC)) {
        visible = true;
    } else if (modifiers.contains(Modifier.PRIVATE) || modifiers.contains(Modifier.PROTECTED)) {
        visible = false;
    } else {
        String subscriberPackage = getPackageElement(typeElement).getQualifiedName().toString();
        if (myPackage == null) {
            visible = subscriberPackage.length() == 0;
        } else {
            visible = myPackage.equals(subscriberPackage);
        }
    }
    return visible;
}
 
Example 2
Project: incubator-netbeans   File: BeanInstaller.java   Source Code and License Vote up 6 votes
public static String isDeclaredAsJavaBean(TypeElement clazz) {
    if (ElementKind.CLASS != clazz.getKind()) {
        return PaletteUtils.getBundleString("MSG_notAClass"); // NOI18N
    }

    Set<javax.lang.model.element.Modifier> mods = clazz.getModifiers();
    if (mods.contains(javax.lang.model.element.Modifier.ABSTRACT)) {
        return PaletteUtils.getBundleString("MSG_abstractClass"); // NOI18N
    }

    if (!mods.contains(javax.lang.model.element.Modifier.PUBLIC)) {
        return PaletteUtils.getBundleString("MSG_notPublic"); // NOI18N
    }
    
    for (Element member : clazz.getEnclosedElements()) {
        mods = member.getModifiers();
        if (ElementKind.CONSTRUCTOR == member.getKind() &&
                mods.contains(javax.lang.model.element.Modifier.PUBLIC) &&
                ((ExecutableElement) member).getParameters().isEmpty()) {
            return null;
        }
    }
    
    return PaletteUtils.getBundleString("MSG_noPublicConstructor"); // NOI18N
}
 
Example 3
Project: RxPay   File: ClassEntity.java   Source Code and License Vote up 6 votes
/**
 * @param elementUtils
 * @param typeUtils
 * @param element      current anntated class
 */
public ClassEntity(Elements elementUtils, Types typeUtils, TypeElement element) {
    elementWeakCache = new WeakReference<TypeElement>(element);
    this.classPackageName = elementUtils.getPackageOf(element).getQualifiedName().toString();
    this.modifierSet = element.getModifiers();
    this.className = element.toString();
    annotationMirrors = element.getAnnotationMirrors();
    this.classSimpleName = element.getSimpleName();
    this.classQualifiedName = element.getQualifiedName();
    if ("java.lang.Object".equals(element.getSuperclass().toString())){
        this.superclass = null;
    }else{
        this.superclass = element.getSuperclass().toString();
    }
    List<? extends TypeMirror> interfaces = element.getInterfaces();

    for (TypeMirror anInterface : interfaces){
        this.interfaces.add(typeUtils.asElement(anInterface).toString());
    }
}
 
Example 4
Project: openjdk-jdk10   File: PubapiVisitor.java   Source Code and License Vote up 6 votes
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public Void visitType(TypeElement e, Void p) {
    if (isNonPrivate(e)) {
        PubApi prevApi = collectedApi;
        collectedApi = new PubApi();
        super.visitType(e, p);
        if (!isAnonymous(e)) {
            String name = ((ClassSymbol) e).flatname.toString();
            PubType t = new PubType(e.getModifiers(),
                                    name,
                                    //e.getQualifiedName().toString(),
                                    collectedApi);
            prevApi.types.put(t.fqName, t);
        }
        collectedApi = prevApi;
    }
    return null;
}
 
Example 5
Project: XModulable   File: XModuleProcessor.java   Source Code and License Vote up 5 votes
/**
 * 校验{@code moduleElement}的合法性
 *
 * @param moduleElement 携带@XModule的Element
 * @throws ProcessException
 */
private void verify(Element moduleElement) throws ProcessException {
    XModule XModule = moduleElement.getAnnotation(XModule.class);
    // 检验XModule注解
    if (XModule == null) {
        throw new ProcessException(
                String.format("当前的element(%s)未携带%s注解",
                        moduleElement.toString(),
                        XModule.class.getSimpleName()));
    } else if (XModule.name() == null || XModule.name().isEmpty()) {
        throw new ProcessException(String.format("%s的组件名不能为空", moduleElement.toString()));
    }

    // 检验被XModule注解的是否为class
    if (moduleElement.getKind() != ElementKind.CLASS) {
        throw new ProcessException(
                String.format("%s不是类,只有类才能使用%s",
                        moduleElement.toString(),
                        XModule.class.getSimpleName()));
    }

    TypeElement classElement = (TypeElement) moduleElement;

    // 检验类修饰符
    Set<Modifier> modifiers = classElement.getModifiers();
    if (!modifiers.contains(Modifier.PUBLIC)) {
        throw new ProcessException(
                String.format("被%s注解的%s的权限修饰符必须为public",
                        XModule.class.getSimpleName(),
                        classElement.getQualifiedName().toString()));
    }
    if (modifiers.contains(Modifier.ABSTRACT)) {
        throw new ProcessException(
                String.format("%s是抽象类,不能被%s注解",
                        classElement.getQualifiedName().toString(),
                        XModule.class.getSimpleName()));
    }
}
 
Example 6
Project: easy-plugins   File: Util.java   Source Code and License Vote up 5 votes
private static void checkClassVisibility(Elements eles, TypeElement target, String fromPkg) {
    Set<Modifier> targetMods = target.getModifiers();
    if (!isNestedClass(target)) {
        simpleVisibility(target.getModifiers(), eles.getPackageOf(target), fromPkg, target);
    } else {
        Element surround = target.getEnclosingElement();

        if (surround.getKind() != CLASS
                && surround.getKind() != INTERFACE
                // Not sure if that is allowed, but we include it anyways.
                && surround.getKind() != ENUM) {
            throw new IllegalStateException(
                    target.toString() + " is not nested? " + surround.toString());
        }

        TypeElement enclosingType = (TypeElement) surround;

        // The surrounding class must be visible for us to see the nested class
        if (!isElementVisibleFrom(eles, enclosingType, fromPkg)) {
            throw new EasyPluginException(enclosingType.toString()
                    + " is not visible from output package; need for access to " + target.toString());
        }
        // Nested class must be static since we don't have an enclosing instance
        if (!targetMods.contains(Modifier.STATIC)) {
            throw new EasyPluginException(target.toString() + " must be static.");
        }

        simpleVisibility(target.getModifiers(), eles.getPackageOf(target), fromPkg, target);
    }
}
 
Example 7
Project: incubator-netbeans   File: ElementJavadoc.java   Source Code and License Vote up 4 votes
private StringBuilder getClassHeader(TypeElement cdoc) {
    StringBuilder sb = new StringBuilder();
    sb.append("<pre>"); //NOI18N
    cdoc.getAnnotationMirrors().forEach((annotationDesc) -> {
        appendAnnotation(sb, annotationDesc, true);
    });
    for (Modifier modifier : cdoc.getModifiers()) {
        switch(cdoc.getKind()) {
            case ENUM:
                if (modifier == Modifier.FINAL)
                    continue;
                break;
            case INTERFACE:
            case ANNOTATION_TYPE:
                if (modifier == Modifier.ABSTRACT)
                    continue;
                break;                     
        }
        sb.append(modifier).append(' '); //NOI18N
    }
    switch (cdoc.getKind()) {
        case ANNOTATION_TYPE:
            sb.append("@interface "); //NOI18N
            break;
        case ENUM:
            sb.append("enum "); //NOI18N
            break;
        case INTERFACE:
            sb.append("interface "); //NOI18N
            break;
        default:
            sb.append("class "); //NOI18N            
    }
    sb.append("<b>").append(cdoc.getSimpleName()); //NOI18N
    List<? extends TypeParameterElement> typeParams = cdoc.getTypeParameters();
    if (!typeParams.isEmpty()) {
        sb.append("&lt;"); //NOI18N
        for (Iterator<? extends TypeParameterElement> it = typeParams.iterator(); it.hasNext();) {
            TypeParameterElement typeParam = it.next();
            appendType(sb, typeParam.asType(), false, true, false);
            if (it.hasNext())
                sb.append(","); //NOI18N
        }
        sb.append("&gt;"); //NOI18N
    }
    sb.append("</b>"); //NOi18N
    if (cdoc.getKind() != ElementKind.ANNOTATION_TYPE) {
        TypeMirror supercls = cdoc.getSuperclass();
        if (supercls != null && supercls.getKind() != TypeKind.NONE) {
            sb.append("<br>extends "); //NOI18N
            appendType(sb, supercls, false, false, false);            
        }
        List<? extends TypeMirror> ifaces = cdoc.getInterfaces();
        if (!ifaces.isEmpty()) {
            sb.append(cdoc.getKind().isInterface() ? "<br>extends " : "<br>implements "); //NOI18N
            for (Iterator<? extends TypeMirror> it = ifaces.iterator(); it.hasNext();) {
                TypeMirror iface = it.next();
                appendType(sb, iface, false, false, false);
                if (it.hasNext())
                    sb.append(", "); //NOI18N
            }            
        }
    }
    sb.append("</pre>"); //NOI18N
    return sb;
}
 
Example 8
Project: resourceprovider   File: ClassValidator.java   Source Code and License Vote up 4 votes
ClassValidator(TypeElement annotatedClass) {
  this.annotatedClass = annotatedClass;
  this.modifiers = annotatedClass.getModifiers();
}
 
Example 9
Project: OpenJSharp   File: WebServiceVisitor.java   Source Code and License Vote up 4 votes
protected boolean isLegalImplementation(WebService webService, TypeElement classElement) {
    boolean isStateful = isStateful(classElement);

    Collection<Modifier> modifiers = classElement.getModifiers();
    if (!modifiers.contains(Modifier.PUBLIC)) {
        builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_CLASS_NOT_PUBLIC(classElement.getQualifiedName()), classElement);
        return false;
    }
    if (modifiers.contains(Modifier.FINAL) && !isStateful) {
        builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_CLASS_IS_FINAL(classElement.getQualifiedName()), classElement);
        return false;
    }
    if (modifiers.contains(Modifier.ABSTRACT) && !isStateful) {
        builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_CLASS_IS_ABSTRACT(classElement.getQualifiedName()), classElement);
        return false;
    }
    boolean hasDefaultConstructor = false;
    for (ExecutableElement constructor : ElementFilter.constructorsIn(classElement.getEnclosedElements())) {
        if (constructor.getModifiers().contains(Modifier.PUBLIC) &&
                constructor.getParameters().isEmpty()) {
            hasDefaultConstructor = true;
            break;
        }
    }
    if (!hasDefaultConstructor && !isStateful) {
        if (classElement.getEnclosingElement() != null && !modifiers.contains(Modifier.STATIC)) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_CLASS_IS_INNERCLASS_NOT_STATIC(
                    classElement.getQualifiedName()), classElement);
            return false;
        }

        builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_NO_DEFAULT_CONSTRUCTOR(
                classElement.getQualifiedName()), classElement);
        return false;
    }
    if (webService.endpointInterface().isEmpty()) {
        if (!methodsAreLegal(classElement))
            return false;
    } else {
        TypeElement interfaceElement = getEndpointInterfaceElement(webService.endpointInterface(), classElement);
        if (!classImplementsSei(classElement, interfaceElement))
            return false;
    }

    return true;
}
 
Example 10
Project: openjdk-jdk10   File: GraphNodeProcessor.java   Source Code and License Vote up 4 votes
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (roundEnv.processingOver()) {
        return false;
    }

    GraphNodeVerifier verifier = new GraphNodeVerifier(this);

    for (Element element : roundEnv.getElementsAnnotatedWith(NodeInfo.class)) {
        scope = element;
        try {
            if (!isNodeType(element)) {
                errorMessage(element, "%s can only be applied to Node subclasses", NodeInfo.class.getSimpleName());
                continue;
            }

            NodeInfo nodeInfo = element.getAnnotation(NodeInfo.class);
            if (nodeInfo == null) {
                errorMessage(element, "Cannot get %s annotation from annotated element", NodeInfo.class.getSimpleName());
                continue;
            }

            TypeElement typeElement = (TypeElement) element;

            Set<Modifier> modifiers = typeElement.getModifiers();
            if (!modifiers.contains(Modifier.FINAL) && !modifiers.contains(Modifier.ABSTRACT)) {
                // TODO(thomaswue): Reenable this check.
                // errorMessage(element, "%s annotated class must be either final or abstract",
                // NodeInfo.class.getSimpleName());
                // continue;
            }
            boolean found = false;
            for (Element e : typeElement.getEnclosedElements()) {
                if (e.getKind() == ElementKind.FIELD) {
                    if (e.getSimpleName().toString().equals("TYPE")) {
                        found = true;
                        break;
                    }
                }
            }
            if (!found) {
                errorMessage(element, "%s annotated class must have a field named TYPE", NodeInfo.class.getSimpleName());
            }

            if (!typeElement.equals(verifier.Node) && !modifiers.contains(Modifier.ABSTRACT)) {
                verifier.verify(typeElement);
            }
        } catch (ElementException ee) {
            errorMessage(ee.element, ee.getMessage());
        } catch (Throwable t) {
            reportException(isBug367599(t) ? Kind.NOTE : Kind.ERROR, element, t);
        } finally {
            scope = null;
        }
    }
    return false;
}
 
Example 11
Project: openjdk-jdk10   File: WebServiceVisitor.java   Source Code and License Vote up 4 votes
protected boolean isLegalImplementation(WebService webService, TypeElement classElement) {
    boolean isStateful = isStateful(classElement);

    Collection<Modifier> modifiers = classElement.getModifiers();
    if (!modifiers.contains(Modifier.PUBLIC)) {
        builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_CLASS_NOT_PUBLIC(classElement.getQualifiedName()), classElement);
        return false;
    }
    if (modifiers.contains(Modifier.FINAL) && !isStateful) {
        builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_CLASS_IS_FINAL(classElement.getQualifiedName()), classElement);
        return false;
    }
    if (modifiers.contains(Modifier.ABSTRACT) && !isStateful) {
        builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_CLASS_IS_ABSTRACT(classElement.getQualifiedName()), classElement);
        return false;
    }
    boolean hasDefaultConstructor = false;
    for (ExecutableElement constructor : ElementFilter.constructorsIn(classElement.getEnclosedElements())) {
        if (constructor.getModifiers().contains(Modifier.PUBLIC) &&
                constructor.getParameters().isEmpty()) {
            hasDefaultConstructor = true;
            break;
        }
    }
    if (!hasDefaultConstructor && !isStateful) {
        if (classElement.getEnclosingElement() != null && !modifiers.contains(Modifier.STATIC)) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_CLASS_IS_INNERCLASS_NOT_STATIC(
                    classElement.getQualifiedName()), classElement);
            return false;
        }

        builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_NO_DEFAULT_CONSTRUCTOR(
                classElement.getQualifiedName()), classElement);
        return false;
    }
    if (webService.endpointInterface().isEmpty()) {
        if (!methodsAreLegal(classElement))
            return false;
    } else {
        TypeElement interfaceElement = getEndpointInterfaceElement(webService.endpointInterface(), classElement);
        if (!classImplementsSei(classElement, interfaceElement))
            return false;
    }

    return true;
}
 
Example 12
Project: easy-plugins   File: Util.java   Source Code and License Vote up 4 votes
static void checkConcreteType(TypeElement e) {
    Set<Modifier> mods = e.getModifiers();
    if (mods.contains(Modifier.ABSTRACT) || e.getKind().isInterface()) {
        throw new EasyPluginException(e.toString() + " must be concrete to use as a service.");
    }
}