List of usage examples for javax.lang.model.element TypeElement getKind
ElementKind getKind();
From source file:cop.raml.processor.rest.SpringRestImpl.java
private static String getClassRequestPath(TypeElement classElement) { if (classElement == null || classElement.getKind() != ElementKind.CLASS) return null; RestController annotation = classElement.getAnnotation(RestController.class); return annotation != null && StringUtils.isNoneBlank(annotation.value()) ? annotation.value() : null; }
From source file:cop.raml.utils.Utils.java
public static String toEnumStr(String doc, VariableElement var) { doc = StringUtils.isBlank(doc) || EMPTY_ENUM.matcher(doc).matches() ? null : doc; if (doc != null || var == null) return doc; try {//from ww w.j a v a 2s .co m TypeElement typeElement = ElementUtils.asElement(var.asType()); if (typeElement.getKind() != ElementKind.ENUM) return null; return toEnumStr(typeElement.getEnclosedElements().stream() .filter(element -> element.getKind() == ElementKind.ENUM_CONSTANT) .map(element -> element.getSimpleName().toString()).toArray(String[]::new)); } catch (Exception ignored) { return null; } }
From source file:info.archinnov.achilles.internals.parser.validator.BeanValidator.java
public static void validateIsAConcreteNonFinalClass(AptUtils aptUtils, TypeElement typeElement) { final Name name = typeElement.getQualifiedName(); aptUtils.validateTrue(typeElement.getKind() == ElementKind.CLASS, "Bean type '%s' should be a class", name); final Set<Modifier> modifiers = typeElement.getModifiers(); aptUtils.validateFalse(modifiers.contains(Modifier.ABSTRACT), "Bean type '%s' should not be abstract", name);/*from ww w . j a v a2 s . co m*/ aptUtils.validateFalse(modifiers.contains(Modifier.FINAL), "Bean type '%s' should not be final", name); }
From source file:auto.parse.processor.AutoParseProcessor.java
private static boolean isJavaLangObject(TypeElement type) { return type.getSuperclass().getKind() == TypeKind.NONE && type.getKind() == ElementKind.CLASS; }
From source file:io.github.jeddict.jcode.util.JavaSourceHelper.java
public static boolean isInjectionTarget(CompilationController controller, TypeElement typeElement) { if (ElementKind.INTERFACE != typeElement.getKind()) { List<? extends AnnotationMirror> annotations = typeElement.getAnnotationMirrors(); boolean found = false; for (AnnotationMirror m : annotations) { Name qualifiedName = ((TypeElement) m.getAnnotationType().asElement()).getQualifiedName(); if (qualifiedName.contentEquals("javax.jws.WebService")) { //NOI18N found = true;/*w ww. j a va2 s . co m*/ break; } if (qualifiedName.contentEquals("javax.jws.WebServiceProvider")) { //NOI18N found = true; break; } } if (found) { return true; } } return false; }
From source file:fr.xebia.extras.selma.codegen.FactoryWrapper.java
/** * Generates the code to declare custom factory fields, setter and call default constructor in mapper constructor * * @param writer//from w w w.ja v a2s . co m * @param assign * @throws IOException */ public void emitFactoryFields(JavaWriter writer, boolean assign) throws IOException { for (TypeElement factoryField : factoryFields) { final String field = String.format(FACTORY_FIELD_TPL, factoryField.getSimpleName().toString()); if (assign) { if (factoryField.getKind() != ElementKind.INTERFACE) { TypeConstructorWrapper constructorWrapper = new TypeConstructorWrapper(context, factoryField); // assign the customMapper field to a newly created instance passing to it the declared source params writer.emitStatement("this.%s = new %s(%s)", field, factoryField.getQualifiedName().toString(), constructorWrapper.hasMatchingSourcesConstructor ? context.newParams() : ""); } } else { writer.emitEmptyLine(); writer.emitJavadoc("This field is used for custom Mapping"); if (ioC == IoC.SPRING) { writer.emitAnnotation("org.springframework.beans.factory.annotation.Autowired"); } writer.emitField(factoryField.asType().toString(), String.format(FACTORY_FIELD_TPL, factoryField.getSimpleName().toString()), EnumSet.of(PRIVATE)); writer.emitEmptyLine(); writer.emitJavadoc("Factory setter for " + field); EnumSet<Modifier> modifiers = EnumSet.of(PUBLIC, FINAL); if (ioC == IoC.CDI) { modifiers = EnumSet.of(PUBLIC); } writer.beginMethod("void", "setFactory" + factoryField.getSimpleName(), modifiers, factoryField.asType().toString(), "_factory"); writer.emitStatement("this.%s = _factory", field); writer.endMethod(); writer.emitEmptyLine(); } } }
From source file:fr.xebia.extras.selma.codegen.CustomMapperWrapper.java
void emitCustomMappersFields(JavaWriter writer, boolean assign) throws IOException { for (TypeElement customMapperField : customMapperFields) { final String field = String.format(CUSTOM_MAPPER_FIELD_TPL, customMapperField.getSimpleName().toString()); if (assign) { if (customMapperField.getKind() != ElementKind.INTERFACE && !(customMapperField.getKind() == ElementKind.CLASS && customMapperField.getModifiers().contains(ABSTRACT))) { TypeConstructorWrapper constructorWrapper = new TypeConstructorWrapper(context, customMapperField); // assign the customMapper field to a newly created instance passing to it the declared source params writer.emitStatement("this.%s = new %s(%s)", field, customMapperField.getQualifiedName().toString(), constructorWrapper.hasMatchingSourcesConstructor ? context.newParams() : ""); }/*from ww w.ja v a2s .c om*/ } else { writer.emitEmptyLine(); writer.emitJavadoc("This field is used for custom Mapping"); if (ioC == IoC.SPRING) { writer.emitAnnotation("org.springframework.beans.factory.annotation.Autowired"); } writer.emitField(customMapperField.asType().toString(), String.format(CUSTOM_MAPPER_FIELD_TPL, customMapperField.getSimpleName().toString()), EnumSet.of(PRIVATE)); writer.emitEmptyLine(); writer.emitJavadoc("Custom Mapper setter for " + field); EnumSet<Modifier> modifiers = EnumSet.of(PUBLIC, FINAL); if (ioC == IoC.CDI) { modifiers = EnumSet.of(PUBLIC); } writer.beginMethod("void", "setCustomMapper" + customMapperField.getSimpleName(), modifiers, customMapperField.asType().toString(), "mapper"); writer.emitStatement("this.%s = mapper", field); writer.endMethod(); writer.emitEmptyLine(); } } }
From source file:org.kie.workbench.common.forms.adf.processors.FormDefinitionGenerator.java
private List<FormDefinitionFieldData> extracFormFields(TypeElement type, FieldPolicy policy, I18nSettings i18nSettings, Map<String, String> defaultParams) throws Exception { final Types typeUtils = context.getProcessingEnvironment().getTypeUtils(); Collection<FieldInfo> fieldInfos = FormGenerationUtils.extractFieldInfos(type, fieldElement -> filter(fieldElement, policy)); List<FormDefinitionFieldData> fieldSettings = new ArrayList<>(); for (FieldInfo fieldInfo : fieldInfos) { if (fieldInfo.getSetter() != null && fieldInfo.getGetter() != null) { String fieldName = fieldInfo.getFieldElement().getSimpleName().toString(); FormDefinitionFieldData fieldData = new FormDefinitionFieldData(type.getQualifiedName().toString(), fieldName);// w w w.ja v a 2 s. c o m fieldData.setLabel(fieldName); fieldData.setBinding(fieldName); fieldData.setMethodName("getFormElement_" + fieldName); boolean isList = false; org.kie.workbench.common.forms.model.TypeKind typeKind = org.kie.workbench.common.forms.model.TypeKind.BASE; boolean overrideI18n = false; TypeMirror finalType = fieldInfo.getFieldElement().asType(); TypeElement finalTypeElement = (TypeElement) typeUtils.asElement(finalType); String fieldModifier = ""; if (finalTypeElement.getKind().equals(ElementKind.CLASS)) { FieldDefinition fieldDefinitionAnnotation = finalTypeElement .getAnnotation(FieldDefinition.class); if (fieldDefinitionAnnotation != null) { // Override the using the i18n mechanism if (fieldDefinitionAnnotation.i18nMode().equals(I18nMode.OVERRIDE_I18N_KEY)) { fieldData.setLabel(finalType.toString() + i18nSettings.separator() + fieldDefinitionAnnotation.labelKeySuffix()); Collection<FieldInfo> labelInfos = FormGenerationUtils.extractFieldInfos( finalTypeElement, fieldElement -> fieldElement.getAnnotation(FieldLabel.class) != null); if (labelInfos != null && labelInfos.size() == 1) { FieldInfo labelInfo = labelInfos.iterator().next(); fieldData.setLabel(finalType.toString() + i18nSettings.separator() + labelInfo.getFieldElement().getSimpleName()); } fieldData.setHelpMessage(finalType.toString() + i18nSettings.separator() + fieldDefinitionAnnotation.helpMessageKeySuffix()); Collection<FieldInfo> helpMessages = FormGenerationUtils.extractFieldInfos( finalTypeElement, fieldElement -> fieldElement.getAnnotation(FieldHelp.class) != null); if (helpMessages != null && helpMessages.size() == 1) { FieldInfo helpInfo = helpMessages.iterator().next(); fieldData.setHelpMessage(finalType.toString() + i18nSettings.separator() + helpInfo.getFieldElement().getSimpleName()); } } Collection<FieldInfo> fieldValue = FormGenerationUtils.extractFieldInfos(finalTypeElement, fieldElement -> fieldElement.getAnnotation(FieldValue.class) != null); if (fieldValue == null || fieldValue.size() != 1) { throw new Exception("Problem processing FieldDefinition [" + finalType + "]: it should have one field marked as @FieldValue"); } FieldInfo valueInfo = fieldValue.iterator().next(); fieldData.setBinding( fieldData.getBinding() + "." + valueInfo.getFieldElement().getSimpleName()); fieldModifier = FormGenerationUtils.fixClassName(finalType.toString()) + "_FieldStatusModifier"; finalType = valueInfo.getFieldElement().asType(); finalTypeElement = (TypeElement) typeUtils.asElement(finalType); overrideI18n = !fieldDefinitionAnnotation.i18nMode().equals(I18nMode.DONT_OVERRIDE); } else { FormDefinition formDefinitionAnnotation = finalTypeElement .getAnnotation(FormDefinition.class); if (formDefinitionAnnotation != null) { fieldData.setLabel( finalType.toString() + i18nSettings.separator() + FieldDefinition.LABEL); Collection<FieldInfo> labelInfos = FormGenerationUtils.extractFieldInfos( finalTypeElement, fieldElement -> fieldElement.getAnnotation(FieldLabel.class) != null); if (labelInfos != null && labelInfos.size() == 1) { FieldInfo labelInfo = labelInfos.iterator().next(); fieldData.setLabel(finalType.toString() + i18nSettings.separator() + labelInfo.getFieldElement().getSimpleName()); overrideI18n = true; } fieldData.setHelpMessage( finalType.toString() + i18nSettings.separator() + FieldDefinition.HELP_MESSAGE); Collection<FieldInfo> helpMessages = FormGenerationUtils.extractFieldInfos( finalTypeElement, fieldElement -> fieldElement.getAnnotation(FieldHelp.class) != null); if (helpMessages != null && helpMessages.size() == 1) { FieldInfo helpInfo = helpMessages.iterator().next(); fieldData.setHelpMessage(finalType.toString() + i18nSettings.separator() + helpInfo.getFieldElement().getSimpleName()); overrideI18n = true; } typeKind = org.kie.workbench.common.forms.model.TypeKind.OBJECT; } } } DeclaredType fieldType = (DeclaredType) finalType; if (typeUtils.isAssignable(finalTypeElement.asType(), listType)) { if (fieldType.getTypeArguments().size() != 1) { throw new IllegalArgumentException("Impossible to generate a field for type " + fieldType.toString() + ". Type should have one and only one Type arguments."); } isList = true; finalType = fieldType.getTypeArguments().get(0); finalTypeElement = (TypeElement) typeUtils.asElement(finalType); if (FormModelPropertiesUtil.isBaseType(finalTypeElement.getQualifiedName().toString())) { typeKind = org.kie.workbench.common.forms.model.TypeKind.BASE; } else if (typeUtils.isAssignable(finalTypeElement.asType(), enumType)) { typeKind = org.kie.workbench.common.forms.model.TypeKind.ENUM; } else { typeKind = org.kie.workbench.common.forms.model.TypeKind.OBJECT; } } else if (typeUtils.isAssignable(finalTypeElement.asType(), enumType)) { typeKind = org.kie.workbench.common.forms.model.TypeKind.ENUM; } fieldData.setType(typeKind.toString()); fieldData.setClassName(finalTypeElement.getQualifiedName().toString()); fieldData.setList(String.valueOf(isList)); fieldData.setFieldModifier(fieldModifier); fieldData.getParams().putAll(defaultParams); FormField settings = fieldInfo.getFieldElement().getAnnotation(FormField.class); if (settings != null) { try { fieldData.setPreferredType(settings.type().getName()); } catch (MirroredTypeException exception) { fieldData.setPreferredType(exception.getTypeMirror().toString()); } fieldData.setAfterElement(settings.afterElement()); if (!overrideI18n && !isEmpty(settings.labelKey())) { fieldData.setLabel(settings.labelKey()); } if (!overrideI18n && !isEmpty(settings.helpMessageKey())) { fieldData.setHelpMessage(settings.helpMessageKey()); } fieldData.setRequired(Boolean.valueOf(settings.required()).toString()); fieldData.setReadOnly(Boolean.valueOf(settings.readonly()).toString()); for (FieldParam fieldParam : settings.settings()) { fieldData.getParams().put(fieldParam.name(), fieldParam.value()); } fieldData.setWrap(Boolean.valueOf(settings.layoutSettings().wrap()).toString()); fieldData.setHorizontalSpan(String.valueOf(settings.layoutSettings().horizontalSpan())); fieldData.setVerticalSpan(String.valueOf(settings.layoutSettings().verticalSpan())); } if (!overrideI18n) { if (!isEmpty(i18nSettings.keyPreffix())) { fieldData.setLabel( i18nSettings.keyPreffix() + i18nSettings.separator() + fieldData.getLabel()); if (!isEmpty(fieldData.getHelpMessage())) { fieldData.setHelpMessage(i18nSettings.keyPreffix() + i18nSettings.separator() + fieldData.getHelpMessage()); } } } extractFieldExtraSettings(fieldData, fieldInfo.getFieldElement()); fieldSettings.add(fieldData); } } return fieldSettings; }
From source file:com.dspot.declex.action.Actions.java
private void createInformationForMethods(Element typeElement, ActionInfo actionInfo, List<String> methodsHandled) { if (methodsHandled == null) { methodsHandled = new LinkedList<>(); }//from ww w . j a va2 s . c o m for (Element elem : typeElement.getEnclosedElements()) { if (elem.getKind() == ElementKind.METHOD) { if (methodsHandled.contains(elem.toString())) continue; final ExecutableElement element = (ExecutableElement) elem; List<ActionMethodParam> params = new LinkedList<>(); for (VariableElement param : element.getParameters()) { List<Annotation> annotations = new LinkedList<>(); for (Class<? extends Annotation> annotation : ACTION_ANNOTATION) { Annotation containedAnnotation = param.getAnnotation(annotation); if (containedAnnotation != null) { annotations.add(containedAnnotation); } } final AbstractJClass paramType = codeModelHelper.elementTypeToJClass(param); ActionMethodParam actionMethodParam = new ActionMethodParam(param.getSimpleName().toString(), paramType, annotations); params.add(actionMethodParam); } List<Annotation> annotations = new LinkedList<>(); for (Class<? extends Annotation> annotation : ACTION_ANNOTATION) { Annotation containedAnnotation = element.getAnnotation(annotation); if (containedAnnotation != null) { annotations.add(containedAnnotation); } } String javaDoc = env.getProcessingEnvironment().getElementUtils().getDocComment(element); final String clazz = element.getReturnType().toString(); actionInfo.addMethod(element.getSimpleName().toString(), clazz, javaDoc, params, annotations); methodsHandled.add(element.toString()); } } List<? extends TypeMirror> superTypes = env.getProcessingEnvironment().getTypeUtils() .directSupertypes(typeElement.asType()); for (TypeMirror type : superTypes) { TypeElement superElement = env.getProcessingEnvironment().getElementUtils() .getTypeElement(type.toString()); if (superElement == null) continue; if (superElement.getKind().equals(ElementKind.INTERFACE)) continue; if (superElement.asType().toString().equals(Object.class.getCanonicalName())) continue; createInformationForMethods(superElement, actionInfo, methodsHandled); } }
From source file:com.dspot.declex.action.Actions.java
private void addActions(String actions) { if (!EXTERNAL_ACTIONS.contains(actions)) { TypeElement typeElement = env.getProcessingEnvironment().getElementUtils().getTypeElement(actions); CLASSES: for (Element element : typeElement.getEnclosedElements()) { if (element.getKind().isClass()) { List<? extends TypeMirror> superTypesForGate = env.getProcessingEnvironment().getTypeUtils() .directSupertypes(element.asType()); for (TypeMirror gate : superTypesForGate) { TypeElement superElementForGate = env.getProcessingEnvironment().getElementUtils() .getTypeElement(gate.toString()); if (superElementForGate == null) continue; if (superElementForGate.getKind().equals(ElementKind.INTERFACE)) continue; if (superElementForGate.asType().toString().equals(Object.class.getCanonicalName())) continue; //This is the Gate element, its parent it is the Holder List<? extends TypeMirror> superTypesForHolder = env.getProcessingEnvironment() .getTypeUtils().directSupertypes(superElementForGate.asType()); for (TypeMirror holder : superTypesForHolder) { TypeElement superElementForHolder = env.getProcessingEnvironment().getElementUtils() .getTypeElement(holder.toString()); if (superElementForHolder == null) continue; if (superElementForHolder.getKind().equals(ElementKind.INTERFACE)) continue; if (superElementForHolder.asType().toString().equals(Object.class.getCanonicalName())) continue; addActionHolder(superElementForHolder.asType().toString(), true); //This is the Holder element continue CLASSES; }// www . j a v a2s. c om } } } EXTERNAL_ACTIONS.add(actions); } }