Search in sources :

Example 21 with Modifier

use of javax.lang.model.element.Modifier in project ngAndroid by davityle.

the class ModelSourceLinker method getSourceLink.

private NgModelSourceLink getSourceLink(Element element) {
    TypeMirror fieldType = element.asType();
    TypeElement typeElement = typeUtils.asTypeElement(fieldType);
    Option<String> packageName = elementUtils.getPackageName(typeElement);
    String fullName = elementUtils.getFullName(typeElement);
    List<SourceField> fields = new ArrayList<>();
    String modelName = elementUtils.stripClassName(fieldType);
    List<? extends Element> enclosedElements = typeElement.getEnclosedElements();
    boolean isInterface = typeElement.getKind().isInterface();
    if (isInterface) {
        messageUtils.error(Option.of(typeElement), "Model must be a class");
    }
    Set<String> duplicateCheck = new HashSet<>();
    for (int index = 0; index < enclosedElements.size(); index++) {
        Element enclosedElement = enclosedElements.get(index);
        if (elementUtils.isSetter(enclosedElement)) {
            ExecutableElement setter = (ExecutableElement) enclosedElement;
            if (!elementUtils.returnsVoid(setter)) {
                messageUtils.error(Option.of(element), "Setter '%s' must not return a value", element.toString());
                continue;
            }
            Set<Modifier> modifiers = setter.getModifiers();
            if (modifiers.contains(Modifier.PRIVATE) || modifiers.contains(Modifier.PROTECTED)) {
                messageUtils.error(Option.of(setter), "Unable to access field '%s' from scope '%s'. Must have default or public access", element.toString(), element.getEnclosingElement().toString());
                continue;
            }
            String fName = setter.getSimpleName().toString().substring(3).toLowerCase();
            if (duplicateCheck.contains(fName)) {
                messageUtils.error(Option.of(setter), "Field '%s' in model '%s' is a duplicate.", setter.getSimpleName().toString().substring(3), fullName);
                continue;
            }
            duplicateCheck.add(fName);
            TypeMirror typeMirror = setter.getParameters().get(0).asType();
            String type = typeMirror.toString();
            SourceField sourceField = new SourceField(fName, type, primitiveUtils);
            sourceField.setSetter(setter.getSimpleName().toString());
            // TODO O(n^2) is the best
            boolean getterFound = false;
            for (Element possGetter : enclosedElements) {
                if (elementUtils.isGetterForField(possGetter, fName, typeMirror.getKind())) {
                    sourceField.setGetter(possGetter.getSimpleName().toString());
                    getterFound = true;
                    break;
                }
            }
            if (!getterFound) {
                messageUtils.error(Option.of(setter), "Field '%s' is missing a corresponding getter", fName);
            }
            fields.add(sourceField);
        }
    }
    return new NgModelSourceLink(modelName, packageName, fullName, isInterface, fields, element);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) SourceField(com.github.davityle.ngprocessor.source.SourceField) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) NgModelSourceLink(com.github.davityle.ngprocessor.source.links.NgModelSourceLink) TypeMirror(javax.lang.model.type.TypeMirror) Modifier(javax.lang.model.element.Modifier) HashSet(java.util.HashSet)

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