/** Returns a string for {@code type}. Primitive types are always boxed. */ public static TypeName injectableType(TypeMirror type) { return type.accept(new SimpleTypeVisitor6<TypeName, Void>() { @Override public TypeName visitPrimitive(PrimitiveType primitiveType, Void v) { return box(primitiveType); } @Override public TypeName visitError(ErrorType errorType, Void v) { // Error type found, a type may not yet have been generated, but we need the type // so we can generate the correct code in anticipation of the type being available // to the compiler. // Paramterized types which don't exist are returned as an error type whose name is "<any>" if ("<any>".equals(errorType.toString())) { throw new CodeGenerationIncompleteException( "Type reported as <any> is likely a not-yet generated parameterized type."); } return ClassName.bestGuess(errorType.toString()); } @Override protected TypeName defaultAction(TypeMirror typeMirror, Void v) { return TypeName.get(typeMirror); } }, null); }
Type get(TypeMirror type) { return type.accept(converter, parameters); }
static List<TypeMirror> listGenericTypeArguments(TypeMirror typeMirror) { final List<TypeMirror> typeArguments = new ArrayList<>(); typeMirror.accept(new SimpleTypeVisitor6<Void, Void>() { @Override public Void visitDeclared(DeclaredType declaredType, Void v) { if (!declaredType.getTypeArguments().isEmpty()) { typeArguments.addAll(declaredType.getTypeArguments()); } return null; } @Override protected Void defaultAction(TypeMirror typeMirror, Void v) { return null; } }, null); return typeArguments; }
@Override public String visitArray(ArrayType t, Builder builder) { return t.getComponentType().accept(this, builder) + "[]"; }
private TypeElement visitInterface(DeclaredType t) { if (t.getTypeArguments().isEmpty()) { return (TypeElement) t.asElement(); } else { int count = t.getTypeArguments().size(); if (t.asElement().toString().startsWith("java.util")) { return t.getTypeArguments().get(count - 1).accept(this, null); } else { return (TypeElement) t.asElement(); } } }
private List<Type.Defined> getBounds(Type.Parameters parameters, TypeParameterElement p) { List<Type.Defined> bounds = new ArrayList<>(); for (TypeMirror b : p.getBounds()) { bounds.add((Type.Defined) b.accept(converter, parameters)); } return bounds; }
@Override public String visitDeclared(DeclaredType t, Builder builder) { String baseType = qualifyType(state, builder, ((Type) t).tsym); if (t.getTypeArguments().isEmpty()) { return baseType; } StringBuilder b = new StringBuilder(baseType); b.append('<'); boolean started = false; for (TypeMirror arg : t.getTypeArguments()) { if (started) { b.append(','); } b.append(arg.accept(this, builder)); started = true; } b.append('>'); return b.toString(); } },
@SuppressWarnings("unchecked") @Override public R visitUnknown(TypeMirror t, P p) { if (intersectionTypeClass != null && intersectionTypeClass.isInstance(t)) { try { List<TypeMirror> bounds = (List<TypeMirror>) getBoundsMethod.invoke(t); return bounds.get(0).accept(this, p); } catch (IllegalAccessException e) { throw new RuntimeException(e.getMessage(), e); } catch (InvocationTargetException e) { throw new RuntimeException(e.getMessage(), e); } } else { return super.visitUnknown(t, p); } }
@Override public TypeName visitDeclared(DeclaredType t, Void p) { ClassName rawType = ClassName.get((TypeElement) t.asElement()); TypeMirror enclosingType = t.getEnclosingType(); TypeName enclosing = (enclosingType.getKind() != TypeKind.NONE) && !t.asElement().getModifiers().contains(Modifier.STATIC) ? enclosingType.accept(this, null) : null; if (t.getTypeArguments().isEmpty() && !(enclosing instanceof ParameterizedTypeName)) { return rawType; } List<TypeName> typeArgumentNames = new ArrayList<>(); for (TypeMirror mirror : t.getTypeArguments()) { typeArgumentNames.add(get(mirror, typeVariables)); } return enclosing instanceof ParameterizedTypeName ? ((ParameterizedTypeName) enclosing).nestedClass( rawType.simpleName(), typeArgumentNames) : new ParameterizedTypeName(null, rawType, typeArgumentNames); }
@Override public Type visitDeclared(DeclaredType t, Type.Parameters p) { Type.Reference reference = factory.reference(qualifiedNameOf(t)); List<? extends TypeMirror> typeArguments = t.getTypeArguments(); if (typeArguments.isEmpty()) { return reference; } List<Type.Nonprimitive> args = new ArrayList<>(); for (TypeMirror a : typeArguments) { args.add((Type.Nonprimitive) a.accept(this, p)); } return factory.parameterized(reference, args); }
@Override public Type visitWildcard(WildcardType t, Type.Parameters p) { @Nullable TypeMirror superBound = t.getSuperBound(); if (superBound != null) { return factory.superWildcard((Type.Defined) superBound.accept(this, p)); } @Nullable TypeMirror extendsBound = t.getExtendsBound(); if (extendsBound != null) { return factory.extendsWildcard((Type.Defined) extendsBound.accept(this, p)); } return factory.extendsWildcard(Type.OBJECT); }
@Override public Type visitArray(ArrayType t, Type.Parameters p) { return factory.array(t.getComponentType().accept(this, p)); }
@Override public AnnotationMetaAttribute visitExecutable(ExecutableType t, Element p) { if ( !p.getKind().equals( ElementKind.METHOD ) ) { return null; } String string = p.getSimpleName().toString(); if ( !StringUtil.isProperty( string, TypeUtils.toTypeString( t.getReturnType() ) ) ) { return null; } TypeMirror returnType = t.getReturnType(); return returnType.accept( this, p ); }
public static String toArrayTypeString(ArrayType type, Context context) { TypeMirror componentType = type.getComponentType(); if ( componentType.getKind().isPrimitive() ) { return PRIMITIVES.get( componentType.getKind() ) + "[]"; } // When an ArrayType is annotated with an annotation which uses TYPE_USE targets, // we cannot simply take the TypeMirror returned by #getComponentType because it // itself is an AnnotatedType. // // The simplest approach here to get the TypeMirror for both ArrayType use cases // is to use the visitor to retrieve the underlying TypeMirror. TypeMirror component = componentType.accept( new SimpleTypeVisitor6<TypeMirror, Void>() { @Override protected TypeMirror defaultAction(TypeMirror e, Void aVoid) { return e; } }, null ); return extractClosestRealTypeAsString( component, context ) + "[]"; }
private void addPersistentMembers(List<? extends Element> membersOfClass, AccessType membersKind) { for ( Element memberOfClass : membersOfClass ) { AccessType forcedAccessType = TypeUtils.determineAnnotationSpecifiedAccessType( memberOfClass ); if ( entityAccessTypeInfo.getAccessType() != membersKind && forcedAccessType == null ) { continue; } if ( TypeUtils.containsAnnotation( memberOfClass, Constants.TRANSIENT ) || memberOfClass.getModifiers().contains( Modifier.TRANSIENT ) || memberOfClass.getModifiers().contains( Modifier.STATIC ) ) { continue; } MetaAttributeGenerationVisitor visitor = new MetaAttributeGenerationVisitor( this, context ); AnnotationMetaAttribute result = memberOfClass.asType().accept( visitor, memberOfClass ); if ( result != null ) { members.put( result.getPropertyName(), result ); } } } }
@Override public String visitExecutable(ExecutableType t, Element p) { if ( !p.getKind().equals( ElementKind.METHOD ) ) { return null; } String string = p.getSimpleName().toString(); if ( !StringUtil.isProperty( string, TypeUtils.toTypeString( t.getReturnType() ) ) ) { return null; } TypeMirror returnType = t.getReturnType(); return returnType.accept( this, p ); } }
@Override public Boolean visitExecutable(ExecutableType t, Element element) { if ( !element.getKind().equals( ElementKind.METHOD ) ) { return Boolean.FALSE; } String string = element.getSimpleName().toString(); if ( !StringUtil.isProperty( string, TypeUtils.toTypeString( t.getReturnType() ) ) ) { return Boolean.FALSE; } TypeMirror returnType = t.getReturnType(); return returnType.accept( this, element ); } }
private static void updateEmbeddableAccessTypeForMember(Context context, AccessType defaultAccessType, Element member) { EmbeddedAttributeVisitor visitor = new EmbeddedAttributeVisitor( context ); String embeddedClassName = member.asType().accept( visitor, member ); if ( embeddedClassName != null ) { AccessTypeInformation accessTypeInfo = context.getAccessTypeInfo( embeddedClassName ); if ( accessTypeInfo == null ) { accessTypeInfo = new AccessTypeInformation( embeddedClassName, null, defaultAccessType ); context.addAccessTypeInformation( embeddedClassName, accessTypeInfo ); } else { accessTypeInfo.setDefaultAccessType( defaultAccessType ); } } }
private boolean isBasicAttribute(Element element, Element returnedElement) { if ( TypeUtils.containsAnnotation( element, Constants.BASIC ) || TypeUtils.containsAnnotation( element, Constants.ONE_TO_ONE ) || TypeUtils.containsAnnotation( element, Constants.MANY_TO_ONE ) || TypeUtils.containsAnnotation( element, Constants.EMBEDDED_ID ) || TypeUtils.containsAnnotation( element, Constants.ID ) ) { return true; } // METAGEN-28 if ( TypeUtils.getAnnotationMirror( element, ORG_HIBERNATE_ANNOTATIONS_TYPE ) != null ) { return true; } BasicAttributeVisitor basicVisitor = new BasicAttributeVisitor( context ); return returnedElement.asType().accept( basicVisitor, returnedElement ); }