Search in sources :

Example 76 with TypeMirror

use of javax.lang.model.type.TypeMirror in project camel by apache.

the class CoreEipAnnotationProcessor method processRefWhenClauses.

/**
     * Special for processing an @XmlElementRef when field
     */
private void processRefWhenClauses(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement originalClassType, XmlElementRef elementRef, VariableElement fieldElement, String fieldName, Set<EipOption> eipOptions, String prefix) {
    Elements elementUtils = processingEnv.getElementUtils();
    if ("whenClauses".equals(fieldName)) {
        String kind = "element";
        String name = elementRef.name();
        if (isNullOrEmpty(name) || "##default".equals(name)) {
            name = fieldName;
        }
        name = prefix + name;
        TypeMirror fieldType = fieldElement.asType();
        String fieldTypeName = fieldType.toString();
        // find javadoc from original class as it will override the setExpression method where we can provide the javadoc for the given EIP
        String docComment = findJavaDoc(elementUtils, fieldElement, fieldName, name, originalClassType, true);
        boolean deprecated = fieldElement.getAnnotation(Deprecated.class) != null;
        // indicate that this element is one of when
        Set<String> oneOfTypes = new HashSet<String>();
        oneOfTypes.add("when");
        // when is predicate
        boolean asPredicate = true;
        String displayName = null;
        Metadata metadata = fieldElement.getAnnotation(Metadata.class);
        if (metadata != null) {
            displayName = metadata.displayName();
        }
        EipOption ep = new EipOption(name, displayName, kind, fieldTypeName, false, "", docComment, deprecated, false, null, true, oneOfTypes, asPredicate);
        eipOptions.add(ep);
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) Metadata(org.apache.camel.spi.Metadata) Elements(javax.lang.model.util.Elements) XmlElements(javax.xml.bind.annotation.XmlElements) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 77 with TypeMirror

use of javax.lang.model.type.TypeMirror in project robolectric by robolectric.

the class RobolectricModel method getImplementedClass.

public TypeMirror getImplementedClass(AnnotationMirror am) {
    if (am == null) {
        return null;
    }
    // RobolectricWiringTest prefers className (if provided) to value, so we do the same here.
    TypeMirror impType = getImplementedClassName(am);
    if (impType != null) {
        return impType;
    }
    AnnotationValue av = getAnnotationValue(am, "value");
    if (av == null) {
        return null;
    }
    TypeMirror type = valueVisitor.visit(av);
    if (type == null) {
        return null;
    }
    // If the class is Robolectric.Anything, treat as if it wasn't specified at all.
    if (ANYTHING_MIRROR != null && types.isSameType(type, ANYTHING_MIRROR)) {
        return null;
    }
    return type;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) AnnotationValue(javax.lang.model.element.AnnotationValue)

Example 78 with TypeMirror

use of javax.lang.model.type.TypeMirror in project robolectric by robolectric.

the class RobolectricModel method appendParameterList.

public void appendParameterList(StringBuilder message, List<? extends TypeParameterElement> tpeList) {
    boolean first = true;
    for (TypeParameterElement tpe : tpeList) {
        if (first) {
            first = false;
        } else {
            message.append(',');
        }
        message.append(tpe.toString());
        boolean iFirst = true;
        for (TypeMirror bound : getExplicitBounds(tpe)) {
            if (iFirst) {
                message.append(" extends ");
                iFirst = false;
            } else {
                message.append(',');
            }
            message.append(bound);
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeParameterElement(javax.lang.model.element.TypeParameterElement)

Example 79 with TypeMirror

use of javax.lang.model.type.TypeMirror in project qpid-broker-j by apache.

the class ConfiguredObjectFactoryGenerator method generateAccessCheckedMethods.

private void generateAccessCheckedMethods(final String className, final TypeElement typeElement, final PrintWriter pw, final HashSet<TypeElement> processedClasses, final HashSet<String> processedMethods) throws ClassNotFoundException {
    if (processedClasses.add(typeElement)) {
        Element superClassElement = processingEnv.getTypeUtils().asElement(typeElement.getSuperclass());
        if (superClassElement instanceof TypeElement) {
            generateAccessCheckedMethods(className, (TypeElement) superClassElement, pw, processedClasses, processedMethods);
        }
        for (TypeMirror ifMirror : typeElement.getInterfaces()) {
            Element ifElement = processingEnv.getTypeUtils().asElement(ifMirror);
            if (ifElement instanceof TypeElement) {
                generateAccessCheckedMethods(className, (TypeElement) ifElement, pw, processedClasses, processedMethods);
            }
        }
        for (Element element : typeElement.getEnclosedElements()) {
            if (element instanceof ExecutableElement && element.getKind() == ElementKind.METHOD && !processedMethods.contains(element.getSimpleName().toString())) {
                for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
                    if (annotationMirror.getAnnotationType().toString().equals("org.apache.qpid.server.model.ManagedOperation")) {
                        processedMethods.add(element.getSimpleName().toString());
                        processManagedOperation(pw, className, (ExecutableElement) element, annotationMirror);
                        break;
                    } else if (annotationMirror.getAnnotationType().toString().equals("org.apache.qpid.server.model.DoOnConfigThread")) {
                        processedMethods.add(element.getSimpleName().toString());
                        processDoOnConfigMethod(pw, className, (ExecutableElement) element, annotationMirror);
                        break;
                    }
                }
            }
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 80 with TypeMirror

use of javax.lang.model.type.TypeMirror in project qpid-broker-j by apache.

the class ConfiguredObjectFactoryGenerator method generateObjectFactory.

private String generateObjectFactory(final Filer filer, final ExecutableElement constructorElement) {
    TypeElement classElement = (TypeElement) constructorElement.getEnclosingElement();
    String objectQualifiedClassName = classElement.getQualifiedName().toString();
    String factoryName = objectQualifiedClassName + "Factory";
    String factorySimpleName = classElement.getSimpleName().toString() + "Factory";
    String objectSimpleName = classElement.getSimpleName().toString();
    processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Generating factory file for " + objectQualifiedClassName);
    final ManagedObjectFactoryConstructor annotation = constructorElement.getAnnotation(ManagedObjectFactoryConstructor.class);
    PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
    try {
        JavaFileObject factoryFile = filer.createSourceFile(factoryName);
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(factoryFile.openOutputStream(), "UTF-8"));
        pw.println("/*");
        for (String headerLine : License.LICENSE) {
            pw.println(" *" + headerLine);
        }
        pw.println(" */");
        pw.println();
        pw.print("package ");
        pw.print(packageElement.getQualifiedName());
        pw.println(";");
        pw.println();
        pw.println("import java.util.Map;");
        pw.println();
        pw.println("import org.apache.qpid.server.model.AbstractConfiguredObjectTypeFactory;");
        pw.println("import org.apache.qpid.server.model.ConfiguredObject;");
        pw.println("import org.apache.qpid.server.plugin.PluggableService;");
        if (annotation.conditionallyAvailable()) {
            pw.println("import org.apache.qpid.server.plugin.ConditionallyAvailable;");
        }
        pw.println();
        pw.println("@PluggableService");
        pw.println("public final class " + factorySimpleName + " extends AbstractConfiguredObjectTypeFactory<" + objectSimpleName + ">");
        if (annotation.conditionallyAvailable()) {
            pw.println("    implements ConditionallyAvailable");
        }
        pw.println("{");
        pw.println("    public " + factorySimpleName + "()");
        pw.println("    {");
        pw.println("        super(" + objectSimpleName + ".class);");
        pw.println("    }");
        pw.println();
        pw.println("    @Override");
        pw.println("    protected " + objectSimpleName + " createInstance(final Map<String, Object> attributes, final ConfiguredObject<?> parent)");
        pw.println("    {");
        pw.print("        return new " + objectSimpleName + "WithAccessChecking(attributes");
        boolean first = true;
        for (VariableElement param : constructorElement.getParameters()) {
            if (first) {
                first = false;
            } else {
                TypeMirror erasureType = processingEnv.getTypeUtils().erasure(param.asType());
                pw.print(String.format(", (%s)parent", erasureType.toString()));
            }
        }
        pw.println(");");
        pw.println("    }");
        if (annotation.conditionallyAvailable()) {
            final String condition = annotation.condition();
            pw.println();
            pw.println("    @Override");
            pw.println("    public boolean isAvailable()");
            pw.println("    {");
            if ("".equals(condition)) {
                pw.println("        return " + objectSimpleName + ".isAvailable();");
            } else {
                if (condition.matches("([\\w][\\w\\d_]+\\.)+[\\w][\\w\\d_\\$]*#[\\w\\d_]+\\s*\\(\\s*\\)")) {
                    pw.println("        return " + condition.replace('#', '.') + ";");
                } else {
                    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, String.format("Invalid condition expression for '%s' : %s", objectQualifiedClassName, condition));
                }
            }
            pw.println("    }");
        }
        pw.println("}");
        pw.close();
    } catch (IOException e) {
        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to write factory file: " + factoryName + " - " + e.getLocalizedMessage());
    }
    return factoryName;
}
Also used : JavaFileObject(javax.tools.JavaFileObject) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) OutputStreamWriter(java.io.OutputStreamWriter) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Aggregations

TypeMirror (javax.lang.model.type.TypeMirror)1086 TypeElement (javax.lang.model.element.TypeElement)419 ExecutableElement (javax.lang.model.element.ExecutableElement)259 VariableElement (javax.lang.model.element.VariableElement)237 Element (javax.lang.model.element.Element)196 DeclaredType (javax.lang.model.type.DeclaredType)177 ArrayList (java.util.ArrayList)154 Test (org.junit.Test)152 AnnotationMirror (javax.lang.model.element.AnnotationMirror)75 Types (javax.lang.model.util.Types)70 List (java.util.List)66 Elements (javax.lang.model.util.Elements)60 HashSet (java.util.HashSet)53 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)50 Map (java.util.Map)49 ExecutableType (javax.lang.model.type.ExecutableType)45 MethodSpec (com.squareup.javapoet.MethodSpec)44 PackageElement (javax.lang.model.element.PackageElement)41 ArrayType (javax.lang.model.type.ArrayType)41 Expression (com.google.devtools.j2objc.ast.Expression)39