Search in sources :

Example 61 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)

Example 62 with ExecutableElement

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

the class EventBusAnnotationProcessor method checkForSubscribersToSkip.

/**
     * Subscriber classes should be skipped if their class or any involved event class are not visible to the index.
     */
private void checkForSubscribersToSkip(Messager messager, String myPackage) {
    for (TypeElement skipCandidate : methodsByClass.keySet()) {
        TypeElement subscriberClass = skipCandidate;
        while (subscriberClass != null) {
            if (!isVisible(myPackage, subscriberClass)) {
                boolean added = classesToSkip.add(skipCandidate);
                if (added) {
                    String msg;
                    if (subscriberClass.equals(skipCandidate)) {
                        msg = "Falling back to reflection because class is not public";
                    } else {
                        msg = "Falling back to reflection because " + skipCandidate + " has a non-public super class";
                    }
                    messager.printMessage(Diagnostic.Kind.NOTE, msg, subscriberClass);
                }
                break;
            }
            List<ExecutableElement> methods = methodsByClass.get(subscriberClass);
            if (methods != null) {
                for (ExecutableElement method : methods) {
                    String skipReason = null;
                    VariableElement param = method.getParameters().get(0);
                    TypeMirror typeMirror = getParamTypeMirror(param, messager);
                    if (!(typeMirror instanceof DeclaredType) || !(((DeclaredType) typeMirror).asElement() instanceof TypeElement)) {
                        skipReason = "event type cannot be processed";
                    }
                    if (skipReason == null) {
                        TypeElement eventTypeElement = (TypeElement) ((DeclaredType) typeMirror).asElement();
                        if (!isVisible(myPackage, eventTypeElement)) {
                            skipReason = "event type is not public";
                        }
                    }
                    if (skipReason != null) {
                        boolean added = classesToSkip.add(skipCandidate);
                        if (added) {
                            String msg = "Falling back to reflection because " + skipReason;
                            if (!subscriberClass.equals(skipCandidate)) {
                                msg += " (found in super class for " + skipCandidate + ")";
                            }
                            messager.printMessage(Diagnostic.Kind.NOTE, msg, param);
                        }
                        break;
                    }
                }
            }
            subscriberClass = getSuperclass(subscriberClass);
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 63 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project tiger by google.

the class Utils method findAllModulesRecursively.

@SuppressWarnings("unchecked")
public static Set<TypeElement> findAllModulesRecursively(TypeElement inModule) {
    Set<TypeElement> result = new HashSet<>();
    result.add(inModule);
    for (AnnotationMirror annotationMirror : inModule.getAnnotationMirrors()) {
        for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : annotationMirror.getElementValues().entrySet()) {
            ExecutableElement key = entry.getKey();
            /** Checks for {@link Module.includes}. */
            if (key.getSimpleName().contentEquals("includes")) {
                for (AnnotationValue annotationValue : (List<AnnotationValue>) entry.getValue().getValue()) {
                    TypeElement childModule = (TypeElement) ((DeclaredType) annotationValue.getValue()).asElement();
                    result.addAll(findAllModulesRecursively(childModule));
                }
            }
        }
    }
    return result;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) AnnotationValue(javax.lang.model.element.AnnotationValue) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) IntoMap(dagger.multibindings.IntoMap) HashSet(java.util.HashSet)

Example 64 with ExecutableElement

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

the class JavadocConverter method visitParam.

@Override
public Void visitParam(ParamTree node, TagElement tag) {
    DCTree.DCIdentifier identifier = (DCTree.DCIdentifier) node.getName();
    if (identifier == null || node.isTypeParameter()) {
        return null;
    }
    List<? extends VariableElement> params = element instanceof ExecutableElement ? ((ExecutableElement) element).getParameters() : Collections.emptyList();
    tag.setTagName(TagElement.TAG_PARAM);
    String name = identifier.toString();
    VariableElement param = null;
    for (VariableElement p : params) {
        if (name.equals(p.getSimpleName().toString())) {
            param = p;
            break;
        }
    }
    // param will be null if the @param tag refers to a nonexistent parameter.
    TreeNode nameNode = param != null ? new SimpleName(param) : new SimpleName(name);
    setPos(identifier, nameNode);
    tag.addFragment(nameNode);
    scan(node.getDescription(), tag);
    int lastEnd = nameNode.getStartPosition();
    for (TreeNode fragment : tag.getFragments()) {
        // TODO(tball): remove and fix JavadocGenerator after javac switch.
        if (fragment.getKind() == TreeNode.Kind.TEXT_ELEMENT) {
            TextElement text = (TextElement) fragment;
            text.setText(" " + text.getText());
            text.setSourceRange(text.getStartPosition(), text.getLength() + 1);
        }
        int thisEnd = lastEnd + fragment.getLength();
        setPos(fragment, lastEnd, thisEnd);
        lastEnd = thisEnd;
    }
    setPos(tag, pos(node), endPos(node));
    tag.setLineNumber(nameNode.getLineNumber());
    return null;
}
Also used : TextElement(com.google.devtools.j2objc.ast.TextElement) TreeNode(com.google.devtools.j2objc.ast.TreeNode) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableElement(javax.lang.model.element.VariableElement) DCTree(com.sun.tools.javac.tree.DCTree)

Example 65 with ExecutableElement

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

the class JdtAnnotationMirror method getElementValues.

@Override
public Map<? extends ExecutableElement, ? extends AnnotationValue> getElementValues() {
    Map<ExecutableElement, AnnotationValue> elementValues = new HashMap<>();
    for (IMemberValuePairBinding pair : binding.getAllMemberValuePairs()) {
        ExecutableElement element = new JdtExecutableElement(pair.getMethodBinding());
        AnnotationValue value = new JdtAnnotationValue(pair.getValue());
        elementValues.put(element, value);
    }
    return elementValues;
}
Also used : HashMap(java.util.HashMap) ExecutableElement(javax.lang.model.element.ExecutableElement) AnnotationValue(javax.lang.model.element.AnnotationValue) IMemberValuePairBinding(org.eclipse.jdt.core.dom.IMemberValuePairBinding)

Aggregations

ExecutableElement (javax.lang.model.element.ExecutableElement)345 TypeElement (javax.lang.model.element.TypeElement)158 TypeMirror (javax.lang.model.type.TypeMirror)97 VariableElement (javax.lang.model.element.VariableElement)85 Element (javax.lang.model.element.Element)72 Test (org.junit.Test)41 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)32 DeclaredType (javax.lang.model.type.DeclaredType)31 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 HashSet (java.util.HashSet)17 IJExpression (com.helger.jcodemodel.IJExpression)16 ElementValidation (org.androidannotations.ElementValidation)15 Block (com.google.devtools.j2objc.ast.Block)13 JMethod (com.helger.jcodemodel.JMethod)13 Elements (javax.lang.model.util.Elements)13