Example usage for javax.lang.model.element ExecutableElement getModifiers

List of usage examples for javax.lang.model.element ExecutableElement getModifiers

Introduction

In this page you can find the example usage for javax.lang.model.element ExecutableElement getModifiers.

Prototype

Set<Modifier> getModifiers();

Source Link

Document

Returns the modifiers of this element, excluding annotations.

Usage

From source file:therian.buildweaver.StandardOperatorsProcessor.java

private static ExecutableElement findDefaultConstructor(TypeElement t) {
    for (Element element : t.getEnclosedElements()) {
        if (element.getKind() == ElementKind.CONSTRUCTOR) {
            ExecutableElement cs = (ExecutableElement) element;
            if (cs.getParameters().size() == 0 && cs.getModifiers().contains(Modifier.PUBLIC)) {
                return cs;
            }//from  ww  w .j  a  va  2s. c o  m
        }
    }
    return null;
}

From source file:com.github.jdot.type.Property.java

/**
 * Returns new BeanProperty instance if the specified element is a JavaBean accessor method, otherwise null.
 * /*from  ww  w  . j  av a  2s .co  m*/
 * @param element
 * @return
 */
public static Property build(Type owner, ExecutableElement element) {
    Property beanProperty = null;
    String name = null;
    boolean propertyFound = true;
    boolean writeable = false;
    String type = null;

    // Check modifiers
    boolean publicFound = false;
    boolean staticFound = false;
    for (Modifier modifier : element.getModifiers()) {
        if (Modifier.PUBLIC.equals(modifier)) {
            publicFound = true;
        }
        if (Modifier.STATIC.equals(modifier)) {
            staticFound = true;
        }
    }
    if (!publicFound || staticFound) {
        propertyFound = false;
    }

    // Check method name
    if (propertyFound) {
        String methodName = element.getSimpleName().toString();
        if (methodName.startsWith("set") && Character.isUpperCase(methodName.charAt(3))) {
            name = StringUtils.uncapitalize(methodName.substring(3));
            writeable = true;
        } else if (methodName.startsWith("get") && Character.isUpperCase(methodName.charAt(3))) {
            name = StringUtils.uncapitalize(methodName.substring(3));
        } else if (methodName.startsWith("is") && Character.isUpperCase(methodName.charAt(2))) {
            name = StringUtils.uncapitalize(methodName.substring(2));
        } else {
            propertyFound = false;
        }
    }

    // Check arguments / return type
    if (propertyFound) {
        if (writeable) {
            if (element.getParameters().size() == 1
                    && TypeKind.VOID.equals(element.getReturnType().getKind())) {
                type = element.getParameters().get(0).asType().toString();
            } else {
                propertyFound = false;
            }
        } else {
            if (TypeKind.VOID.equals(element.getReturnType().getKind())) {
                propertyFound = false;
            } else {
                type = element.getReturnType().toString();
            }
        }
    }

    if (propertyFound) {
        beanProperty = new Property(owner, element, name, type, writeable);
    }
    return beanProperty;
}

From source file:org.leandreck.endpoints.processor.model.MethodNodeFactory.java

private static boolean defineIgnored(final ExecutableElement methodElement,
        final RequestMapping requestMapping) {
    boolean hasIgnoreAnnotation = methodElement.getAnnotation(TypeScriptIgnore.class) != null;
    boolean hasRequestMappingAnnotation = requestMapping != null;
    boolean producesJson = hasRequestMappingAnnotation && Arrays.stream(requestMapping.produces())
            .map(value -> value.startsWith(MediaType.APPLICATION_JSON_VALUE)).reduce(false, (a, b) -> a || b);

    boolean isPublic = methodElement.getModifiers().contains(Modifier.PUBLIC);
    return hasIgnoreAnnotation || !isPublic || !hasRequestMappingAnnotation || !producesJson;
}

From source file:auto.parse.processor.AutoParseProcessor.java

/**
 * Given a list of all methods defined in or inherited by a class, returns a map with keys
 * "toString", "equals", "hashCode" and corresponding value true if that method should be
 * generated./*from w w w .ja  v a 2  s. c o m*/
 */
private static Map<String, Boolean> objectMethodsToGenerate(List<ExecutableElement> methods) {
    Map<String, Boolean> vars = new TreeMap<String, Boolean>();
    // The defaults here only come into play when an ancestor class doesn't exist.
    // Compilation will fail in that case, but we don't want it to crash the compiler with
    // an exception before it does. If all ancestors do exist then we will definitely find
    // definitions of these three methods (perhaps the ones in Object) so we will overwrite these:
    vars.put("equals", false);
    vars.put("hashCode", false);
    vars.put("toString", false);
    for (ExecutableElement method : methods) {
        if (isToStringOrEqualsOrHashCode(method)) {
            boolean canGenerate = method.getModifiers().contains(Modifier.ABSTRACT)
                    || isJavaLangObject((TypeElement) method.getEnclosingElement());
            vars.put(method.getSimpleName().toString(), canGenerate);
        }
    }
    assert vars.size() == 3;
    return vars;
}

From source file:com.github.pellaton.jazoon2012.JazoonProcessor.java

private void processMethod(TypeElement typeElement, ExecutableElement method) {
    if (method.getModifiers().contains(Modifier.PRIVATE)) {
        this.messager.printMessage(Kind.ERROR, "@bean methods must not be private.", method);
    }/*from   w  w  w.  j a  v a 2  s.  c o  m*/
}

From source file:org.mule.devkit.validation.RestValidator.java

@Override
public void validate(DevKitTypeElement typeElement, GeneratorContext context) throws ValidationException {

    for (ExecutableElement method : typeElement.getMethodsAnnotatedWith(RestCall.class)) {

        if (!method.getModifiers().contains(Modifier.ABSTRACT)) {
            throw new ValidationException(method, "@RestCall can only be applied to abstract methods");
        }/*from  ww w  . ja  v  a2s. co m*/

        if (method.getThrownTypes().size() != 1) {
            throw new ValidationException(method, "@RestCall abstract method must throw IOException");
        }

        RestExceptionOn restExceptionOn = method.getAnnotation(RestExceptionOn.class);
        if (restExceptionOn != null) {
            if (restExceptionOn.statusCodeIs().length != 0 && restExceptionOn.statusCodeIsNot().length != 0) {
                throw new ValidationException(method,
                        "@RestExceptionOn can only be used with statusCodeIs or statusCodeIsNot. Not both.");
            }
            if (restExceptionOn.statusCodeIs().length == 0 && restExceptionOn.statusCodeIsNot().length == 0) {
                throw new ValidationException(method,
                        "@RestExceptionOn must have either statusCodeIs or statusCodeIsNot.");
            }
        }

        int nonAnnotatedParameterCount = 0;
        for (VariableElement parameter : method.getParameters()) {
            if (parameter.getAnnotation(RestUriParam.class) == null
                    && parameter.getAnnotation(RestHeaderParam.class) == null
                    && parameter.getAnnotation(RestQueryParam.class) == null) {
                nonAnnotatedParameterCount++;
            }
        }

        if (nonAnnotatedParameterCount > 1) {
            throw new ValidationException(method,
                    "Only one parameter can be used as payload, everything else must be annotated with @RestUriParam, @RestQueryParam or @RestHeaderParam.");
        }
    }

    for (VariableElement field : typeElement.getFieldsAnnotatedWith(RestUriParam.class)) {
        boolean getterFound = false;
        for (ExecutableElement method : typeElement.getMethods()) {
            if (method.getSimpleName().toString()
                    .equals("get" + StringUtils.capitalize(field.getSimpleName().toString()))) {
                getterFound = true;
                break;
            }
        }
        if (!getterFound) {
            throw new ValidationException(field,
                    "Cannot find a getter method for " + field.getSimpleName().toString()
                            + " but its being marked as URI parameter of a REST call.");
        }
    }

    if (typeElement.getFieldsAnnotatedWith(RestHttpClient.class).size() > 1) {
        throw new ValidationException(typeElement,
                "There can only be one field annotated with @RestHttpClient.");
    }

    if (typeElement.getFieldsAnnotatedWith(RestHttpClient.class).size() > 0) {
        if (!typeElement.getFieldsAnnotatedWith(RestHttpClient.class).get(0).asType().toString()
                .equals(HttpClient.class.getName())) {
            throw new ValidationException(typeElement.getFieldsAnnotatedWith(RestHttpClient.class).get(0),
                    "A field annotated with @RestHttpClient must be of type " + HttpClient.class.getName());
        }
    }

    for (VariableElement field : typeElement.getFieldsAnnotatedWith(RestHttpClient.class)) {
        boolean getterFound = false;
        for (ExecutableElement method : typeElement.getMethods()) {
            if (method.getSimpleName().toString()
                    .equals("get" + StringUtils.capitalize(field.getSimpleName().toString()))) {
                getterFound = true;
                break;
            }
        }
        if (!getterFound) {
            throw new ValidationException(field, "Cannot find a getter method for "
                    + field.getSimpleName().toString() + " but its being marked with @RestHttpClient.");
        }
    }

}

From source file:com.github.pellaton.springconfigvalidation.SpringConfigurationValidationProcessor.java

private boolean isVisibleElement(ExecutableElement constructor) {
    return !constructor.getModifiers().contains(Modifier.PRIVATE);
}

From source file:uniol.apt.compiler.AbstractServiceProcessor.java

private boolean isValidClass(Element ele) {
    if (ele.getKind() != ElementKind.CLASS) {
        error(ele, "Non-Class %s annotated with %s.", ele.getSimpleName().toString(),
                this.annotationClass.getCanonicalName());
    }/* w  ww.  jav a  2 s . c o m*/

    TypeElement classEle = (TypeElement) ele;

    if (!classEle.getModifiers().contains(Modifier.PUBLIC)) {
        error(classEle, "Class %s is not public.", classEle.getQualifiedName().toString());
        return false;
    }

    if (classEle.getModifiers().contains(Modifier.ABSTRACT)) {
        error(classEle, "Class %s is abstract.", classEle.getQualifiedName().toString());
        return false;
    }

    TypeMirror expected = this.types.erasure(this.elements.getTypeElement(this.interfaceName).asType());
    boolean found = false;
    for (TypeMirror actual : classEle.getInterfaces()) {
        if (this.types.isAssignable(actual, expected)) {
            found = true;
            break;
        }
    }
    if (!found) {
        error(classEle, "Class %s doesn't implement interface %s.", classEle.getQualifiedName().toString(),
                this.interfaceName);
        return false;
    }

    if (!allowGenerics && !classEle.getTypeParameters().isEmpty()) {
        error(classEle, "Class %s is generic.", classEle.getQualifiedName().toString());
        return false;
    }

    for (Element enclosed : classEle.getEnclosedElements()) {
        if (enclosed.getKind() == ElementKind.CONSTRUCTOR) {
            ExecutableElement constructorEle = (ExecutableElement) enclosed;
            if (constructorEle.getParameters().size() == 0
                    && constructorEle.getModifiers().contains(Modifier.PUBLIC)) {
                return true;
            }
        }
    }

    error(classEle, String.format("Class %s needs an public no-arg constructor",
            classEle.getQualifiedName().toString()));
    return false;
}

From source file:auto.parse.processor.AutoParseProcessor.java

private void findLocalAndInheritedMethods(TypeElement type, List<ExecutableElement> methods) {
    note("Looking at methods in " + type);
    Types typeUtils = processingEnv.getTypeUtils();
    Elements elementUtils = processingEnv.getElementUtils();
    for (TypeMirror superInterface : type.getInterfaces()) {
        findLocalAndInheritedMethods((TypeElement) typeUtils.asElement(superInterface), methods);
    }/*from w ww.  j a v  a 2  s. c  o  m*/
    if (type.getSuperclass().getKind() != TypeKind.NONE) {
        // Visit the superclass after superinterfaces so we will always see the implementation of a
        // method after any interfaces that declared it.
        findLocalAndInheritedMethods((TypeElement) typeUtils.asElement(type.getSuperclass()), methods);
    }
    // Add each method of this class, and in so doing remove any inherited method it overrides.
    // This algorithm is quadratic in the number of methods but it's hard to see how to improve
    // that while still using Elements.overrides.
    List<ExecutableElement> theseMethods = ElementFilter.methodsIn(type.getEnclosedElements());
    eclipseHack().sortMethodsIfSimulatingEclipse(theseMethods);
    for (ExecutableElement method : theseMethods) {
        if (!method.getModifiers().contains(Modifier.PRIVATE)) {
            boolean alreadySeen = false;
            for (Iterator<ExecutableElement> methodIter = methods.iterator(); methodIter.hasNext();) {
                ExecutableElement otherMethod = methodIter.next();
                if (elementUtils.overrides(method, otherMethod, type)) {
                    methodIter.remove();
                } else if (method.getSimpleName().equals(otherMethod.getSimpleName())
                        && method.getParameters().equals(otherMethod.getParameters())) {
                    // If we inherit this method on more than one path, we don't want to add it twice.
                    alreadySeen = true;
                }
            }
            if (!alreadySeen) {
                methods.add(method);
            }
        }
    }
}

From source file:auto.parse.processor.AutoParseProcessor.java

private List<ExecutableElement> methodsToImplement(List<ExecutableElement> methods, Map<String, Object> vars)
        throws CompileException {
    List<ExecutableElement> toImplement = new ArrayList<ExecutableElement>();
    boolean errors = false;
    for (ExecutableElement method : methods) {
        if (method.getModifiers().contains(Modifier.ABSTRACT) && !isToStringOrEqualsOrHashCode(method)
                && !isFromParcelable(method)) {
            if (method.getParameters().isEmpty() && method.getReturnType().getKind() != TypeKind.VOID
                    && (method.getSimpleName().toString().startsWith("get"))) {
                if (isReferenceArrayType(method.getReturnType())) {
                    reportError("An @AutoParse class cannot define an array-valued property unless it is "
                            + "a byte array", method);
                    errors = true;/* w  w w .  j  a va  2s.  c o m*/
                }

                toImplement.add(method);
            } else if ((method.getParameters().size() == 1)
                    && (method.getReturnType().getKind() != TypeKind.VOID)
                    && (method.getSimpleName().toString().startsWith("get"))) {
                if (!method.getParameters().get(0).asType().equals(method.getReturnType())) {
                    reportError("An @AutoParse class cannot define an default-value that is difference"
                            + "to return-value ", method);
                    errors = true;
                }

                toImplement.add(method);
            } else if ((method.getParameters().size() == 1)
                    && isSuperType(method.getReturnType(), ((TypeElement) vars.get("type")).asType())
                    && (method.getSimpleName().toString().startsWith("set"))) {
                if (isReferenceArrayType(method.getParameters().get(0).asType())) {
                    reportError("An @AutoParse class cannot define an array-valued property unless it is "
                            + "a byte array", method);
                    errors = true;
                }

                toImplement.add(method);
            } else {
                reportError(
                        "@AutoParse classes cannot have abstract methods other than property getters and setters",
                        method);
                errors = true;
            }
        }
    }
    if (errors) {
        throw new CompileException();
    }
    return toImplement;
}