List of usage examples for javax.lang.model.element TypeElement getSimpleName
@Override Name getSimpleName();
From source file:org.kie.workbench.common.stunner.core.processors.MainProcessor.java
private boolean processRuleExtension(final Element e) throws Exception { 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 rule extension for class [" + classElement.getSimpleName() + "]"); final String classNameActivity = classElement.getSimpleName() + RULE_EXTENSION_SUFFIX_CLASSNAME; generateRuleCode(extensionRuleGenerator, messager, classElement, packageElement, classNameActivity); }/*from ww w . j a v a 2 s . co m*/ return true; }
From source file:org.kie.workbench.common.stunner.core.processors.MainProcessor.java
private boolean processContainmentRules(final Element e) throws Exception { 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 containment rule for class [" + classElement.getSimpleName() + "]"); final String classNameActivity = classElement.getSimpleName() + RULE_CONTAINMENT_SUFFIX_CLASSNAME; generateRuleCode(containmentRuleGenerator, messager, classElement, packageElement, classNameActivity); }/*from www . j av a 2 s. c o m*/ return true; }
From source file:org.kie.workbench.common.stunner.core.processors.MainProcessor.java
private boolean processCardinalityRules(final Element e) throws Exception { 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); }/*from w ww . j ava 2 s. c o m*/ return true; }
From source file:org.kie.workbench.common.stunner.core.processors.MainProcessor.java
private boolean processPropertySets(final Set<? extends TypeElement> set, final Element e, final RoundEnvironment roundEnv) throws Exception { 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(); // Name fields. processFieldName(classElement, propertyClassName, ANNOTATION_NAME, processingContext.getPropertySetAnnotations().getNameFieldNames(), true); // Properties fields. Map<String, Element> propertyElements = getFieldNames(classElement, ANNOTATION_PROPERTY); if (null != propertyElements) { processingContext.getPropertyElements().addAll(propertyElements.values()); processingContext.getPropertySetAnnotations().getPropertiesFieldNames().put(propertyClassName, new LinkedHashSet<>(propertyElements.keySet())); if (propertyElements.isEmpty()) { note("Property Set for tye [" + propertyClassName + "] have no Property members."); }//from w ww . java2 s .c o m } } return false; }
From source file:org.kie.workbench.common.stunner.core.processors.MainProcessor.java
private boolean processConnectionRules(final Element element) throws Exception { final Messager messager = processingEnv.getMessager(); final boolean isIface = element.getKind() == ElementKind.INTERFACE; final boolean isClass = element.getKind() == ElementKind.CLASS; if (isIface || isClass) { TypeElement classElement = (TypeElement) element; PackageElement packageElement = (PackageElement) classElement.getEnclosingElement(); messager.printMessage(Diagnostic.Kind.NOTE, "Discovered connection rule for class [" + classElement.getSimpleName() + "]"); final String classNameActivity = classElement.getSimpleName() + RULE_CONNECTION_SUFFIX_CLASSNAME; generateRuleCode(connectionRuleGenerator, messager, classElement, packageElement, classNameActivity); }//from ww w .j av a2 s . co m return true; }
From source file:org.kie.workbench.common.stunner.core.processors.MainProcessor.java
private boolean processEdgeCardinalityRules(final Element e) throws Exception { 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 edge cardinality rule for class [" + classElement.getSimpleName() + "]"); final String classNameActivity = classElement.getSimpleName() + RULE_EDGE_CARDINALITY_SUFFIX_CLASSNAME; generateRuleCode(edgeCardinalityRuleGenerator, messager, classElement, packageElement, classNameActivity);/*from w ww . j a v a 2 s .c o m*/ } return true; }
From source file:org.kie.workbench.common.stunner.core.processors.MainProcessor.java
private boolean processProperties(final Set<? extends TypeElement> set, final Element e, final RoundEnvironment roundEnv) throws Exception { 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) { PropertyMetaTypes type = metaProperty.meta(); if (!PropertyMetaTypes.NONE.equals(type)) { processingContext.getMetaPropertyTypes().put(type, propertyClassName + ".class"); }//w w w . j av a 2s . c o m } // Value fields. processFieldName(classElement, propertyClassName, ANNOTATION_PROPERTY_VALUE, processingContext.getPropertyAnnotations().getValueFieldNames(), true); // Default Value fields. processFieldName(classElement, propertyClassName, ANNOTATION_PROPERTY_DEFAULT_VALUE, processingContext.getPropertyAnnotations().getDefaultValueFieldNames(), true); // Allowed Values fields. processFieldName(classElement, propertyClassName, ANNOTATION_PROPERTY_ALLOWED_VALUES, processingContext.getPropertyAnnotations().getAllowedValuesFieldNames(), false); // Caption fields. processFieldName(classElement, propertyClassName, ANNOTATION_PROPERTY_CAPTION, processingContext.getPropertyAnnotations().getCaptionFieldNames(), true); // Description fields. processFieldName(classElement, propertyClassName, ANNOTATION_DESCRIPTION, processingContext.getPropertyAnnotations().getDescriptionFieldNames(), true); // Type fields. processFieldName(classElement, propertyClassName, ANNOTATION_PROPERTY_TYPE, processingContext.getPropertyAnnotations().getTypeFieldNames(), true); // Read only fields. processFieldName(classElement, propertyClassName, ANNOTATION_PROPERTY_READONLY, processingContext.getPropertyAnnotations().getReadOnlyFieldNames(), true); // Optional fields. processFieldName(classElement, propertyClassName, ANNOTATION_PROPERTY_OPTIONAL, processingContext.getPropertyAnnotations().getOptionalFieldNames(), true); } return false; }
From source file:org.kie.workbench.common.stunner.core.processors.MainProcessor.java
private boolean processDefinitionSets(final Set<? extends TypeElement> set, final Element e, final RoundEnvironment roundEnv) throws Exception { final Messager messager = processingEnv.getMessager(); final boolean isClass = e.getKind() == ElementKind.CLASS; if (isClass) { TypeElement classElement = (TypeElement) e; PackageElement packageElement = (PackageElement) classElement.getEnclosingElement(); messager.printMessage(Diagnostic.Kind.NOTE, "Discovered definition set class [" + classElement.getSimpleName() + "]"); final String packageName = packageElement.getQualifiedName().toString(); final String className = classElement.getSimpleName().toString(); processingContext.setDefinitionSet(packageName, className); String defSetClassName = packageName + "." + className; // Description fields. processFieldName(classElement, defSetClassName, ANNOTATION_DESCRIPTION, processingContext.getDefSetAnnotations().getDescriptionFieldNames(), true); // Definitions identifiers. DefinitionSet definitionSetAnn = e.getAnnotation(DefinitionSet.class); List<? extends TypeMirror> mirrors = null; try {//from w w w . j av a2s . com Class<?>[] defsClasses = definitionSetAnn.definitions(); } catch (MirroredTypesException mte) { mirrors = mte.getTypeMirrors(); } if (null == mirrors) { throw new RuntimeException("No graph class class specifyed for the @DefinitionSet."); } Set<String> defIds = new LinkedHashSet<>(); for (TypeMirror mirror : mirrors) { if (mirror.getKind().equals(TypeKind.DECLARED)) { final TypeElement t = (TypeElement) ((DeclaredType) mirror).asElement(); processingContext.getDefinitionElements().add(t); } String fqcn = mirror.toString(); defIds.add(fqcn); } processingContext.getDefSetAnnotations().getDefinitionIds().addAll(defIds); // Builder class. processDefinitionSetModelBuilder(e, defSetClassName, processingContext.getDefSetAnnotations().getBuilderFieldNames()); // Graph factory type. TypeMirror mirror = null; try { Class<?> graphClass = definitionSetAnn.graphFactory(); } catch (MirroredTypeException mte) { mirror = mte.getTypeMirror(); } if (null == mirror) { throw new RuntimeException("No graph factory class specifyed for the @DefinitionSet."); } String fqcn = mirror.toString(); processingContext.getDefSetAnnotations().getGraphFactoryTypes().put(defSetClassName, fqcn); // Definition Set's qualifier. try { Class<?> qualifierClass = definitionSetAnn.qualifier(); } catch (MirroredTypeException mte) { mirror = mte.getTypeMirror(); } if (null == mirror) { throw new RuntimeException("No qualifier class specifyed for the @DefinitionSet."); } processingContext.getDefSetAnnotations().getQualifiers().put(defSetClassName, mirror.toString()); } return true; }
From source file:me.oriley.shiv.ShivProcessor.java
private void collectBindings(@NonNull RoundEnvironment env, @NonNull Map<TypeElement, BindingManager> bindings, @NonNull Class<? extends Annotation> annotation) throws ShivException { for (Element e : env.getElementsAnnotatedWith(annotation)) { if (e.getKind() != ElementKind.FIELD) { throw new ShivException( e.getSimpleName() + " is annotated with @" + annotation.getName() + " but is not a field"); }/*from w w w. j a v a 2 s . c om*/ TypeMirror fieldType = e.asType(); if (isPrivate(e)) { throw new ShivException("Field must not be private: " + e.getSimpleName()); } else if (isStatic(e)) { throw new ShivException("Field must not be static: " + e.getSimpleName()); } final TypeElement type = findEnclosingElement(e); // class should exist if (type == null) { throw new ShivException("Could not find a class for " + e.getSimpleName()); } // and it should be public if (isPrivate(type)) { throw new ShivException("Class is private: " + type); } // as well as all parent classes TypeElement parentType = findEnclosingElement(type); while (parentType != null) { if (isPrivate(parentType)) { throw new ShivException("Parent class is private: " + parentType); } parentType = findEnclosingElement(parentType); } if (annotation == BindView.class) { if (!isSubtypeOfType(type, Activity.class) && !isSubtypeOfType(type, Fragment.class) && !isSubtypeOfType(type, android.support.v4.app.Fragment.class) && !isSubtypeOfType(type, ViewGroup.class)) { throw new ShivException("Invalid view binding class: " + type.getSimpleName()); } else if (!isSubtypeOfType(fieldType, View.class)) { throw new ShivException("Field must inherit from View type: " + e.getSimpleName()); } } else if (annotation == BindExtra.class) { if (!isSubtypeOfType(type, Activity.class) && !isSubtypeOfType(type, Fragment.class) && !isSubtypeOfType(type, android.support.v4.app.Fragment.class)) { throw new ShivException("Invalid extra binding class: " + type.getSimpleName()); } else if (!isValidBundleEntry(fieldType)) { throw new ShivException("Extra field not suitable for bundle: " + e.getSimpleName()); } } else if (annotation == BindPreference.class) { if (isSubtypeOfType(type, PreferenceFragment.class) || isSubtypeOfType(type, PreferenceActivity.class)) { if (!isSubtypeOfType(fieldType, Preference.class)) { throw new ShivException("Preferences in " + type.getQualifiedName() + " must inherit from " + Preference.class + ": " + e.getSimpleName()); } } else if (isSubtypeOfType(type, PreferenceFragmentCompat.class)) { if (!isSubtypeOfType(fieldType, android.support.v7.preference.Preference.class)) { throw new ShivException("Preferences in " + PreferenceFragmentCompat.class + " must inherit from " + android.support.v7.preference.Preference.class + ": " + e.getSimpleName()); } } else { throw new ShivException("Invalid preference binding class: " + type.getSimpleName()); } } else if (annotation == BindInstance.class) { if (!isSubtypeOfType(type, Activity.class) && !isSubtypeOfType(type, Fragment.class) && !isSubtypeOfType(type, android.support.v4.app.Fragment.class)) { throw new ShivException("Invalid instance binding class: " + type.getSimpleName()); } else if (!isValidBundleEntry(fieldType)) { throw new ShivException("Instance field not suitable for bundle: " + e.getSimpleName()); } } else if (annotation == BindNonConfigurationInstance.class) { if (!isSubtypeOfType(type, Activity.class)) { throw new ShivException( "Invalid non-configuration instance binding class: " + type.getSimpleName()); } } else if (annotation == BindService.class) { if (!isSubtypeOfType(type, Activity.class) && !isSubtypeOfType(type, Fragment.class) && !isSubtypeOfType(type, android.support.v4.app.Fragment.class) && !isSubtypeOfType(type, View.class)) { throw new ShivException("Invalid service binding class: " + type.getSimpleName()); } } else { throw new ShivException("Unrecognised annotation: " + annotation); } BindingManager manager = bindings.get(type); if (manager == null) { manager = new BindingManager(this, type); bindings.put(type, manager); } manager.addBinding(annotation, e); } }
From source file:com.rgeldmacher.leash.LeashAnnotationProcessor.java
private void writeJavaFile(TypeElement classWithAnnotations, Set<Element> annotatedFields) throws IOException { TypeSpec retainedFragmentSpec = createRetainedFragmentSpec(classWithAnnotations, annotatedFields); ClassName retainedFragmentType = ClassName.bestGuess(retainedFragmentSpec.name); MethodSpec getRetainedFragmentMethodSpec = createGetRetainedFragmentMethodSpec(classWithAnnotations, retainedFragmentType);/* ww w . jav a 2 s . c o m*/ MethodSpec restoreMethodSpec = createRestoreMethodSpec(classWithAnnotations, annotatedFields, retainedFragmentType, getRetainedFragmentMethodSpec); MethodSpec retainMethodSpec = createRetainMethodSpec(classWithAnnotations, annotatedFields, retainedFragmentType, getRetainedFragmentMethodSpec); MethodSpec clearMethodSpec = createClearMethodSpec(classWithAnnotations, annotatedFields, retainedFragmentType, getRetainedFragmentMethodSpec); MethodSpec ctor = MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).build(); TypeSpec leash = TypeSpec.classBuilder(classWithAnnotations.getSimpleName() + Leash.SUFFIX) .addModifiers(Modifier.PUBLIC, Modifier.FINAL).addMethod(ctor).addMethod(restoreMethodSpec) .addMethod(retainMethodSpec).addMethod(clearMethodSpec).addMethod(getRetainedFragmentMethodSpec) .addType(retainedFragmentSpec).build(); JavaFile.builder(ClassName.get(classWithAnnotations).packageName(), leash).build().writeTo(filer); }