Search in sources :

Example 96 with ExecutableElement

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

the class MethodProcessor method isCallbackMethodAnnotationValid.

public boolean isCallbackMethodAnnotationValid(Element element, String annotationName) {
    TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
    if (enclosingElement.getAnnotation(JsonObject.class) == null) {
        error(enclosingElement, "%s: @%s methods can only be in classes annotated with @%s.", enclosingElement.getQualifiedName(), annotationName, JsonObject.class.getSimpleName());
        return false;
    }
    ExecutableElement executableElement = (ExecutableElement) element;
    if (executableElement.getParameters().size() > 0) {
        error(element, "%s: @%s methods must not take any parameters.", enclosingElement.getQualifiedName(), annotationName);
        return false;
    }
    List<? extends Element> allElements = enclosingElement.getEnclosedElements();
    int methodInstances = 0;
    for (Element enclosedElement : allElements) {
        for (AnnotationMirror am : enclosedElement.getAnnotationMirrors()) {
            if (am.getAnnotationType().asElement().getSimpleName().toString().equals(annotationName)) {
                methodInstances++;
            }
        }
    }
    if (methodInstances != 1) {
        error(element, "%s: There can only be one @%s method per class.", enclosingElement.getQualifiedName(), annotationName);
        return false;
    }
    return true;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) JsonObject(com.bluelinelabs.logansquare.annotation.JsonObject)

Example 97 with ExecutableElement

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

the class OnJsonParseCompleteProcessor method processOnCompleteMethodAnnotation.

private void processOnCompleteMethodAnnotation(Element element, Map<String, JsonObjectHolder> jsonObjectMap, Elements elements) throws Exception {
    if (!isCallbackMethodAnnotationValid(element, OnJsonParseComplete.class.getSimpleName())) {
        return;
    }
    TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
    ExecutableElement executableElement = (ExecutableElement) element;
    JsonObjectHolder objectHolder = jsonObjectMap.get(TypeUtils.getInjectedFQCN(enclosingElement, elements));
    objectHolder.onCompleteCallback = executableElement.getSimpleName().toString();
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) JsonObjectHolder(com.bluelinelabs.logansquare.processor.JsonObjectHolder)

Example 98 with ExecutableElement

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

the class OnPreSerializeProcessor method processOnPreJsonSerializeMethodAnnotation.

private void processOnPreJsonSerializeMethodAnnotation(Element element, Map<String, JsonObjectHolder> jsonObjectMap, Elements elements) throws Exception {
    if (!isCallbackMethodAnnotationValid(element, OnPreJsonSerialize.class.getSimpleName())) {
        return;
    }
    TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
    ExecutableElement executableElement = (ExecutableElement) element;
    JsonObjectHolder objectHolder = jsonObjectMap.get(TypeUtils.getInjectedFQCN(enclosingElement, elements));
    objectHolder.preSerializeCallback = executableElement.getSimpleName().toString();
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) JsonObjectHolder(com.bluelinelabs.logansquare.processor.JsonObjectHolder)

Example 99 with ExecutableElement

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

the class JsonFieldHolder method getGetter.

public static String getGetter(Element element, Elements elements) {
    TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
    TypeKind elementTypeKind = element.asType().getKind();
    String elementName = element.getSimpleName().toString();
    String elementNameLowerCase = elementName.toLowerCase();
    List<String> possibleMethodNames = new ArrayList<>();
    possibleMethodNames.add("get" + elementNameLowerCase);
    if (elementTypeKind == TypeKind.BOOLEAN) {
        possibleMethodNames.add("is" + elementNameLowerCase);
        possibleMethodNames.add("has" + elementNameLowerCase);
        possibleMethodNames.add(elementNameLowerCase);
    }
    // Handle the case where variables are named in the form mVariableName instead of just variableName
    if (elementName.length() > 1 && elementName.charAt(0) == 'm' && (elementName.charAt(1) >= 'A' && elementName.charAt(1) <= 'Z')) {
        possibleMethodNames.add("get" + elementNameLowerCase.substring(1));
        if (elementTypeKind == TypeKind.BOOLEAN) {
            possibleMethodNames.add("is" + elementNameLowerCase.substring(1));
            possibleMethodNames.add("has" + elementNameLowerCase.substring(1));
            possibleMethodNames.add(elementNameLowerCase.substring(1));
        }
    }
    List<? extends Element> elementMembers = elements.getAllMembers(enclosingElement);
    List<ExecutableElement> elementMethods = ElementFilter.methodsIn(elementMembers);
    for (ExecutableElement methodElement : elementMethods) {
        if (methodElement.getParameters().size() == 0) {
            String methodNameString = methodElement.getSimpleName().toString();
            String methodNameLowerCase = methodNameString.toLowerCase();
            if (possibleMethodNames.contains(methodNameLowerCase)) {
                if (methodElement.getParameters().size() == 0) {
                    if (methodElement.getReturnType().toString().equals(element.asType().toString())) {
                        return methodNameString;
                    }
                }
            }
        }
    }
    return null;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeKind(javax.lang.model.type.TypeKind) ArrayList(java.util.ArrayList)

Example 100 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project EventBus by greenrobot.

the class EventBusAnnotationProcessor method writeCreateSubscriberMethods.

private void writeCreateSubscriberMethods(BufferedWriter writer, List<ExecutableElement> methods, String callPrefix, String myPackage) throws IOException {
    for (ExecutableElement method : methods) {
        List<? extends VariableElement> parameters = method.getParameters();
        TypeMirror paramType = getParamTypeMirror(parameters.get(0), null);
        TypeElement paramElement = (TypeElement) processingEnv.getTypeUtils().asElement(paramType);
        String methodName = method.getSimpleName().toString();
        String eventClass = getClassString(paramElement, myPackage) + ".class";
        Subscribe subscribe = method.getAnnotation(Subscribe.class);
        List<String> parts = new ArrayList<>();
        parts.add(callPrefix + "(\"" + methodName + "\",");
        String lineEnd = "),";
        if (subscribe.priority() == 0 && !subscribe.sticky()) {
            if (subscribe.threadMode() == ThreadMode.POSTING) {
                parts.add(eventClass + lineEnd);
            } else {
                parts.add(eventClass + ",");
                parts.add("ThreadMode." + subscribe.threadMode().name() + lineEnd);
            }
        } else {
            parts.add(eventClass + ",");
            parts.add("ThreadMode." + subscribe.threadMode().name() + ",");
            parts.add(subscribe.priority() + ",");
            parts.add(subscribe.sticky() + lineEnd);
        }
        writeLine(writer, 3, parts.toArray(new String[parts.size()]));
        if (verbose) {
            processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Indexed @Subscribe at " + method.getEnclosingElement().getSimpleName() + "." + methodName + "(" + paramElement.getSimpleName() + ")");
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) Subscribe(org.greenrobot.eventbus.Subscribe)

Aggregations

ExecutableElement (javax.lang.model.element.ExecutableElement)341 TypeElement (javax.lang.model.element.TypeElement)154 TypeMirror (javax.lang.model.type.TypeMirror)96 VariableElement (javax.lang.model.element.VariableElement)85 Element (javax.lang.model.element.Element)69 Test (org.junit.Test)41 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)32 DeclaredType (javax.lang.model.type.DeclaredType)30 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 IJExpression (com.helger.jcodemodel.IJExpression)16 HashSet (java.util.HashSet)16 ElementValidation (org.androidannotations.ElementValidation)15 Block (com.google.devtools.j2objc.ast.Block)13 JMethod (com.helger.jcodemodel.JMethod)13 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)11