|
| 1 | +package org.seasar.doma.internal.apt; |
| 2 | + |
| 3 | +import static org.seasar.doma.internal.util.AssertionUtil.assertNotNull; |
| 4 | + |
| 5 | +import java.io.Writer; |
| 6 | +import java.lang.annotation.Annotation; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.function.Predicate; |
| 11 | +import javax.annotation.processing.ProcessingEnvironment; |
| 12 | +import javax.lang.model.element.*; |
| 13 | +import javax.lang.model.type.DeclaredType; |
| 14 | +import javax.lang.model.type.ExecutableType; |
| 15 | +import javax.lang.model.util.ElementFilter; |
| 16 | +import javax.lang.model.util.SimpleElementVisitor8; |
| 17 | +import javax.lang.model.util.SimpleTypeVisitor8; |
| 18 | +import org.seasar.doma.ParameterName; |
| 19 | + |
| 20 | +public class Elements { |
| 21 | + |
| 22 | + private final Context ctx; |
| 23 | + |
| 24 | + private final ProcessingEnvironment env; |
| 25 | + |
| 26 | + private final javax.lang.model.util.Elements elementUtils; |
| 27 | + |
| 28 | + public Elements(Context ctx) { |
| 29 | + assertNotNull(ctx); |
| 30 | + this.ctx = ctx; |
| 31 | + this.env = ctx.getEnv(); |
| 32 | + this.elementUtils = env.getElementUtils(); |
| 33 | + } |
| 34 | + |
| 35 | + // delegate to elementUtils |
| 36 | + public PackageElement getPackageElement(CharSequence name) { |
| 37 | + return elementUtils.getPackageElement(name); |
| 38 | + } |
| 39 | + |
| 40 | + // delegate to elementUtils |
| 41 | + public TypeElement getTypeElement(CharSequence name) { |
| 42 | + return elementUtils.getTypeElement(name); |
| 43 | + } |
| 44 | + |
| 45 | + // delegate to elementUtils |
| 46 | + public Map<? extends ExecutableElement, ? extends AnnotationValue> getElementValuesWithDefaults( |
| 47 | + AnnotationMirror a) { |
| 48 | + return elementUtils.getElementValuesWithDefaults(a); |
| 49 | + } |
| 50 | + |
| 51 | + // delegate to elementUtils |
| 52 | + public String getDocComment(Element e) { |
| 53 | + return elementUtils.getDocComment(e); |
| 54 | + } |
| 55 | + |
| 56 | + // delegate to elementUtils |
| 57 | + public boolean isDeprecated(Element e) { |
| 58 | + return elementUtils.isDeprecated(e); |
| 59 | + } |
| 60 | + |
| 61 | + // delegate to elementUtils |
| 62 | + public Name getBinaryName(TypeElement type) { |
| 63 | + return elementUtils.getBinaryName(type); |
| 64 | + } |
| 65 | + |
| 66 | + // delegate to elementUtils |
| 67 | + public PackageElement getPackageOf(Element type) { |
| 68 | + return elementUtils.getPackageOf(type); |
| 69 | + } |
| 70 | + |
| 71 | + // delegate to elementUtils |
| 72 | + public List<? extends Element> getAllMembers(TypeElement type) { |
| 73 | + return elementUtils.getAllMembers(type); |
| 74 | + } |
| 75 | + |
| 76 | + // delegate to elementUtils |
| 77 | + public List<? extends AnnotationMirror> getAllAnnotationMirrors(Element e) { |
| 78 | + return elementUtils.getAllAnnotationMirrors(e); |
| 79 | + } |
| 80 | + |
| 81 | + // delegate to elementUtils |
| 82 | + public boolean hides(Element hider, Element hidden) { |
| 83 | + return elementUtils.hides(hider, hidden); |
| 84 | + } |
| 85 | + |
| 86 | + // delegate to elementUtils |
| 87 | + public boolean overrides( |
| 88 | + ExecutableElement overrider, ExecutableElement overridden, TypeElement type) { |
| 89 | + return elementUtils.overrides(overrider, overridden, type); |
| 90 | + } |
| 91 | + |
| 92 | + // delegate to elementUtils |
| 93 | + public String getConstantExpression(Object value) { |
| 94 | + return elementUtils.getConstantExpression(value); |
| 95 | + } |
| 96 | + |
| 97 | + // delegate to elementUtils |
| 98 | + public void printElements(Writer w, Element... elements) { |
| 99 | + elementUtils.printElements(w, elements); |
| 100 | + } |
| 101 | + |
| 102 | + // delegate to elementUtils |
| 103 | + public Name getName(CharSequence cs) { |
| 104 | + return elementUtils.getName(cs); |
| 105 | + } |
| 106 | + |
| 107 | + // delegate to elementUtils |
| 108 | + public boolean isFunctionalInterface(TypeElement type) { |
| 109 | + return elementUtils.isFunctionalInterface(type); |
| 110 | + } |
| 111 | + |
| 112 | + public String getBinaryNameAsString(TypeElement type) { |
| 113 | + Name binaryName = elementUtils.getBinaryName(type); |
| 114 | + return binaryName.toString(); |
| 115 | + } |
| 116 | + |
| 117 | + public String getPackageName(Element element) { |
| 118 | + PackageElement packageElement = ctx.getEnv().getElementUtils().getPackageOf(element); |
| 119 | + return packageElement.getQualifiedName().toString(); |
| 120 | + } |
| 121 | + |
| 122 | + public String getPackageExcludedBinaryName(TypeElement typeElement) { |
| 123 | + String binaryName = ctx.getEnv().getElementUtils().getBinaryName(typeElement).toString(); |
| 124 | + int pos = binaryName.lastIndexOf('.'); |
| 125 | + if (pos < 0) { |
| 126 | + return binaryName; |
| 127 | + } |
| 128 | + return binaryName.substring(pos + 1); |
| 129 | + } |
| 130 | + |
| 131 | + public String getParameterName(VariableElement variableElement) { |
| 132 | + assertNotNull(variableElement); |
| 133 | + ParameterName parameterName = variableElement.getAnnotation(ParameterName.class); |
| 134 | + if (parameterName != null && !parameterName.value().isEmpty()) { |
| 135 | + return parameterName.value(); |
| 136 | + } |
| 137 | + return variableElement.getSimpleName().toString(); |
| 138 | + } |
| 139 | + |
| 140 | + public boolean isEnclosing(Element enclosingElement, Element enclosedElement) { |
| 141 | + assertNotNull(enclosingElement, enclosedElement); |
| 142 | + if (enclosingElement.equals(enclosedElement)) { |
| 143 | + return true; |
| 144 | + } |
| 145 | + for (Element e = enclosedElement; e != null; e = e.getEnclosingElement()) { |
| 146 | + if (enclosingElement.equals(e)) { |
| 147 | + return true; |
| 148 | + } |
| 149 | + } |
| 150 | + return false; |
| 151 | + } |
| 152 | + |
| 153 | + public ExecutableType toExecutableType(ExecutableElement element) { |
| 154 | + assertNotNull(element); |
| 155 | + return element |
| 156 | + .asType() |
| 157 | + .accept( |
| 158 | + new SimpleTypeVisitor8<ExecutableType, Void>() { |
| 159 | + |
| 160 | + // delegate to elementUtils |
| 161 | + public ExecutableType visitExecutable(ExecutableType t, Void p) { |
| 162 | + return t; |
| 163 | + } |
| 164 | + }, |
| 165 | + null); |
| 166 | + } |
| 167 | + |
| 168 | + public TypeElement toTypeElement(Element element) { |
| 169 | + assertNotNull(element); |
| 170 | + return element.accept( |
| 171 | + new SimpleElementVisitor8<TypeElement, Void>() { |
| 172 | + |
| 173 | + // delegate to elementUtils |
| 174 | + public TypeElement visitType(TypeElement e, Void p) { |
| 175 | + return e; |
| 176 | + } |
| 177 | + }, |
| 178 | + null); |
| 179 | + } |
| 180 | + |
| 181 | + public TypeElement getTypeElement(String binaryName) { |
| 182 | + assertNotNull(binaryName); |
| 183 | + String[] parts = binaryName.split("\\$"); |
| 184 | + if (parts.length > 1) { |
| 185 | + TypeElement topElement = getTypeElement(parts[0]); |
| 186 | + if (topElement == null) { |
| 187 | + return null; |
| 188 | + } |
| 189 | + return getEnclosedTypeElement(topElement, Arrays.asList(parts).subList(1, parts.length)); |
| 190 | + } |
| 191 | + // Class<?> clazz = null; |
| 192 | + // try { |
| 193 | + // clazz = Class.forName(className); |
| 194 | + // return getTypeElement(clazz, env); |
| 195 | + // } catch (ClassNotFoundException ignored) { |
| 196 | + // } |
| 197 | + try { |
| 198 | + return elementUtils.getTypeElement(binaryName); |
| 199 | + } catch (NullPointerException ignored) { |
| 200 | + return null; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + public TypeElement getTypeElement(Class<?> clazz) { |
| 205 | + assertNotNull(clazz); |
| 206 | + return ctx.getEnv().getElementUtils().getTypeElement(clazz.getCanonicalName()); |
| 207 | + } |
| 208 | + |
| 209 | + public TypeElement getEnclosedTypeElement(TypeElement typeElement, List<String> enclosedNames) { |
| 210 | + TypeElement enclosing = typeElement; |
| 211 | + for (String enclosedName : enclosedNames) { |
| 212 | + for (TypeElement enclosed : ElementFilter.typesIn(enclosing.getEnclosedElements())) { |
| 213 | + if (enclosed.getSimpleName().contentEquals(enclosedName)) { |
| 214 | + enclosing = enclosed; |
| 215 | + break; |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + return typeElement != enclosing ? enclosing : null; |
| 220 | + } |
| 221 | + |
| 222 | + public AnnotationMirror getAnnotationMirror( |
| 223 | + Element element, Class<? extends Annotation> annotationClass) { |
| 224 | + return getAnnotationMirrorInternal( |
| 225 | + element, type -> ctx.getTypes().isSameType(type, annotationClass)); |
| 226 | + } |
| 227 | + |
| 228 | + public AnnotationMirror getAnnotationMirror(Element element, String annotationClassName) { |
| 229 | + return getAnnotationMirrorInternal( |
| 230 | + element, type -> ctx.getTypes().getClassName(type).equals(annotationClassName)); |
| 231 | + } |
| 232 | + |
| 233 | + protected AnnotationMirror getAnnotationMirrorInternal( |
| 234 | + Element element, Predicate<DeclaredType> predicate) { |
| 235 | + for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) { |
| 236 | + DeclaredType annotationType = annotationMirror.getAnnotationType(); |
| 237 | + if (predicate.test(annotationType)) { |
| 238 | + return annotationMirror; |
| 239 | + } |
| 240 | + } |
| 241 | + return null; |
| 242 | + } |
| 243 | + |
| 244 | + public ExecutableElement getNoArgConstructor(TypeElement typeElement) { |
| 245 | + for (ExecutableElement constructor : |
| 246 | + ElementFilter.constructorsIn(typeElement.getEnclosedElements())) { |
| 247 | + if (constructor.getParameters().isEmpty()) { |
| 248 | + return constructor; |
| 249 | + } |
| 250 | + } |
| 251 | + return null; |
| 252 | + } |
| 253 | +} |
0 commit comments