List of usage examples for javax.lang.model.type TypeMirror toString
String toString();
From source file:com.mastfrog.parameters.processor.Processor.java
private AnnotationMirror findMirror(Element el) { for (AnnotationMirror mir : el.getAnnotationMirrors()) { TypeMirror type = mir.getAnnotationType().asElement().asType(); if (Params.class.getName().equals(type.toString())) { return mir; }// w w w .j a v a 2s . co m } return null; }
From source file:org.versly.rest.wsdoc.AnnotationProcessor.java
/** * This is Spring-MVC only -- JAX-RS doesn't have an (obvious) analog. *//*from w w w . j av a 2 s . c o m*/ private void scanForSpringMVCMultipart(ExecutableElement executableElement, RestDocumentation.RestApi.Resource.Method doc) { for (VariableElement var : executableElement.getParameters()) { TypeMirror varType = var.asType(); if (varType.toString().startsWith(MultipartHttpServletRequest.class.getName())) { doc.setMultipartRequest(true); return; } } }
From source file:com.mastfrog.parameters.processor.Processor.java
private void checkConstructor(TypeElement el, GeneratedParamsClass inf) { Elements elements = processingEnv.getElementUtils(); TypeElement pageParamsType = elements .getTypeElement("org.apache.wicket.request.mapper.parameter.PageParameters"); TypeElement customParamsType = elements.getTypeElement(inf.qualifiedName()); boolean found = false; boolean foundArgument = false; ExecutableElement con = null; outer: for (Element sub : el.getEnclosedElements()) { switch (sub.getKind()) { case CONSTRUCTOR: for (AnnotationMirror mir : sub.getAnnotationMirrors()) { DeclaredType type = mir.getAnnotationType(); switch (type.toString()) { case "javax.inject.Inject": case "com.google.inject.Inject": ExecutableElement constructor = (ExecutableElement) sub; con = constructor;/*from ww w . ja va 2 s.c o m*/ for (VariableElement va : constructor.getParameters()) { TypeMirror varType = va.asType(); if (pageParamsType != null && varType.toString().equals(pageParamsType.toString())) { foundArgument = true; break; } if (customParamsType != null && varType.toString().equals(customParamsType.toString())) { foundArgument = true; break; } else if (customParamsType == null && inf.qualifiedName().equals(varType.toString())) { //first compilation - type not generated yet foundArgument = true; break; } } found = true; break outer; default: //do nothing } } } } if (!found) { processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Usually a constructor " + "annotated with @Inject that takes a " + inf.className + " is desired", el); } else if (found && !foundArgument) { processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Usually a constructor taking " + "an argument of " + inf.className + " is desired", con); } }
From source file:org.versly.rest.wsdoc.AnnotationProcessor.java
private boolean isJsonPrimitive(TypeMirror typeMirror) { return (typeMirror.getKind().isPrimitive() || JsonPrimitive.isPrimitive(typeMirror.toString())); }
From source file:org.androidtransfuse.analysis.ServiceAnalysis.java
private InjectionNodeBuilderRepository buildVariableBuilderMap(TypeMirror type) { InjectionNodeBuilderRepository injectionNodeBuilderRepository = injectionNodeRepositoryProvider.get(); injectionNodeBuilderRepository.putType(Context.class, injectionBindingBuilder.buildThis(Context.class)); injectionNodeBuilderRepository.putType(Application.class, injectionBindingBuilder.dependency(Context.class) .invoke(Application.class, "getApplication").build()); injectionNodeBuilderRepository.putType(android.app.Service.class, injectionBindingBuilder.buildThis(android.app.Service.class)); injectionNodeBuilderRepository.putType(ContextScopeHolder.class, injectionBindingBuilder.buildThis(ContextScopeHolder.class)); if (type != null && !type.toString().equals(android.app.Service.class.getName())) { ASTType serviceASTType = type.accept(astTypeBuilderVisitor, null); injectionNodeBuilderRepository.putType(serviceASTType, injectionBindingBuilder.buildThis(serviceASTType)); }/* w ww. ja va 2 s . c o m*/ injectionNodeBuilderRepository .addRepository(injectionNodeBuilderRepositoryFactory.buildApplicationInjections()); injectionNodeBuilderRepository .addRepository(injectionNodeBuilderRepositoryFactory.buildModuleConfiguration()); return injectionNodeBuilderRepository; }
From source file:com.dspot.declex.server.ServerModelHandler.java
private void getFieldsAndMethods(TypeElement element, Map<String, String> fields, Map<String, String> methods) { List<? extends Element> elems = element.getEnclosedElements(); for (Element elem : elems) { final String elemName = elem.getSimpleName().toString(); final String elemType = elem.asType().toString(); //Static methods are included if (methods != null) { if (elem.getKind() == ElementKind.METHOD) { if (elem.getModifiers().contains(Modifier.PRIVATE)) continue; ExecutableElement executableElement = (ExecutableElement) elem; //One parameter means that the method can be used to update the model if (executableElement.getParameters().size() == 1) { VariableElement param = executableElement.getParameters().get(0); methods.put(elemName, TypeUtils.typeFromTypeString(param.asType().toString(), getEnvironment())); }/*from ww w. j a v a 2 s. c om*/ } } if (elem.getModifiers().contains(Modifier.STATIC)) continue; if (fields != null) { if (elem.getKind() == ElementKind.FIELD) { if (elem.getModifiers().contains(Modifier.PRIVATE)) continue; fields.put(elemName, TypeUtils.typeFromTypeString(elemType, getEnvironment())); } } } //Apply to Extensions List<? extends TypeMirror> superTypes = getProcessingEnvironment().getTypeUtils() .directSupertypes(element.asType()); for (TypeMirror type : superTypes) { TypeElement superElement = getProcessingEnvironment().getElementUtils().getTypeElement(type.toString()); if (superElement.getAnnotation(Extension.class) != null) { getFieldsAndMethods(superElement, fields, methods); } break; } }
From source file:org.androidtransfuse.analysis.ActivityAnalysis.java
private InjectionNodeBuilderRepository buildVariableBuilderMap(TypeMirror activityType) { InjectionNodeBuilderRepository injectionNodeBuilderRepository = injectionNodeBuilderRepositoryProvider .get();//from w w w. jav a2s . c o m injectionNodeBuilderRepository.putType(Context.class, injectionBindingBuilder.buildThis(Context.class)); injectionNodeBuilderRepository.putType(Application.class, injectionBindingBuilder.dependency(Context.class) .invoke(Application.class, "getApplication").build()); injectionNodeBuilderRepository.putType(android.app.Activity.class, injectionBindingBuilder.buildThis(android.app.Activity.class)); injectionNodeBuilderRepository.putType(ContextScopeHolder.class, injectionBindingBuilder.buildThis(ContextScopeHolder.class)); if (activityType != null && !activityType.toString().equals(android.app.Activity.class.getName())) { ASTType activityASTType = activityType.accept(astTypeBuilderVisitor, null); injectionNodeBuilderRepository.putType(activityASTType, injectionBindingBuilder.buildThis(activityASTType)); } injectionNodeBuilderRepository.putAnnotation(Extra.class, extraInjectionNodeBuilder); injectionNodeBuilderRepository.putAnnotation(Resource.class, resourceInjectionNodeBuilder); injectionNodeBuilderRepository.putAnnotation(SystemService.class, systemServiceBindingInjectionNodeBuilder); injectionNodeBuilderRepository.putAnnotation(Preference.class, preferenceInjectionNodeBuilder); injectionNodeBuilderRepository.putAnnotation(View.class, viewVariableBuilder); injectionNodeBuilderRepository .addRepository(injectionNodeBuilderRepositoryFactory.buildApplicationInjections()); injectionNodeBuilderRepository .addRepository(injectionNodeBuilderRepositoryFactory.buildModuleConfiguration()); return injectionNodeBuilderRepository; }
From source file:org.versly.rest.wsdoc.AnnotationProcessor.java
private JsonType jsonTypeFromTypeMirror(TypeMirror typeMirror, Collection<String> typeRecursionGuard) { JsonType type;//from w w w .j a va 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.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 ww . j a va2 s . co 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.googlecode.androidannotations.helper.ValidatorHelper.java
public void hasCorrectDefaultAnnotation(ExecutableElement method) { checkDefaultAnnotation(method, DefaultBoolean.class, "boolean", new TypeKindAnnotationCondition(TypeKind.BOOLEAN)); checkDefaultAnnotation(method, DefaultFloat.class, "float", new TypeKindAnnotationCondition(TypeKind.FLOAT)); checkDefaultAnnotation(method, DefaultInt.class, "int", new TypeKindAnnotationCondition(TypeKind.INT)); checkDefaultAnnotation(method, DefaultLong.class, "long", new TypeKindAnnotationCondition(TypeKind.LONG)); checkDefaultAnnotation(method, DefaultString.class, "String", new DefaultAnnotationCondition() { @Override//w w w .j ava2 s. c om public boolean correctReturnType(TypeMirror returnType) { return returnType.toString().equals(CanonicalNameConstants.STRING); } }); }