|
| 1 | +package org.seasar.doma.internal.apt.decl; |
| 2 | + |
| 3 | +import static org.seasar.doma.internal.util.AssertionUtil.assertNotNull; |
| 4 | +import static org.seasar.doma.internal.util.AssertionUtil.assertTrue; |
| 5 | + |
| 6 | +import java.util.*; |
| 7 | +import javax.lang.model.element.*; |
| 8 | +import javax.lang.model.type.DeclaredType; |
| 9 | +import javax.lang.model.type.TypeKind; |
| 10 | +import javax.lang.model.type.TypeMirror; |
| 11 | +import org.seasar.doma.internal.apt.Context; |
| 12 | + |
| 13 | +public class Declarations { |
| 14 | + |
| 15 | + private final Context ctx; |
| 16 | + |
| 17 | + public Declarations(Context ctx) { |
| 18 | + assertNotNull(ctx); |
| 19 | + this.ctx = ctx; |
| 20 | + } |
| 21 | + |
| 22 | + public TypeDeclaration newUnknownTypeDeclaration() { |
| 23 | + TypeMirror type = ctx.getTypes().getNoType(TypeKind.NONE); |
| 24 | + return newTypeDeclaration(type); |
| 25 | + } |
| 26 | + |
| 27 | + public TypeDeclaration newBooleanTypeDeclaration() { |
| 28 | + TypeMirror type = ctx.getTypes().getTypeMirror(boolean.class); |
| 29 | + return newTypeDeclaration(type); |
| 30 | + } |
| 31 | + |
| 32 | + public TypeDeclaration newTypeDeclaration(Class<?> clazz) { |
| 33 | + assertNotNull(clazz); |
| 34 | + TypeMirror type = ctx.getTypes().getTypeMirror(clazz); |
| 35 | + return newTypeDeclaration(type); |
| 36 | + } |
| 37 | + |
| 38 | + public TypeDeclaration newTypeDeclaration(TypeMirror type) { |
| 39 | + assertNotNull(type); |
| 40 | + TypeElement typeElement = ctx.getTypes().toTypeElement(type); |
| 41 | + Map<String, List<TypeParameterDeclaration>> map = new LinkedHashMap<>(); |
| 42 | + gatherTypeParameterDeclarations(type, map); |
| 43 | + return new TypeDeclaration(ctx, type, typeElement, map); |
| 44 | + } |
| 45 | + |
| 46 | + private void gatherTypeParameterDeclarations( |
| 47 | + TypeMirror type, Map<String, List<TypeParameterDeclaration>> typeParameterDeclarationsMap) { |
| 48 | + TypeElement typeElement = ctx.getTypes().toTypeElement(type); |
| 49 | + if (typeElement == null) { |
| 50 | + return; |
| 51 | + } |
| 52 | + String binaryName = ctx.getElements().getBinaryNameAsString(typeElement); |
| 53 | + typeParameterDeclarationsMap.put( |
| 54 | + binaryName, createTypeParameterDeclarations(typeElement, type)); |
| 55 | + for (TypeMirror superType : ctx.getTypes().directSupertypes(type)) { |
| 56 | + TypeElement superElement = ctx.getTypes().toTypeElement(superType); |
| 57 | + if (superElement == null) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + String superBinaryName = ctx.getElements().getBinaryNameAsString(superElement); |
| 61 | + if (typeParameterDeclarationsMap.containsKey(superBinaryName)) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + typeParameterDeclarationsMap.put( |
| 65 | + superBinaryName, createTypeParameterDeclarations(superElement, superType)); |
| 66 | + gatherTypeParameterDeclarations(superType, typeParameterDeclarationsMap); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private List<TypeParameterDeclaration> createTypeParameterDeclarations( |
| 71 | + TypeElement typeElement, TypeMirror type) { |
| 72 | + assertNotNull(typeElement, type); |
| 73 | + List<TypeParameterDeclaration> list = new ArrayList<>(); |
| 74 | + Iterator<? extends TypeParameterElement> formalParams = |
| 75 | + typeElement.getTypeParameters().iterator(); |
| 76 | + DeclaredType declaredType = ctx.getTypes().toDeclaredType(type); |
| 77 | + Iterator<? extends TypeMirror> actualParams = declaredType.getTypeArguments().iterator(); |
| 78 | + for (; formalParams.hasNext() && actualParams.hasNext(); ) { |
| 79 | + TypeMirror formalType = formalParams.next().asType(); |
| 80 | + TypeMirror actualType = actualParams.next(); |
| 81 | + TypeParameterDeclaration typeParameterDeclaration = |
| 82 | + ctx.getDeclarations().newTypeParameterDeclaration(formalType, actualType); |
| 83 | + list.add(typeParameterDeclaration); |
| 84 | + } |
| 85 | + return Collections.unmodifiableList(list); |
| 86 | + } |
| 87 | + |
| 88 | + FieldDeclaration newFieldDeclaration( |
| 89 | + VariableElement fieldElement, List<TypeParameterDeclaration> typeParameterDeclarations) { |
| 90 | + assertNotNull(fieldElement, typeParameterDeclarations); |
| 91 | + assertTrue( |
| 92 | + fieldElement.getKind() == ElementKind.FIELD |
| 93 | + || fieldElement.getKind() == ElementKind.ENUM_CONSTANT, |
| 94 | + fieldElement.getKind().toString()); |
| 95 | + TypeMirror fieldType = resolveTypeParameter(fieldElement.asType(), typeParameterDeclarations); |
| 96 | + TypeDeclaration typeDeclaration = newTypeDeclaration(fieldType); |
| 97 | + return new FieldDeclaration(fieldElement, typeDeclaration); |
| 98 | + } |
| 99 | + |
| 100 | + ConstructorDeclaration newConstructorDeclaration( |
| 101 | + ExecutableElement constructorElement, |
| 102 | + List<TypeParameterDeclaration> typeParameterDeclarations) { |
| 103 | + assertNotNull(constructorElement, typeParameterDeclarations); |
| 104 | + assertTrue(constructorElement.getKind() == ElementKind.CONSTRUCTOR); |
| 105 | + ConstructorDeclaration constructorDeclaration = new ConstructorDeclaration(constructorElement); |
| 106 | + return constructorDeclaration; |
| 107 | + } |
| 108 | + |
| 109 | + MethodDeclaration newMethodDeclaration( |
| 110 | + ExecutableElement methodElement, List<TypeParameterDeclaration> typeParameterDeclarations) { |
| 111 | + assertNotNull(methodElement, typeParameterDeclarations); |
| 112 | + assertTrue(methodElement.getKind() == ElementKind.METHOD); |
| 113 | + TypeMirror returnTypeMirror = |
| 114 | + resolveTypeParameter(methodElement.getReturnType(), typeParameterDeclarations); |
| 115 | + TypeDeclaration returnTypeDeclaration = newTypeDeclaration(returnTypeMirror); |
| 116 | + return new MethodDeclaration(methodElement, returnTypeDeclaration); |
| 117 | + } |
| 118 | + |
| 119 | + private TypeParameterDeclaration newTypeParameterDeclaration( |
| 120 | + TypeMirror formalType, TypeMirror actualType) { |
| 121 | + assertNotNull(formalType, actualType); |
| 122 | + TypeParameterDeclaration typeParameterDeclaration = |
| 123 | + new TypeParameterDeclaration(formalType, actualType); |
| 124 | + return typeParameterDeclaration; |
| 125 | + } |
| 126 | + |
| 127 | + private TypeMirror resolveTypeParameter( |
| 128 | + TypeMirror formalType, List<TypeParameterDeclaration> typeParameterDeclarations) { |
| 129 | + for (TypeParameterDeclaration typeParameterDecl : typeParameterDeclarations) { |
| 130 | + if (formalType.equals(typeParameterDecl.getFormalType())) { |
| 131 | + return typeParameterDecl.getActualType(); |
| 132 | + } |
| 133 | + } |
| 134 | + return formalType; |
| 135 | + } |
| 136 | +} |
0 commit comments