Search in sources :

Example 56 with TypeElement

use of javax.lang.model.element.TypeElement in project motan by weibocom.

the class MotanAsyncProcessor method addMethods.

private void addMethods(TypeElement interfaceClazz, TypeSpec.Builder classBuilder) {
    List<? extends Element> elements = interfaceClazz.getEnclosedElements();
    if (elements != null && !elements.isEmpty()) {
        for (Element e : elements) {
            if (ElementKind.METHOD.equals(e.getKind())) {
                ExecutableElement method = (ExecutableElement) e;
                MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(method.getSimpleName().toString() + ASYNC).addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT).returns(ResponseFuture.class).addTypeVariables(getTypeNames(method.getTypeParameters()));
                // add method params
                List<? extends VariableElement> vars = method.getParameters();
                for (VariableElement var : vars) {
                    methodBuilder.addParameter(ParameterSpec.builder(TypeName.get(var.asType()), var.getSimpleName().toString()).build());
                }
                classBuilder.addMethod(methodBuilder.build());
            }
        }
    }
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeParameterElement(javax.lang.model.element.TypeParameterElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ResponseFuture(com.weibo.api.motan.rpc.ResponseFuture) VariableElement(javax.lang.model.element.VariableElement)

Example 57 with TypeElement

use of javax.lang.model.element.TypeElement in project motan by weibocom.

the class MotanAsyncProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (roundEnv.processingOver()) {
        return true;
    }
    for (Element elem : roundEnv.getElementsAnnotatedWith(MotanAsync.class)) {
        processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "MotanAsyncProcessor will process " + elem.toString() + ", generate class path:" + TARGET_DIR);
        try {
            writeAsyncClass(elem);
            processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "MotanAsyncProcessor done for " + elem.toString());
        } catch (Exception e) {
            processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "MotanAsyncProcessor process " + elem.toString() + " fail. exception:" + e.getMessage());
            e.printStackTrace();
        }
    }
    return true;
}
Also used : VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeParameterElement(javax.lang.model.element.TypeParameterElement) IOException(java.io.IOException)

Example 58 with TypeElement

use of javax.lang.model.element.TypeElement in project intellij-community by JetBrains.

the class JavacASTAnalyser method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    final Set<? extends Element> elements = roundEnv.getRootElements();
    for (Element element : elements) {
        if (!(element instanceof TypeElement)) {
            continue;
        }
        final TypeElement typeElement = (TypeElement) element;
        final ImportsCollector importsCollector = new ImportsCollector();
        importsCollector.scan(myTrees.getPath(typeElement).getParentPath().getLeaf(), myTrees);
        final Set<String> imports = importsCollector.getImports();
        final Set<String> staticImports = importsCollector.getStaticImports();
        if (!imports.isEmpty() || !staticImports.isEmpty()) {
            final String className = typeElement.getQualifiedName().toString();
            myOutputConsumer.registerImports(className, imports, staticImports);
        }
        break;
    }
    return mySuppressOtherProcessors;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement)

Example 59 with TypeElement

use of javax.lang.model.element.TypeElement in project lombok by rzwitserloot.

the class TestAp method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    roundCounter++;
    log("TestAP in round " + roundCounter);
    boolean foundGetTest = false;
    int annotatedElemCount = 0;
    for (Element annotated : roundEnv.getElementsAnnotatedWith(ExampleAnnotation.class)) {
        annotatedElemCount++;
        for (Element child : annotated.getEnclosedElements()) {
            if (child.getSimpleName().toString().equals("getTest") && child.getKind() == ElementKind.METHOD)
                foundGetTest = true;
            if (child instanceof ExecutableElement) {
                TypeMirror returnType = ((ExecutableElement) child).getReturnType();
                System.out.println("RETURN TYPE for " + child.getSimpleName() + ": " + returnType.getClass() + " -- " + returnType.toString());
            }
        }
    }
    if (foundGetTest)
        log("RESULT: POSITIVE -- found the getTest method");
    else if (annotatedElemCount > 0)
        log("RESULT: NEGATIVE -- found the example class but there's no getTest method in it according to the type mirror.");
    else
        log("RESULT: AMBIVALENT -- The example class is not provided by 'getElementsAnnotatedWith' in this round. Not an issue, unless previously you got a NEGATIVE result.");
    return false;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 60 with TypeElement

use of javax.lang.model.element.TypeElement in project spring-framework by spring-projects.

the class TypeHelper method getType.

public String getType(TypeMirror type) {
    if (type == null) {
        return null;
    }
    if (type instanceof DeclaredType) {
        DeclaredType declaredType = (DeclaredType) type;
        Element enclosingElement = declaredType.asElement().getEnclosingElement();
        if (enclosingElement != null && enclosingElement instanceof TypeElement) {
            return getQualifiedName(enclosingElement) + "$" + declaredType.asElement().getSimpleName().toString();
        } else {
            return getQualifiedName(declaredType.asElement());
        }
    }
    return type.toString();
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

TypeElement (javax.lang.model.element.TypeElement)1562 ExecutableElement (javax.lang.model.element.ExecutableElement)517 Element (javax.lang.model.element.Element)502 TypeMirror (javax.lang.model.type.TypeMirror)420 VariableElement (javax.lang.model.element.VariableElement)333 DeclaredType (javax.lang.model.type.DeclaredType)201 ArrayList (java.util.ArrayList)177 PackageElement (javax.lang.model.element.PackageElement)159 AnnotationMirror (javax.lang.model.element.AnnotationMirror)151 Test (org.junit.Test)136 HashMap (java.util.HashMap)118 HashSet (java.util.HashSet)109 Elements (javax.lang.model.util.Elements)101 Map (java.util.Map)98 IOException (java.io.IOException)97 List (java.util.List)97 ClassName (com.squareup.javapoet.ClassName)88 Test (org.junit.jupiter.api.Test)80 MethodSpec (com.squareup.javapoet.MethodSpec)72 TypeSpec (com.squareup.javapoet.TypeSpec)63