Search in sources :

Example 66 with PackageElement

use of javax.lang.model.element.PackageElement in project kie-wb-common by kiegroup.

the class MainProcessor method processProperties.

private boolean processProperties(final Element e) {
    final boolean isClass = e.getKind() == ElementKind.CLASS;
    if (isClass) {
        TypeElement classElement = (TypeElement) e;
        PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
        String propertyClassName = packageElement.getQualifiedName().toString() + "." + classElement.getSimpleName();
        // Meta-properties
        Property metaProperty = e.getAnnotation(Property.class);
        if (null != metaProperty) {
            org.kie.workbench.common.stunner.core.definition.property.PropertyMetaTypes type = metaProperty.meta();
            if (!org.kie.workbench.common.stunner.core.definition.property.PropertyMetaTypes.NONE.equals(type)) {
                processingContext.getMetaPropertyTypes().put(type, propertyClassName + ".class");
            }
        }
        // Value fields.
        processFieldName(classElement, propertyClassName, ANNOTATION_PROPERTY_VALUE, processingContext.getPropertyAnnotations().getValueFieldNames(), true);
    }
    return false;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) MorphProperty(org.kie.workbench.common.stunner.core.definition.annotation.morph.MorphProperty) Property(org.kie.workbench.common.stunner.core.definition.annotation.Property)

Example 67 with PackageElement

use of javax.lang.model.element.PackageElement in project kie-wb-common by kiegroup.

the class MainProcessor method processCardinalityRules.

private boolean processCardinalityRules(final Element e) {
    final Messager messager = processingEnv.getMessager();
    final boolean isIface = e.getKind() == ElementKind.INTERFACE;
    final boolean isClass = e.getKind() == ElementKind.CLASS;
    if (isIface || isClass) {
        TypeElement classElement = (TypeElement) e;
        PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
        messager.printMessage(Diagnostic.Kind.NOTE, "Discovered cardinality rule for class [" + classElement.getSimpleName() + "]");
        final String classNameActivity = classElement.getSimpleName() + RULE_CARDINALITY_SUFFIX_CLASSNAME;
        generateRuleCode(cardinalityRuleGenerator, messager, classElement, packageElement, classNameActivity);
    }
    return true;
}
Also used : Messager(javax.annotation.processing.Messager) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement)

Example 68 with PackageElement

use of javax.lang.model.element.PackageElement in project kie-wb-common by kiegroup.

the class MainProcessor method processDockingRules.

private boolean processDockingRules(final Element e) {
    final Messager messager = processingEnv.getMessager();
    final boolean isIface = e.getKind() == ElementKind.INTERFACE;
    final boolean isClass = e.getKind() == ElementKind.CLASS;
    if (isIface || isClass) {
        TypeElement classElement = (TypeElement) e;
        PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
        messager.printMessage(Diagnostic.Kind.NOTE, "Discovered docking rule for class [" + classElement.getSimpleName() + "]");
        final String classNameActivity = classElement.getSimpleName() + RULE_DOCKING_SUFFIX_CLASSNAME;
        generateRuleCode(dockingRuleGenerator, messager, classElement, packageElement, classNameActivity);
    }
    return true;
}
Also used : Messager(javax.annotation.processing.Messager) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement)

Example 69 with PackageElement

use of javax.lang.model.element.PackageElement in project qpid-broker-j by apache.

the class ConfiguredObjectFactoryGenerator method generateAccessControlEnforcingSubclass.

private void generateAccessControlEnforcingSubclass(final Filer filer, final ExecutableElement constructorElement) {
    TypeElement classElement = (TypeElement) constructorElement.getEnclosingElement();
    String childClassName = classElement.getQualifiedName().toString() + "WithAccessChecking";
    String childClassSimpleName = classElement.getSimpleName().toString() + "WithAccessChecking";
    String objectSimpleName = classElement.getSimpleName().toString();
    PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
    try {
        JavaFileObject factoryFile = filer.createSourceFile(childClassName);
        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 static org.apache.qpid.server.security.access.Operation.INVOKE_METHOD;");
        pw.println();
        pw.println("import java.util.Map;");
        pw.println("import java.util.concurrent.ExecutionException;");
        pw.println();
        pw.println("import com.google.common.util.concurrent.Futures;");
        pw.println("import com.google.common.util.concurrent.ListenableFuture;");
        pw.println();
        pw.println("import org.apache.qpid.server.configuration.updater.Task;");
        pw.println("import org.apache.qpid.server.util.FixedKeyMapCreator;");
        pw.println();
        pw.println("final class " + childClassSimpleName + " extends " + objectSimpleName);
        pw.println("{");
        pw.print("    " + childClassSimpleName + "(final Map<String, Object> attributes");
        boolean first = true;
        for (VariableElement param : constructorElement.getParameters()) {
            if (first) {
                first = false;
            } else {
                pw.print(", final " + param.asType() + " " + createParamNameFromType(param));
            }
        }
        pw.println(")");
        pw.println("    {");
        pw.print("        super(attributes");
        first = true;
        for (VariableElement param : constructorElement.getParameters()) {
            if (first) {
                first = false;
            } else {
                pw.print(", " + createParamNameFromType(param));
            }
        }
        pw.println(");");
        pw.println("    }");
        pw.println();
        generateAccessCheckedMethods(childClassSimpleName, classElement, pw, new HashSet<TypeElement>(), new HashSet<String>());
        pw.println("}");
        pw.close();
    } catch (ClassNotFoundException | IOException e) {
        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to write file: " + childClassName + " - " + e.getLocalizedMessage());
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) IOException(java.io.IOException) JavaFileObject(javax.tools.JavaFileObject) OutputStreamWriter(java.io.OutputStreamWriter) PackageElement(javax.lang.model.element.PackageElement) PrintWriter(java.io.PrintWriter)

Example 70 with PackageElement

use of javax.lang.model.element.PackageElement 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

PackageElement (javax.lang.model.element.PackageElement)134 TypeElement (javax.lang.model.element.TypeElement)93 Element (javax.lang.model.element.Element)52 ExecutableElement (javax.lang.model.element.ExecutableElement)34 VariableElement (javax.lang.model.element.VariableElement)32 TypeSpec (com.squareup.javapoet.TypeSpec)20 IOException (java.io.IOException)17 HashSet (java.util.HashSet)13 TypeMirror (javax.lang.model.type.TypeMirror)13 AnnotationMirror (javax.lang.model.element.AnnotationMirror)12 JavaFileObject (javax.tools.JavaFileObject)12 ClassName (com.squareup.javapoet.ClassName)11 ArrayList (java.util.ArrayList)11 Set (java.util.Set)10 Elements (javax.lang.model.util.Elements)10 PrintWriter (java.io.PrintWriter)9 HashMap (java.util.HashMap)9 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)8 Map (java.util.Map)8 Messager (javax.annotation.processing.Messager)8