List of usage examples for javax.lang.model.type DeclaredType getTypeArguments
List<? extends TypeMirror> getTypeArguments();
From source file:io.soabase.halva.processor.caseclass.Templates.java
private void addClassTupleMethods(CaseClassSpec spec, TypeSpec.Builder builder, ClassName className, Optional<List<TypeVariableName>> typeVariableNames) { Optional<Class<? extends Tuple>> optionalTupleClass = Tuple.getTupleClass(spec.getItems().size()); if (!optionalTupleClass.isPresent()) { return;//from w ww .j av a 2 s. co m } ClassName anyClassName = ClassName.get(Any.class); ClassName matchClassName = ClassName.get(AnyVal.class); ClassName anyClassTupleName = ClassName.get(AnyClassTuple.class); TypeName localCaseClassName = getLocalCaseClassName(className, typeVariableNames); TypeName classTupleClassName = ParameterizedTypeName.get(anyClassTupleName, localCaseClassName); CodeBlock.Builder returnCode = CodeBlock.builder().add("return new $T($T.Tu(", classTupleClassName, Tuple.class); IntStream.range(0, spec.getItems().size()).forEach(i -> { CaseClassItem item = spec.getItems().get(i); if (i > 0) { returnCode.add(", "); } returnCode.add("$T.loose($L)", anyClassName, item.getName()); }); spec.getItems().forEach(item -> { }); returnCode.addStatement(")){}"); MethodSpec.Builder tupleMethod = MethodSpec.methodBuilder(getClassTupleMethodName(className)) .returns(classTupleClassName).addCode(returnCode.build()) .addModifiers(Modifier.PUBLIC, Modifier.STATIC); spec.getItems().forEach(item -> { TypeName mainType = null; if (item.getType().getKind() == TypeKind.DECLARED) { DeclaredType declaredType = (DeclaredType) item.getType(); List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments(); if (typeArguments.size() > 0) { TypeName[] typeNames = new TypeName[typeArguments.size()]; for (int i = 0; i < typeArguments.size(); ++i) { typeNames[i] = WildcardTypeName.subtypeOf(TypeName.get(typeArguments.get(i)).box()); } mainType = ParameterizedTypeName.get(ClassName.get((TypeElement) declaredType.asElement()), typeNames); } } if (mainType == null) { mainType = TypeName.get(item.getType()).box(); } TypeName wildcareType = WildcardTypeName.subtypeOf(mainType); ParameterizedTypeName type = ParameterizedTypeName.get(matchClassName, wildcareType); tupleMethod.addParameter(type, item.getName()); }); if (typeVariableNames.isPresent()) { tupleMethod.addTypeVariables(typeVariableNames.get()); } builder.addMethod(tupleMethod.build()); }
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);//from w w w.ja v a2s.c om 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:org.versly.rest.wsdoc.AnnotationProcessor.java
private TypeMirror unwrapReturnType(TypeMirror originalReturnType) { if (originalReturnType.getKind() != TypeKind.DECLARED) { return originalReturnType; }//from ww w .ja v a 2 s . c o m DeclaredType declaredType = (DeclaredType) originalReturnType; if (declaredType.getTypeArguments().size() == 0) { return originalReturnType; } TypeElement element = (TypeElement) declaredType.asElement(); // For Spring's Async Support if ("org.springframework.web.context.request.async.DeferredResult" .equalsIgnoreCase(element.getQualifiedName().toString())) { return declaredType.getTypeArguments().get(0); } // For Spring's Async Support if ("java.util.concurrent.Callable".equalsIgnoreCase(element.getQualifiedName().toString())) { return declaredType.getTypeArguments().get(0); } return originalReturnType; }
From source file:org.mule.devkit.module.generation.BeanDefinitionParserGenerator.java
private UpperBlockClosure generateParseArrayOrList(Block body, TypeMirror typeMirror, Variable listElement, Variable builder, String fieldName, Variable parserContext) { DeclaredType variableType = (DeclaredType) typeMirror; java.util.List<? extends TypeMirror> variableTypeParameters = variableType.getTypeArguments(); Variable listChilds = body.decl(ref(List.class).narrow(ref(org.w3c.dom.Element.class)), fieldName.replace("-", "") + "ListChilds", ExpressionFactory._null()); Conditional listElementNotNull = body._if(Op.ne(listElement, ExpressionFactory._null())); Invocation getElementRef = listElement.invoke("getAttribute").arg("ref"); Variable ref = listElementNotNull._then().decl(ref(String.class), fieldName.replace("-", "") + "Ref", getElementRef);//from www. j ava 2 s .c o m Conditional ifRef = listElementNotNull._then()._if(Op.cand(Op.ne(ref, ExpressionFactory._null()), Op.not(ref(StringUtils.class).staticInvoke("isBlank").arg(ref)))); ifRef._then().add(builder.invoke("addPropertyValue").arg(fieldName) .arg(ExpressionFactory._new(ref(RuntimeBeanReference.class)).arg(ref))); Variable managedList = ifRef._else().decl(ref(ManagedList.class), fieldName.replace("-", ""), ExpressionFactory._new(ref(ManagedList.class))); String childName = context.getNameUtils().uncamel(context.getNameUtils().singularize(fieldName)); ifRef._else().assign(listChilds, ref(DomUtils.class).staticInvoke("getChildElementsByTagName").arg(listElement).arg(childName)); Conditional listChildsNotNull = ifRef._else()._if(Op.ne(listChilds, ExpressionFactory._null())); ForEach forEach = listChildsNotNull._then().forEach(ref(org.w3c.dom.Element.class), fieldName.replace("-", "") + "Child", listChilds); Invocation getValueRef = forEach.var().invoke("getAttribute").arg("value-ref"); Variable valueRef = forEach.body().decl(ref(String.class), "valueRef", getValueRef); Conditional ifValueRef = forEach.body()._if(Op.cand(Op.ne(valueRef, ExpressionFactory._null()), Op.not(ref(StringUtils.class).staticInvoke("isBlank").arg(valueRef)))); ifValueRef._then().add(managedList.invoke("add") .arg(ExpressionFactory._new(ref(RuntimeBeanReference.class)).arg(valueRef))); if (variableTypeParameters.size() > 0 && context.getTypeMirrorUtils().isTransferObject(variableTypeParameters.get(0))) { Variable pojoBeanDefinitionBuilder = generateParseTransferObject(variableTypeParameters.get(0), ifValueRef._else(), forEach.var(), parserContext); ifValueRef._else() .add(managedList.invoke("add").arg(pojoBeanDefinitionBuilder.invoke("getBeanDefinition"))); } else if (variableTypeParameters.size() > 0 && context.getTypeMirrorUtils().isArrayOrList(variableTypeParameters.get(0))) { UpperBlockClosure subList = generateParseArrayOrList(ifValueRef._else(), variableTypeParameters.get(0), forEach.var(), builder, "inner-" + childName, parserContext); subList.getNotRefBlock().add(managedList.invoke("add").arg(subList.getManagedCollection())); } else if (variableTypeParameters.size() > 0 && context.getTypeMirrorUtils().isMap(variableTypeParameters.get(0))) { UpperBlockClosure subMap = generateParseMap(ifValueRef._else(), variableTypeParameters.get(0), forEach.var(), builder, "inner-" + childName, parserContext); subMap.getNotRefBlock().add(managedList.invoke("add").arg(subMap.getManagedCollection())); } else { ifValueRef._else().add(managedList.invoke("add").arg(forEach.var().invoke("getTextContent"))); } return new UpperBlockClosure(managedList, ifRef._else()); }
From source file:org.mule.devkit.module.generation.BeanDefinitionParserGenerator.java
private UpperBlockClosure generateParseMap(Block body, TypeMirror typeMirror, Variable listElement, Variable builder, String fieldName, Variable parserContext) { DeclaredType variableType = (DeclaredType) typeMirror; java.util.List<? extends TypeMirror> variableTypeParameters = variableType.getTypeArguments(); Variable listChilds = body.decl(ref(List.class).narrow(ref(org.w3c.dom.Element.class)), fieldName.replace("-", "") + "ListChilds", ExpressionFactory._null()); Conditional listElementNotNull = body._if(Op.ne(listElement, ExpressionFactory._null())); Invocation getElementRef = listElement.invoke("getAttribute").arg("ref"); Variable ref = listElementNotNull._then().decl(ref(String.class), fieldName.replace("-", "") + "Ref", getElementRef);//from ww w . j a v a 2 s .c o m Conditional ifRef = listElementNotNull._then()._if(Op.cand(Op.ne(ref, ExpressionFactory._null()), Op.not(ref(StringUtils.class).staticInvoke("isBlank").arg(ref)))); ifRef._then().add(builder.invoke("addPropertyValue").arg(fieldName) .arg(ExpressionFactory._new(ref(RuntimeBeanReference.class)).arg(ref))); Variable managedMap = ifRef._else().decl(ref(ManagedMap.class), fieldName.replace("-", ""), ExpressionFactory._new(ref(ManagedMap.class))); ifRef._else().assign(listChilds, ref(DomUtils.class).staticInvoke("getChildElementsByTagName").arg(listElement) .arg(context.getNameUtils().uncamel(context.getNameUtils().singularize(fieldName)))); String childName = context.getNameUtils().uncamel(context.getNameUtils().singularize(fieldName)); Conditional listChildsNotNull = ifRef._else()._if(Op.ne(listChilds, ExpressionFactory._null())); Conditional isListEmpty = listChildsNotNull._then() ._if(Op.eq(listChilds.invoke("size"), ExpressionFactory.lit(0))); isListEmpty._then().assign(listChilds, ref(DomUtils.class).staticInvoke("getChildElements").arg(listElement)); ForEach forEach = listChildsNotNull._then().forEach(ref(org.w3c.dom.Element.class), fieldName.replace("-", "") + "Child", listChilds); Invocation getValueRef = forEach.var().invoke("getAttribute").arg("value-ref"); Invocation getKeyRef = forEach.var().invoke("getAttribute").arg("key-ref"); Variable valueRef = forEach.body().decl(ref(String.class), fieldName.replace("-", "") + "ValueRef", getValueRef); Variable keyRef = forEach.body().decl(ref(String.class), fieldName.replace("-", "") + "KeyRef", getKeyRef); Variable valueObject = forEach.body().decl(ref(Object.class), "valueObject", ExpressionFactory._null()); Variable keyObject = forEach.body().decl(ref(Object.class), "keyObject", ExpressionFactory._null()); Conditional ifValueRef = forEach.body()._if(Op.cand(Op.ne(valueRef, ExpressionFactory._null()), Op.not(ref(StringUtils.class).staticInvoke("isBlank").arg(valueRef)))); ifValueRef._then().assign(valueObject, ExpressionFactory._new(ref(RuntimeBeanReference.class)).arg(valueRef)); if (variableTypeParameters.size() > 1 && context.getTypeMirrorUtils().isTransferObject(variableTypeParameters.get(1))) { Variable pojoBuilder = generateParseTransferObject(variableTypeParameters.get(1), ifValueRef._else(), forEach.var(), parserContext); ifValueRef._else().assign(valueObject, pojoBuilder.invoke("getBeanDefinition")); } else if (variableTypeParameters.size() > 1 && context.getTypeMirrorUtils().isArrayOrList(variableTypeParameters.get(1))) { UpperBlockClosure subList = generateParseArrayOrList(forEach.body(), variableTypeParameters.get(1), forEach.var(), builder, "inner-" + childName, parserContext); subList.getNotRefBlock().assign(valueObject, subList.getManagedCollection()); } else if (variableTypeParameters.size() > 1 && context.getTypeMirrorUtils().isMap(variableTypeParameters.get(1))) { UpperBlockClosure subMap = generateParseMap(forEach.body(), variableTypeParameters.get(1), forEach.var(), builder, "inner-" + childName, parserContext); subMap.getNotRefBlock().assign(valueObject, subMap.getManagedCollection()); } else { ifValueRef._else().assign(valueObject, forEach.var().invoke("getTextContent")); } Conditional ifKeyRef = forEach.body()._if(Op.cand(Op.ne(keyRef, ExpressionFactory._null()), Op.not(ref(StringUtils.class).staticInvoke("isBlank").arg(keyRef)))); ifKeyRef._then().assign(keyObject, ExpressionFactory._new(ref(RuntimeBeanReference.class)).arg(keyRef)); ifKeyRef._else().assign(keyObject, forEach.var().invoke("getAttribute").arg("key")); Conditional noKey = forEach.body() ._if(Op.cor(Op.eq(keyObject, ExpressionFactory._null()), Op.cand(Op._instanceof(keyObject, ref(String.class)), ref(StringUtils.class).staticInvoke("isBlank") .arg(ExpressionFactory.cast(ref(String.class), keyObject))))); noKey._then().assign(keyObject, forEach.var().invoke("getTagName")); forEach.body().add(managedMap.invoke("put").arg(keyObject).arg(valueObject)); return new UpperBlockClosure(managedMap, ifRef._else()); }
From source file:org.versly.rest.wsdoc.AnnotationProcessor.java
private JsonType jsonTypeFromTypeMirror(TypeMirror typeMirror, Collection<String> typeRecursionGuard) { JsonType type;/*from w w w .jav a 2s .co m*/ if (_memoizedTypeMirrors.containsKey(typeMirror)) { return _memoizedTypeMirrors.get(typeMirror); } if (isJsonPrimitive(typeMirror)) { type = new JsonPrimitive(typeMirror.toString()); } else if (typeMirror.getKind() == TypeKind.DECLARED) { // some sort of object... walk it DeclaredType declaredType = (DeclaredType) typeMirror; type = jsonTypeForDeclaredType(declaredType, declaredType.getTypeArguments(), typeRecursionGuard); } else if (typeMirror.getKind() == TypeKind.VOID) { type = null; } else if (typeMirror.getKind() == TypeKind.ARRAY) { TypeMirror componentType = ((ArrayType) typeMirror).getComponentType(); type = jsonTypeFromTypeMirror(componentType, typeRecursionGuard); } else if (typeMirror.getKind() == TypeKind.ERROR) { type = new JsonPrimitive("(unresolvable type)"); } else { throw new UnsupportedOperationException(typeMirror.toString()); } _memoizedTypeMirrors.put(typeMirror, type); return type; }
From source file:org.kie.workbench.common.forms.adf.processors.FormDefinitionsProcessor.java
private List<Map<String, String>> extracFormFields(TypeElement type, FieldPolicy policy, I18nSettings i18nSettings) throws Exception { final Elements elementUtils = processingEnv.getElementUtils(); Collection<FieldInfo> fieldInfos = extractFieldInfos(type, fieldElement -> { if (policy.equals(FieldPolicy.ALL)) { AnnotationMirror annotation = GeneratorUtils.getAnnotation(elementUtils, fieldElement, SkipFormField.class.getName()); if (annotation != null) { return false; }/*from w w w. ja v a2 s .c o m*/ } else { AnnotationMirror annotation = GeneratorUtils.getAnnotation(elementUtils, fieldElement, FormField.class.getName()); if (annotation == null) { return false; } } return true; }); List<Map<String, String>> elementsSettings = new ArrayList<>(); for (FieldInfo fieldInfo : fieldInfos) { if (fieldInfo.getter != null && fieldInfo.setter != null) { String fieldName = fieldInfo.fieldElement.getSimpleName().toString(); String fieldLabel = fieldName; String binding = fieldName; String methodName = "getFormElement_" + fieldName; Map<String, Object> elementContext = new HashMap<>(); boolean isList = false; org.kie.workbench.common.forms.model.TypeKind typeKind = org.kie.workbench.common.forms.model.TypeKind.BASE; boolean overrideI18nLabel = false; TypeMirror finalType = fieldInfo.fieldElement.asType(); String fieldModifier = ""; if (finalType instanceof DeclaredType) { Element finalTypeElement = processingEnv.getTypeUtils().asElement(finalType); if (finalTypeElement.getKind().equals(ElementKind.CLASS)) { FieldDefinition fieldDefinitionAnnotation = finalTypeElement .getAnnotation(FieldDefinition.class); if (fieldDefinitionAnnotation != null) { // Override the using the i18n mechanism if (fieldDefinitionAnnotation.labelMode().equals(LabelMode.OVERRIDE_I18N_KEY)) { Collection<FieldInfo> labelInfos = extractFieldInfos((TypeElement) finalTypeElement, fieldElement -> fieldElement.getAnnotation(FieldLabel.class) != null); if (labelInfos == null || labelInfos.size() != 1) { throw new Exception("Problem processing FieldDefinition [" + finalType + "]: it should have one field marked as @FieldLabel"); } FieldInfo labelInfo = labelInfos.iterator().next(); fieldLabel = finalType.toString() + i18nSettings.separator() + labelInfo.fieldElement.getSimpleName(); } Collection<FieldInfo> fieldValue = extractFieldInfos((TypeElement) 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(); binding += "." + valueInfo.getFieldElement().getSimpleName(); fieldModifier = fixClassName(finalType.toString()) + "_FieldStatusModifier"; finalType = valueInfo.getFieldElement().asType(); overrideI18nLabel = !fieldDefinitionAnnotation.labelMode() .equals(LabelMode.DONT_OVERRIDE); } else { FormDefinition formDefinitionAnnotation = finalTypeElement .getAnnotation(FormDefinition.class); if (formDefinitionAnnotation != null) { Collection<FieldInfo> labelInfos = extractFieldInfos((TypeElement) finalTypeElement, fieldElement -> fieldElement.getAnnotation(FieldLabel.class) != null); if (labelInfos != null & labelInfos.size() == 1) { FieldInfo labelInfo = labelInfos.iterator().next(); fieldLabel = finalType.toString() + i18nSettings.separator() + labelInfo.fieldElement.getSimpleName(); overrideI18nLabel = true; } typeKind = org.kie.workbench.common.forms.model.TypeKind.OBJECT; } } } DeclaredType fieldType = (DeclaredType) finalType; if (processingEnv.getTypeUtils().isAssignable(fieldType.asElement().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); typeKind = org.kie.workbench.common.forms.model.TypeKind.OBJECT; } else if (elementUtils.getTypeElement(finalType.toString()).getSuperclass().toString() .startsWith("java.lang.Enum")) { typeKind = org.kie.workbench.common.forms.model.TypeKind.ENUM; } } elementContext.put("formModel", type.getQualifiedName().toString()); elementContext.put("methodName", methodName); elementContext.put("fieldName", fieldName); elementContext.put("binding", binding); elementContext.put("type", typeKind.toString()); elementContext.put("className", finalType.toString()); elementContext.put("isList", String.valueOf(isList)); elementContext.put("fieldModifier", fieldModifier); Map<String, String> params = new HashMap<>(); elementContext.put("params", params); String afterElement = ""; FormField settings = fieldInfo.fieldElement.getAnnotation(FormField.class); if (settings != null) { String typeName; try { typeName = settings.type().getName(); } catch (MirroredTypeException exception) { typeName = exception.getTypeMirror().toString(); } if (StringUtils.isEmpty(typeName)) { typeName = FieldType.class.getName(); } afterElement = settings.afterElement(); elementContext.put("preferredType", typeName); if (!overrideI18nLabel) { fieldLabel = settings.labelKey(); } elementContext.put("required", Boolean.valueOf(settings.required()).toString()); elementContext.put("readOnly", Boolean.valueOf(settings.readonly()).toString()); for (FieldParam fieldParam : settings.settings()) { params.put(fieldParam.name(), fieldParam.value()); } elementContext.put("wrap", Boolean.valueOf(settings.layoutSettings().wrap()).toString()); elementContext.put("horizontalSpan", String.valueOf(settings.layoutSettings().horizontalSpan())); elementContext.put("verticalSpan", String.valueOf(settings.layoutSettings().verticalSpan())); } else { elementContext.put("preferredType", FieldType.class.getName()); elementContext.put("required", Boolean.FALSE.toString()); elementContext.put("readOnly", Boolean.FALSE.toString()); elementContext.put("wrap", Boolean.FALSE.toString()); elementContext.put("horizontalSpan", "1"); elementContext.put("verticalSpan", "1"); } if (!overrideI18nLabel) { if (!StringUtils.isEmpty(i18nSettings.keyPreffix())) { fieldLabel = i18nSettings.keyPreffix() + i18nSettings.separator() + fieldLabel; } } elementContext.put("labelKey", fieldLabel); elementContext.put("afterElement", afterElement); extractFieldExtraSettings(elementContext, fieldInfo.fieldElement); StringBuffer methodCode = writeTemplate("templates/FieldElement.ftl", elementContext); Map<String, String> fieldSettings = new HashMap<>(); fieldSettings.put("elementName", fieldName); fieldSettings.put("afterElement", afterElement); fieldSettings.put("methodName", methodName); fieldSettings.put("method", methodCode.toString()); elementsSettings.add(fieldSettings); } } return elementsSettings; }
From source file:com.googlecode.androidannotations.helper.ValidatorHelper.java
public void hasSetOfHttpMethodReturnType(ExecutableElement element, IsValid valid) { TypeMirror returnType = element.getReturnType(); String returnTypeString = returnType.toString(); if (!returnTypeString.equals("java.util.Set<org.springframework.http.HttpMethod>")) { valid.invalidate();//from w ww. ja v a2s . c o m annotationHelper.printAnnotationError(element, "%s annotated methods can only return a Set of HttpMethod, not " + returnTypeString); } else { DeclaredType declaredType = (DeclaredType) returnType; List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments(); if (typeArguments.size() != 1) { valid.invalidate(); annotationHelper.printAnnotationError(element, "%s annotated methods can only return a parameterized Set (with HttpMethod)"); } else { TypeMirror typeArgument = typeArguments.get(0); if (!typeArgument.toString().equals("org.springframework.http.HttpMethod")) { valid.invalidate(); annotationHelper.printAnnotationError(element, "%s annotated methods can only return a parameterized Set of HttpMethod, not " + typeArgument.toString()); } } } }
From source file:com.googlecode.androidannotations.helper.ValidatorHelper.java
public void returnTypeNotGenericUnlessResponseEntity(ExecutableElement element, IsValid valid) { TypeMirror returnType = element.getReturnType(); TypeKind returnKind = returnType.getKind(); if (returnKind == TypeKind.DECLARED) { DeclaredType declaredReturnType = (DeclaredType) returnType; if (!declaredReturnType.toString().startsWith("org.springframework.http.ResponseEntity<") && declaredReturnType.getTypeArguments().size() > 0) { valid.invalidate();//from ww w .j a va 2 s . co m annotationHelper.printAnnotationError(element, "%s annotated methods cannot return parameterized types, except for ResponseEntity"); } } }
From source file:org.androidannotations.helper.ValidatorHelper.java
public void extendsListOfView(Element element, IsValid valid) { DeclaredType elementType = (DeclaredType) element.asType(); List<? extends TypeMirror> elementTypeArguments = elementType.getTypeArguments(); TypeMirror viewType = annotationHelper.typeElementFromQualifiedName(CanonicalNameConstants.VIEW).asType(); if (!elementType.toString().equals(CanonicalNameConstants.LIST) && elementTypeArguments.size() == 1 && !annotationHelper.isSubtype(elementTypeArguments.get(0), viewType)) { valid.invalidate();//from ww w . jav a2 s . c om annotationHelper.printAnnotationError(element, "%s can only be used on a " + CanonicalNameConstants.LIST + " of elements extending " + CanonicalNameConstants.VIEW); } }