List of usage examples for javax.lang.model.type DeclaredType getTypeArguments
List<? extends TypeMirror> getTypeArguments();
From source file:org.lambdamatic.mongodb.apt.template.TemplateType.java
/** * Returns the {@link TemplateType} for the given {@link VariableType}, or throws a * {@link MetadataGenerationException} if it was not a known or supported type. * /* w w w .j a v a 2 s.c o m*/ * @param variableType the variable to analyze * @return the corresponding {@link TemplateType} * @throws MetadataGenerationException if the given variable type is not supported */ private static TemplateType getMetadataFieldType(final TypeMirror variableType, final BiFunction<PrimitiveType, ProcessingEnvironment, TemplateType> primitiveTypeToTemplateType, final Function<DeclaredType, TemplateType> embeddedDocumentToTemplateType, final BiFunction<TypeMirror, ProcessingEnvironment, TemplateType> collectionToTemplateType, final BiFunction<DeclaredType, ProcessingEnvironment, TemplateType> mapToTemplateType, final Function<DeclaredType, TemplateType> declaredTypeToTemplateType, final ProcessingEnvironment processingEnv) throws MetadataGenerationException { if (variableType instanceof PrimitiveType) { return primitiveTypeToTemplateType.apply((PrimitiveType) variableType, processingEnv); } else if (variableType instanceof DeclaredType) { final DeclaredType declaredType = (DeclaredType) variableType; final TypeElement declaredElement = (TypeElement) declaredType.asElement(); if (declaredElement.getAnnotation(EmbeddedDocument.class) != null) { // embedded documents return embeddedDocumentToTemplateType.apply(declaredType); } else if (ElementUtils.isAssignable(declaredType, Collection.class)) { // collections (list/set) return collectionToTemplateType.apply(declaredType.getTypeArguments().get(0), processingEnv); } else if (ElementUtils.isAssignable(declaredType, Map.class)) { // map return mapToTemplateType.apply(declaredType, processingEnv); } else { return declaredTypeToTemplateType.apply(declaredType); } } else if (variableType.getKind() == TypeKind.ARRAY) { final TypeMirror componentType = ((ArrayType) variableType).getComponentType(); if (componentType instanceof PrimitiveType) { final PrimitiveType primitiveType = (PrimitiveType) componentType; final TypeElement boxedClass = processingEnv.getTypeUtils().boxedClass(primitiveType); return collectionToTemplateType.apply(boxedClass.asType(), processingEnv); } return collectionToTemplateType.apply(componentType, processingEnv); } throw new MetadataGenerationException("Unexpected variable type: " + variableType); }
From source file:io.github.jeddict.jpa.modeler.properties.convert.ConvertPanel.java
static void importAttributeConverter(String classHandle, AtomicBoolean validated, ModelerFile modelerFile) { if (StringUtils.isBlank(classHandle)) { validated.set(true);/*w w w. j a v a 2s . co m*/ return; } FileObject pkg = findSourceGroupForFile(modelerFile.getFileObject()).getRootFolder(); try { JavaSource javaSource = JavaSource.create(ClasspathInfo.create(pkg)); javaSource.runUserActionTask(controller -> { try { controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); TypeElement jc = controller.getElements().getTypeElement(classHandle); EntityMappings entityMappings = (EntityMappings) modelerFile.getDefinitionElement(); Optional<Converter> converter = entityMappings.findConverter(classHandle); if (jc != null) { DeclaredType attributeConverterType = null; if (!jc.getInterfaces().isEmpty()) { //fetch interface info for (TypeMirror interfaceType : jc.getInterfaces()) { if (interfaceType.getKind() == TypeKind.DECLARED && AttributeConverter.class .getName().equals(((DeclaredType) interfaceType).asElement().toString())) { attributeConverterType = (DeclaredType) interfaceType; } } } if (attributeConverterType != null && attributeConverterType.getTypeArguments().size() == 2) { TypeMirror attributeType = attributeConverterType.getTypeArguments().get(0); TypeMirror dbFieldType = attributeConverterType.getTypeArguments().get(1); if (!entityMappings.addConverter(classHandle, attributeType.toString(), dbFieldType.toString())) { message("MSG_ATTRIBUTE_CONVERTER_TYPE_CONFLICT", classHandle); } else { if (!converter.isPresent()) { message("MSG_ATTRIBUTE_CONVERTER_TYPE_REGISTERED", classHandle, attributeType.toString(), dbFieldType.toString()); } validated.set(true); } } else { message("MSG_ATTRIBUTE_CONVERTER_NOT_IMPLEMENTED", classHandle); } } else { if (converter.isPresent()) { validated.set(true); } else { message("MSG_ARTIFACT_NOT_FOUND", classHandle, pkg.getPath()); } } } catch (IOException t) { ExceptionUtils.printStackTrace(t); } }, true); } catch (IOException ex) { Exceptions.printStackTrace(ex); } }
From source file:org.netbeans.jpa.modeler.properties.convert.ConvertPanel.java
static void importAttributeConverter(String classHandle, AtomicBoolean validated, ModelerFile modelerFile) { if (StringUtils.isBlank(classHandle)) { validated.set(true);/*from www. j a v a 2s . c o m*/ return; } FileObject pkg = SourceGroupSupport.findSourceGroupForFile(modelerFile.getFileObject()).getRootFolder(); try { JavaSource javaSource = JavaSource.create(ClasspathInfo.create(pkg)); javaSource.runUserActionTask(controller -> { try { controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); TypeElement jc = controller.getElements().getTypeElement(classHandle); EntityMappings entityMappings = (EntityMappings) modelerFile.getDefinitionElement(); Optional<Converter> converter = entityMappings.findConverter(classHandle); if (jc != null) { DeclaredType attributeConverterType = null; if (!jc.getInterfaces().isEmpty()) { //fetch interface info for (TypeMirror interfaceType : jc.getInterfaces()) { if (interfaceType.getKind() == TypeKind.DECLARED && AttributeConverter.class .getName().equals(((DeclaredType) interfaceType).asElement().toString())) { attributeConverterType = (DeclaredType) interfaceType; } } } if (attributeConverterType != null && attributeConverterType.getTypeArguments().size() == 2) { TypeMirror attributeType = attributeConverterType.getTypeArguments().get(0); TypeMirror dbFieldType = attributeConverterType.getTypeArguments().get(1); if (!entityMappings.addConverter(classHandle, attributeType.toString(), dbFieldType.toString())) { message("MSG_ATTRIBUTE_CONVERTER_TYPE_CONFLICT", classHandle); } else { if (!converter.isPresent()) { message("MSG_ATTRIBUTE_CONVERTER_TYPE_REGISTERED", classHandle, attributeType.toString(), dbFieldType.toString()); } validated.set(true); } } else { message("MSG_ATTRIBUTE_CONVERTER_NOT_IMPLEMENTED", classHandle); } } else { if (converter.isPresent()) { validated.set(true); } else { message("MSG_ARTIFACT_NOT_FOUND", classHandle, pkg.getPath()); } } } catch (IOException t) { ExceptionUtils.printStackTrace(t); } }, true); } catch (IOException ex) { Exceptions.printStackTrace(ex); } }
From source file:android.databinding.tool.store.SetterStore.java
private static boolean hasTypeVar(TypeMirror typeMirror) { TypeKind kind = typeMirror.getKind(); if (kind == TypeKind.TYPEVAR) { return true; } else if (kind == TypeKind.ARRAY) { return hasTypeVar(((ArrayType) typeMirror).getComponentType()); } else if (kind == TypeKind.DECLARED) { DeclaredType declaredType = (DeclaredType) typeMirror; List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments(); if (typeArguments == null || typeArguments.isEmpty()) { return false; }/*from ww w . j av a2 s. c o m*/ for (TypeMirror arg : typeArguments) { if (hasTypeVar(arg)) { return true; } } return false; } else { return false; } }
From source file:org.androidtransfuse.adapter.element.ASTElementFactory.java
public ASTType buildASTElementType(DeclaredType declaredType) { ASTType astType = getType((TypeElement) declaredType.asElement()); if (!declaredType.getTypeArguments().isEmpty()) { return astFactory.buildGenericTypeWrapper(astType, astFactory.buildParameterBuilder(declaredType)); }/*w ww. j a v a 2 s .c om*/ return astType; }
From source file:com.contentful.vault.compiler.Processor.java
private boolean isSubtypeOfType(TypeMirror typeMirror, String otherType) { if (otherType.equals(typeMirror.toString())) { return true; }//from w w w.j av a2 s .co m if (!(typeMirror instanceof DeclaredType)) { return false; } DeclaredType declaredType = (DeclaredType) typeMirror; List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments(); if (typeArguments.size() > 0) { StringBuilder typeString = new StringBuilder(declaredType.asElement().toString()); typeString.append('<'); for (int i = 0; i < typeArguments.size(); i++) { if (i > 0) { typeString.append(','); } typeString.append('?'); } typeString.append('>'); if (typeString.toString().equals(otherType)) { return true; } } Element element = declaredType.asElement(); if (!(element instanceof TypeElement)) { return false; } TypeElement typeElement = (TypeElement) element; TypeMirror superType = typeElement.getSuperclass(); if (isSubtypeOfType(superType, otherType)) { return true; } for (TypeMirror interfaceType : typeElement.getInterfaces()) { if (isSubtypeOfType(interfaceType, otherType)) { return true; } } return false; }
From source file:org.mule.devkit.apt.model.AnnotationProcessorIdentifiable.java
@Override public List<Identifiable> getTypeArguments() { List<Identifiable> typeArguments = new ArrayList<Identifiable>(); DeclaredType declaredType = (DeclaredType) asType(); for (TypeMirror typeMirror : declaredType.getTypeArguments()) { if (typeMirror instanceof WildcardType || typeMirror instanceof TypeVariable) { continue; }/*from www .j av a 2 s .c om*/ Element element = types.asElement(typeMirror); if (element instanceof TypeElement) { typeArguments.add(new AnnotationProcessorType((TypeElement) element, types, elements, trees)); } else { typeArguments.add(new AnnotationProcessorIdentifiable(element, this, types, elements, trees)); } } return typeArguments; }
From source file:org.mule.devkit.apt.model.AnnotationProcessorIdentifiable.java
@Override public boolean isNestedProcessor() { if (asType().toString().startsWith(NestedProcessor.class.getName())) { return true; }//from w ww .j ava 2 s . co m if (asType().toString().startsWith(List.class.getName())) { DeclaredType variableType = (DeclaredType) asType(); List<? extends TypeMirror> variableTypeParameters = variableType.getTypeArguments(); if (variableTypeParameters.isEmpty()) { return false; } if (variableTypeParameters.get(0).toString().startsWith(NestedProcessor.class.getName())) { return true; } } return false; }
From source file:com.contentful.vault.compiler.Processor.java
private void parseContentType(TypeElement element, Map<TypeElement, ModelInjection> models) { String id = element.getAnnotation(ContentType.class).value(); if (id.isEmpty()) { error(element, "@%s id may not be empty. (%s)", ContentType.class.getSimpleName(), element.getQualifiedName()); return;//from w ww. ja va 2 s .c o m } if (!isSubtypeOfType(element.asType(), Resource.class.getName())) { error(element, "Classes annotated with @%s must extend \"" + Resource.class.getName() + "\". (%s)", ContentType.class.getSimpleName(), element.getQualifiedName()); return; } Set<FieldMeta> fields = new LinkedHashSet<>(); Set<String> memberIds = new LinkedHashSet<>(); for (Element enclosedElement : element.getEnclosedElements()) { Field field = enclosedElement.getAnnotation(Field.class); if (field == null) { continue; } String fieldId = field.value(); if (fieldId.isEmpty()) { fieldId = enclosedElement.getSimpleName().toString(); } Set<Modifier> modifiers = enclosedElement.getModifiers(); if (modifiers.contains(Modifier.STATIC)) { error(element, "@%s elements must not be static. (%s.%s)", Field.class.getSimpleName(), element.getQualifiedName(), enclosedElement.getSimpleName()); return; } if (modifiers.contains(Modifier.PRIVATE)) { error(element, "@%s elements must not be private. (%s.%s)", Field.class.getSimpleName(), element.getQualifiedName(), enclosedElement.getSimpleName()); return; } if (!memberIds.add(fieldId)) { error(element, "@%s for the same id (\"%s\") was used multiple times in the same class. (%s)", Field.class.getSimpleName(), fieldId, element.getQualifiedName()); return; } FieldMeta.Builder fieldBuilder = FieldMeta.builder(); if (isList(enclosedElement)) { DeclaredType declaredType = (DeclaredType) enclosedElement.asType(); List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments(); if (typeArguments.size() == 0) { error(element, "Array fields must have a type parameter specified. (%s.%s)", element.getQualifiedName(), enclosedElement.getSimpleName()); return; } TypeMirror arrayType = typeArguments.get(0); if (!isValidListType(arrayType)) { error(element, "Invalid list type \"%s\" specified. (%s.%s)", arrayType.toString(), element.getQualifiedName(), enclosedElement.getSimpleName()); return; } String sqliteType = null; if (String.class.getName().equals(arrayType.toString())) { sqliteType = SqliteUtils.typeForClass(List.class.getName()); } fieldBuilder.setSqliteType(sqliteType).setArrayType(arrayType.toString()); } else { TypeMirror enclosedType = enclosedElement.asType(); String linkType = getLinkType(enclosedType); String sqliteType = null; if (linkType == null) { sqliteType = SqliteUtils.typeForClass(enclosedType.toString()); if (sqliteType == null) { error(element, "@%s specified for unsupported type (\"%s\"). (%s.%s)", Field.class.getSimpleName(), enclosedType.toString(), element.getQualifiedName(), enclosedElement.getSimpleName()); return; } } fieldBuilder.setSqliteType(sqliteType).setLinkType(linkType); } fields.add(fieldBuilder.setId(fieldId).setName(enclosedElement.getSimpleName().toString()) .setType(enclosedElement.asType()).build()); } if (fields.size() == 0) { error(element, "Model must contain at least one @%s element. (%s)", Field.class.getSimpleName(), element.getQualifiedName()); return; } ClassName injectionClassName = getInjectionClassName(element, SUFFIX_MODEL); String tableName = "entry_" + SqliteUtils.hashForId(id); models.put(element, new ModelInjection(id, injectionClassName, element, tableName, fields)); }
From source file:com.spotify.docgenerator.JacksonJerseyAnnotationProcessor.java
/** * Make a {@link TypeDescriptor} by examining the {@link TypeMirror} and recursively looking * at the generic arguments to the type (if they exist). *///from w ww . jav a 2s. c o m private TypeDescriptor makeTypeDescriptor(final TypeMirror type) { if (type.getKind() != TypeKind.DECLARED) { return new TypeDescriptor(type.toString(), ImmutableList.<TypeDescriptor>of()); } final DeclaredType dt = (DeclaredType) type; final String plainType = processingEnv.getTypeUtils().erasure(type).toString(); final List<TypeDescriptor> typeArgumentsList = Lists.newArrayList(); final List<? extends TypeMirror> typeArguments = dt.getTypeArguments(); for (final TypeMirror arg : typeArguments) { typeArgumentsList.add(makeTypeDescriptor(arg)); } return new TypeDescriptor(plainType, typeArgumentsList); }