Tue, 06 Mar 2012 16:09:35 -0800
7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom
ohair@286 | 1 | /* |
ohair@286 | 2 | * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | package com.sun.tools.internal.jxc.model.nav; |
ohair@286 | 27 | |
ohair@286 | 28 | import com.sun.source.tree.CompilationUnitTree; |
ohair@286 | 29 | import com.sun.source.util.TreePath; |
ohair@286 | 30 | import com.sun.source.util.Trees; |
ohair@286 | 31 | import com.sun.xml.internal.bind.v2.model.nav.Navigator; |
ohair@286 | 32 | import com.sun.xml.internal.bind.v2.runtime.Location; |
ohair@286 | 33 | |
ohair@286 | 34 | import javax.annotation.processing.ProcessingEnvironment; |
ohair@286 | 35 | import javax.lang.model.element.Element; |
ohair@286 | 36 | import javax.lang.model.element.ElementKind; |
ohair@286 | 37 | import javax.lang.model.element.ExecutableElement; |
ohair@286 | 38 | import javax.lang.model.element.Modifier; |
ohair@286 | 39 | import javax.lang.model.element.TypeElement; |
ohair@286 | 40 | import javax.lang.model.element.TypeParameterElement; |
ohair@286 | 41 | import javax.lang.model.element.VariableElement; |
ohair@286 | 42 | import javax.lang.model.type.ArrayType; |
ohair@286 | 43 | import javax.lang.model.type.DeclaredType; |
ohair@286 | 44 | import javax.lang.model.type.PrimitiveType; |
ohair@286 | 45 | import javax.lang.model.type.TypeKind; |
ohair@286 | 46 | import javax.lang.model.type.TypeMirror; |
ohair@286 | 47 | import javax.lang.model.type.TypeVariable; |
ohair@286 | 48 | import javax.lang.model.type.TypeVisitor; |
ohair@286 | 49 | import javax.lang.model.type.WildcardType; |
ohair@286 | 50 | import javax.lang.model.util.ElementFilter; |
ohair@286 | 51 | import javax.lang.model.util.Elements; |
ohair@286 | 52 | import javax.lang.model.util.SimpleTypeVisitor6; |
ohair@286 | 53 | import javax.lang.model.util.Types; |
ohair@286 | 54 | import java.util.Collection; |
ohair@286 | 55 | import java.util.HashMap; |
ohair@286 | 56 | import java.util.HashSet; |
ohair@286 | 57 | import java.util.List; |
ohair@286 | 58 | import java.util.Map; |
ohair@286 | 59 | |
ohair@286 | 60 | /** |
ohair@286 | 61 | * {@link Navigator} implementation for annotation processing. |
ohair@286 | 62 | * TODO: check the spec on how generics are supposed to be handled |
ohair@286 | 63 | * |
ohair@286 | 64 | * @author Kohsuke Kawaguchi (kk@kohsuke.org) |
ohair@286 | 65 | */ |
ohair@286 | 66 | public class ApNavigator implements Navigator<TypeMirror, TypeElement, VariableElement, ExecutableElement> { |
ohair@286 | 67 | |
ohair@286 | 68 | private final ProcessingEnvironment env; |
ohair@286 | 69 | |
ohair@286 | 70 | private final PrimitiveType primitiveByte; |
ohair@286 | 71 | |
ohair@286 | 72 | public ApNavigator(ProcessingEnvironment env) { |
ohair@286 | 73 | this.env = env; |
ohair@286 | 74 | this.primitiveByte = env.getTypeUtils().getPrimitiveType(TypeKind.BYTE); |
ohair@286 | 75 | } |
ohair@286 | 76 | |
ohair@286 | 77 | public TypeElement getSuperClass(TypeElement typeElement) { |
ohair@286 | 78 | if (typeElement.getKind().equals(ElementKind.CLASS)) { |
ohair@286 | 79 | TypeMirror sup = typeElement.getSuperclass(); |
ohair@286 | 80 | if (!sup.getKind().equals(TypeKind.NONE)) |
ohair@286 | 81 | return (TypeElement) ((DeclaredType) sup).asElement(); |
ohair@286 | 82 | else |
ohair@286 | 83 | return null; |
ohair@286 | 84 | } |
ohair@286 | 85 | return env.getElementUtils().getTypeElement(Object.class.getName()); |
ohair@286 | 86 | } |
ohair@286 | 87 | |
ohair@286 | 88 | public TypeMirror getBaseClass(TypeMirror type, TypeElement sup) { |
ohair@286 | 89 | return baseClassFinder.visit(type, sup); |
ohair@286 | 90 | } |
ohair@286 | 91 | |
ohair@286 | 92 | public String getClassName(TypeElement t) { |
ohair@286 | 93 | return t.getQualifiedName().toString(); |
ohair@286 | 94 | } |
ohair@286 | 95 | |
ohair@286 | 96 | public String getTypeName(TypeMirror typeMirror) { |
ohair@286 | 97 | return typeMirror.toString(); |
ohair@286 | 98 | } |
ohair@286 | 99 | |
ohair@286 | 100 | public String getClassShortName(TypeElement t) { |
ohair@286 | 101 | return t.getSimpleName().toString(); |
ohair@286 | 102 | } |
ohair@286 | 103 | |
ohair@286 | 104 | public Collection<VariableElement> getDeclaredFields(TypeElement typeElement) { |
ohair@286 | 105 | return ElementFilter.fieldsIn(typeElement.getEnclosedElements()); |
ohair@286 | 106 | } |
ohair@286 | 107 | |
ohair@286 | 108 | public VariableElement getDeclaredField(TypeElement clazz, String fieldName) { |
ohair@286 | 109 | for (VariableElement fd : ElementFilter.fieldsIn(clazz.getEnclosedElements())) { |
ohair@286 | 110 | if (fd.getSimpleName().toString().equals(fieldName)) |
ohair@286 | 111 | return fd; |
ohair@286 | 112 | } |
ohair@286 | 113 | return null; |
ohair@286 | 114 | } |
ohair@286 | 115 | |
ohair@286 | 116 | public Collection<ExecutableElement> getDeclaredMethods(TypeElement typeElement) { |
ohair@286 | 117 | return ElementFilter.methodsIn(typeElement.getEnclosedElements()); |
ohair@286 | 118 | } |
ohair@286 | 119 | |
ohair@286 | 120 | public TypeElement getDeclaringClassForField(VariableElement f) { |
ohair@286 | 121 | return (TypeElement) f.getEnclosingElement(); |
ohair@286 | 122 | } |
ohair@286 | 123 | |
ohair@286 | 124 | public TypeElement getDeclaringClassForMethod(ExecutableElement m) { |
ohair@286 | 125 | return (TypeElement) m.getEnclosingElement(); |
ohair@286 | 126 | } |
ohair@286 | 127 | |
ohair@286 | 128 | public TypeMirror getFieldType(VariableElement f) { |
ohair@286 | 129 | return f.asType(); |
ohair@286 | 130 | } |
ohair@286 | 131 | |
ohair@286 | 132 | public String getFieldName(VariableElement f) { |
ohair@286 | 133 | return f.getSimpleName().toString(); |
ohair@286 | 134 | } |
ohair@286 | 135 | |
ohair@286 | 136 | public String getMethodName(ExecutableElement m) { |
ohair@286 | 137 | return m.getSimpleName().toString(); |
ohair@286 | 138 | } |
ohair@286 | 139 | |
ohair@286 | 140 | public TypeMirror getReturnType(ExecutableElement m) { |
ohair@286 | 141 | return m.getReturnType(); |
ohair@286 | 142 | } |
ohair@286 | 143 | |
ohair@286 | 144 | public TypeMirror[] getMethodParameters(ExecutableElement m) { |
ohair@286 | 145 | Collection<? extends VariableElement> ps = m.getParameters(); |
ohair@286 | 146 | TypeMirror[] r = new TypeMirror[ps.size()]; |
ohair@286 | 147 | int i=0; |
ohair@286 | 148 | for (VariableElement p : ps) |
ohair@286 | 149 | r[i++] = p.asType(); |
ohair@286 | 150 | return r; |
ohair@286 | 151 | } |
ohair@286 | 152 | |
ohair@286 | 153 | public boolean isStaticMethod(ExecutableElement m) { |
ohair@286 | 154 | return hasModifier(m, Modifier.STATIC); |
ohair@286 | 155 | } |
ohair@286 | 156 | |
ohair@286 | 157 | public boolean isFinalMethod(ExecutableElement m) { |
ohair@286 | 158 | return hasModifier(m, Modifier.FINAL); |
ohair@286 | 159 | } |
ohair@286 | 160 | |
ohair@286 | 161 | private boolean hasModifier(Element d, Modifier mod) { |
ohair@286 | 162 | return d.getModifiers().contains(mod); |
ohair@286 | 163 | } |
ohair@286 | 164 | |
ohair@286 | 165 | public boolean isSubClassOf(TypeMirror sub, TypeMirror sup) { |
ohair@286 | 166 | if(sup==DUMMY) |
ohair@286 | 167 | // see ref(). if the sub type is known to Annotation Processing, |
ohair@286 | 168 | // its base class must be known. Thus if the sup is DUMMY, |
ohair@286 | 169 | // it cannot possibly be the super type. |
ohair@286 | 170 | return false; |
ohair@286 | 171 | return env.getTypeUtils().isSubtype(sub,sup); |
ohair@286 | 172 | } |
ohair@286 | 173 | |
ohair@286 | 174 | private String getSourceClassName(Class clazz) { |
ohair@286 | 175 | Class<?> d = clazz.getDeclaringClass(); |
ohair@286 | 176 | if(d==null) |
ohair@286 | 177 | return clazz.getName(); |
ohair@286 | 178 | else { |
ohair@286 | 179 | String shortName = clazz.getName().substring(d.getName().length()+1/*for $*/); |
ohair@286 | 180 | return getSourceClassName(d)+'.'+shortName; |
ohair@286 | 181 | } |
ohair@286 | 182 | } |
ohair@286 | 183 | |
ohair@286 | 184 | public TypeMirror ref(Class c) { |
ohair@286 | 185 | if(c.isArray()) |
ohair@286 | 186 | return env.getTypeUtils().getArrayType( ref(c.getComponentType()) ); |
ohair@286 | 187 | if(c.isPrimitive()) |
ohair@286 | 188 | return getPrimitive(c); |
ohair@286 | 189 | TypeElement t = env.getElementUtils().getTypeElement(getSourceClassName(c)); |
ohair@286 | 190 | // Annotation Processing only operates on a set of classes used in the compilation, |
ohair@286 | 191 | // and it won't recognize additional classes (even if they are visible from javac) |
ohair@286 | 192 | // and return null. |
ohair@286 | 193 | // |
ohair@286 | 194 | // this is causing a problem where we check if a type is collection. |
ohair@286 | 195 | // so until the problem is fixed in Annotation Processing, work around the issue |
ohair@286 | 196 | // by returning a dummy token |
ohair@286 | 197 | // TODO: check if this is still valid |
ohair@286 | 198 | if(t==null) |
ohair@286 | 199 | return DUMMY; |
ohair@286 | 200 | return env.getTypeUtils().getDeclaredType(t); |
ohair@286 | 201 | } |
ohair@286 | 202 | |
ohair@286 | 203 | public TypeMirror use(TypeElement t) { |
ohair@286 | 204 | assert t != null; |
ohair@286 | 205 | return env.getTypeUtils().getDeclaredType(t); |
ohair@286 | 206 | } |
ohair@286 | 207 | |
ohair@286 | 208 | public TypeElement asDecl(TypeMirror m) { |
ohair@286 | 209 | m = env.getTypeUtils().erasure(m); |
ohair@286 | 210 | if (m.getKind().equals(TypeKind.DECLARED)) { |
ohair@286 | 211 | DeclaredType d = (DeclaredType) m; |
ohair@286 | 212 | return (TypeElement) d.asElement(); |
ohair@286 | 213 | } else |
ohair@286 | 214 | return null; |
ohair@286 | 215 | } |
ohair@286 | 216 | |
ohair@286 | 217 | public TypeElement asDecl(Class c) { |
ohair@286 | 218 | return env.getElementUtils().getTypeElement(getSourceClassName(c)); |
ohair@286 | 219 | } |
ohair@286 | 220 | |
ohair@286 | 221 | public TypeMirror erasure(TypeMirror t) { |
ohair@286 | 222 | Types tu = env.getTypeUtils(); |
ohair@286 | 223 | t = tu.erasure(t); |
ohair@286 | 224 | if (t.getKind().equals(TypeKind.DECLARED)) { |
ohair@286 | 225 | DeclaredType dt = (DeclaredType)t; |
ohair@286 | 226 | if (!dt.getTypeArguments().isEmpty()) |
ohair@286 | 227 | return tu.getDeclaredType((TypeElement) dt.asElement()); |
ohair@286 | 228 | } |
ohair@286 | 229 | return t; |
ohair@286 | 230 | } |
ohair@286 | 231 | |
ohair@286 | 232 | public boolean isAbstract(TypeElement clazz) { |
ohair@286 | 233 | return hasModifier(clazz,Modifier.ABSTRACT); |
ohair@286 | 234 | } |
ohair@286 | 235 | |
ohair@286 | 236 | public boolean isFinal(TypeElement clazz) { |
ohair@286 | 237 | return hasModifier(clazz,Modifier.FINAL); |
ohair@286 | 238 | } |
ohair@286 | 239 | |
ohair@286 | 240 | public VariableElement[] getEnumConstants(TypeElement clazz) { |
ohair@286 | 241 | List<? extends Element> elements = env.getElementUtils().getAllMembers(clazz); |
ohair@286 | 242 | Collection<VariableElement> constants = new HashSet<VariableElement>(); |
ohair@286 | 243 | for (Element element : elements) { |
ohair@286 | 244 | if (element.getKind().equals(ElementKind.ENUM_CONSTANT)) { |
ohair@286 | 245 | constants.add((VariableElement) element); |
ohair@286 | 246 | } |
ohair@286 | 247 | } |
ohair@286 | 248 | return constants.toArray(new VariableElement[constants.size()]); |
ohair@286 | 249 | } |
ohair@286 | 250 | |
ohair@286 | 251 | public TypeMirror getVoidType() { |
ohair@286 | 252 | return env.getTypeUtils().getNoType(TypeKind.VOID); |
ohair@286 | 253 | } |
ohair@286 | 254 | |
ohair@286 | 255 | public String getPackageName(TypeElement clazz) { |
ohair@286 | 256 | return env.getElementUtils().getPackageOf(clazz).getQualifiedName().toString(); |
ohair@286 | 257 | } |
ohair@286 | 258 | |
ohair@286 | 259 | public TypeElement findClass(String className, TypeElement referencePoint) { |
ohair@286 | 260 | return env.getElementUtils().getTypeElement(className); |
ohair@286 | 261 | } |
ohair@286 | 262 | |
ohair@286 | 263 | public boolean isBridgeMethod(ExecutableElement method) { |
ohair@286 | 264 | return method.getModifiers().contains(Modifier.VOLATILE); |
ohair@286 | 265 | } |
ohair@286 | 266 | |
ohair@286 | 267 | public boolean isOverriding(ExecutableElement method, TypeElement base) { |
ohair@286 | 268 | Elements elements = env.getElementUtils(); |
ohair@286 | 269 | |
ohair@286 | 270 | while (true) { |
ohair@286 | 271 | for (ExecutableElement m : ElementFilter.methodsIn(elements.getAllMembers(base))) { |
ohair@286 | 272 | if (elements.overrides(method, m, base)) |
ohair@286 | 273 | return true; |
ohair@286 | 274 | } |
ohair@286 | 275 | |
ohair@286 | 276 | if (base.getSuperclass().getKind().equals(TypeKind.NONE)) |
ohair@286 | 277 | return false; |
ohair@286 | 278 | base = (TypeElement) env.getTypeUtils().asElement(base.getSuperclass()); |
ohair@286 | 279 | } |
ohair@286 | 280 | } |
ohair@286 | 281 | |
ohair@286 | 282 | public boolean isInterface(TypeElement clazz) { |
ohair@286 | 283 | return clazz.getKind().isInterface(); |
ohair@286 | 284 | } |
ohair@286 | 285 | |
ohair@286 | 286 | public boolean isTransient(VariableElement f) { |
ohair@286 | 287 | return f.getModifiers().contains(Modifier.TRANSIENT); |
ohair@286 | 288 | } |
ohair@286 | 289 | |
ohair@286 | 290 | public boolean isInnerClass(TypeElement clazz) { |
ohair@286 | 291 | return clazz.getEnclosingElement() != null && !clazz.getModifiers().contains(Modifier.STATIC); |
ohair@286 | 292 | } |
ohair@286 | 293 | |
ohair@286 | 294 | @Override |
ohair@286 | 295 | public boolean isSameType(TypeMirror t1, TypeMirror t2) { |
ohair@286 | 296 | return env.getTypeUtils().isSameType(t1, t2); |
ohair@286 | 297 | } |
ohair@286 | 298 | |
ohair@286 | 299 | public boolean isArray(TypeMirror type) { |
ohair@286 | 300 | return type != null && type.getKind().equals(TypeKind.ARRAY); |
ohair@286 | 301 | } |
ohair@286 | 302 | |
ohair@286 | 303 | public boolean isArrayButNotByteArray(TypeMirror t) { |
ohair@286 | 304 | if(!isArray(t)) |
ohair@286 | 305 | return false; |
ohair@286 | 306 | |
ohair@286 | 307 | ArrayType at = (ArrayType) t; |
ohair@286 | 308 | TypeMirror ct = at.getComponentType(); |
ohair@286 | 309 | |
ohair@286 | 310 | return !ct.equals(primitiveByte); |
ohair@286 | 311 | } |
ohair@286 | 312 | |
ohair@286 | 313 | public TypeMirror getComponentType(TypeMirror t) { |
ohair@286 | 314 | if (isArray(t)) { |
ohair@286 | 315 | ArrayType at = (ArrayType) t; |
ohair@286 | 316 | return at.getComponentType(); |
ohair@286 | 317 | } |
ohair@286 | 318 | |
ohair@286 | 319 | throw new IllegalArgumentException(); |
ohair@286 | 320 | } |
ohair@286 | 321 | |
ohair@286 | 322 | public TypeMirror getTypeArgument(TypeMirror typeMirror, int i) { |
ohair@286 | 323 | if (typeMirror != null && typeMirror.getKind().equals(TypeKind.DECLARED)) { |
ohair@286 | 324 | DeclaredType declaredType = (DeclaredType) typeMirror; |
ohair@286 | 325 | TypeMirror[] args = declaredType.getTypeArguments().toArray(new TypeMirror[declaredType.getTypeArguments().size()]); |
ohair@286 | 326 | return args[i]; |
ohair@286 | 327 | } else throw new IllegalArgumentException(); |
ohair@286 | 328 | } |
ohair@286 | 329 | |
ohair@286 | 330 | public boolean isParameterizedType(TypeMirror typeMirror) { |
ohair@286 | 331 | if (typeMirror != null && typeMirror.getKind().equals(TypeKind.DECLARED)) { |
ohair@286 | 332 | DeclaredType d = (DeclaredType) typeMirror; |
ohair@286 | 333 | return !d.getTypeArguments().isEmpty(); |
ohair@286 | 334 | } |
ohair@286 | 335 | return false; |
ohair@286 | 336 | } |
ohair@286 | 337 | |
ohair@286 | 338 | public boolean isPrimitive(TypeMirror t) { |
ohair@286 | 339 | return t.getKind().isPrimitive(); |
ohair@286 | 340 | } |
ohair@286 | 341 | |
ohair@286 | 342 | private static final Map<Class, TypeKind> primitives = new HashMap<Class, TypeKind>(); |
ohair@286 | 343 | |
ohair@286 | 344 | static { |
ohair@286 | 345 | primitives.put(Integer.TYPE, TypeKind.INT); |
ohair@286 | 346 | primitives.put(Byte.TYPE, TypeKind.BYTE); |
ohair@286 | 347 | primitives.put(Float.TYPE, TypeKind.FLOAT); |
ohair@286 | 348 | primitives.put(Boolean.TYPE, TypeKind.BOOLEAN); |
ohair@286 | 349 | primitives.put(Short.TYPE, TypeKind.SHORT); |
ohair@286 | 350 | primitives.put(Long.TYPE, TypeKind.LONG); |
ohair@286 | 351 | primitives.put(Double.TYPE, TypeKind.DOUBLE); |
ohair@286 | 352 | primitives.put(Character.TYPE, TypeKind.CHAR); |
ohair@286 | 353 | } |
ohair@286 | 354 | |
ohair@286 | 355 | public TypeMirror getPrimitive(Class primitiveType) { |
ohair@286 | 356 | assert primitiveType.isPrimitive(); |
ohair@286 | 357 | if(primitiveType==void.class) |
ohair@286 | 358 | return getVoidType(); |
ohair@286 | 359 | return env.getTypeUtils().getPrimitiveType(primitives.get(primitiveType)); |
ohair@286 | 360 | } |
ohair@286 | 361 | |
ohair@286 | 362 | /** |
ohair@286 | 363 | * see {@link #ref(Class)}. |
ohair@286 | 364 | */ |
ohair@286 | 365 | private static final TypeMirror DUMMY = new TypeMirror() { |
ohair@286 | 366 | @Override |
ohair@286 | 367 | public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
ohair@286 | 368 | throw new IllegalStateException(); |
ohair@286 | 369 | } |
ohair@286 | 370 | |
ohair@286 | 371 | @Override |
ohair@286 | 372 | public TypeKind getKind() { |
ohair@286 | 373 | throw new IllegalStateException(); |
ohair@286 | 374 | } |
ohair@286 | 375 | }; |
ohair@286 | 376 | |
ohair@286 | 377 | public Location getClassLocation(TypeElement typeElement) { |
ohair@286 | 378 | Trees trees = Trees.instance(env); |
ohair@286 | 379 | return getLocation(typeElement.getQualifiedName().toString(), trees.getPath(typeElement)); |
ohair@286 | 380 | } |
ohair@286 | 381 | |
ohair@286 | 382 | public Location getFieldLocation(VariableElement variableElement) { |
ohair@286 | 383 | return getLocation(variableElement); |
ohair@286 | 384 | } |
ohair@286 | 385 | |
ohair@286 | 386 | public Location getMethodLocation(ExecutableElement executableElement) { |
ohair@286 | 387 | return getLocation(executableElement); |
ohair@286 | 388 | } |
ohair@286 | 389 | |
ohair@286 | 390 | public boolean hasDefaultConstructor(TypeElement t) { |
ohair@286 | 391 | if (t == null || !t.getKind().equals(ElementKind.CLASS)) |
ohair@286 | 392 | return false; |
ohair@286 | 393 | |
ohair@286 | 394 | for (ExecutableElement init : ElementFilter.constructorsIn(env.getElementUtils().getAllMembers(t))) { |
ohair@286 | 395 | if (init.getParameters().isEmpty()) |
ohair@286 | 396 | return true; |
ohair@286 | 397 | } |
ohair@286 | 398 | return false; |
ohair@286 | 399 | } |
ohair@286 | 400 | |
ohair@286 | 401 | public boolean isStaticField(VariableElement f) { |
ohair@286 | 402 | return hasModifier(f,Modifier.STATIC); |
ohair@286 | 403 | } |
ohair@286 | 404 | |
ohair@286 | 405 | public boolean isPublicMethod(ExecutableElement m) { |
ohair@286 | 406 | return hasModifier(m,Modifier.PUBLIC); |
ohair@286 | 407 | } |
ohair@286 | 408 | |
ohair@286 | 409 | public boolean isPublicField(VariableElement f) { |
ohair@286 | 410 | return hasModifier(f,Modifier.PUBLIC); |
ohair@286 | 411 | } |
ohair@286 | 412 | |
ohair@286 | 413 | public boolean isEnum(TypeElement t) { |
ohair@286 | 414 | return t != null && t.getKind().equals(ElementKind.ENUM); |
ohair@286 | 415 | } |
ohair@286 | 416 | |
ohair@286 | 417 | private Location getLocation(Element element) { |
ohair@286 | 418 | Trees trees = Trees.instance(env); |
ohair@286 | 419 | return getLocation( |
ohair@286 | 420 | ((TypeElement) element.getEnclosingElement()).getQualifiedName() + "." + element.getSimpleName(), |
ohair@286 | 421 | trees.getPath(element) |
ohair@286 | 422 | ); |
ohair@286 | 423 | } |
ohair@286 | 424 | |
ohair@286 | 425 | private Location getLocation(final String name, final TreePath treePath) { |
ohair@286 | 426 | return new Location() { |
ohair@286 | 427 | public String toString() { |
ohair@286 | 428 | if (treePath == null) |
ohair@286 | 429 | return name + " (Unknown Source)"; |
ohair@286 | 430 | // just like stack trace, we just print the file name and |
ohair@286 | 431 | // not the whole path. The idea is that the package name should |
ohair@286 | 432 | // provide enough clue on which directory it lives. |
ohair@286 | 433 | CompilationUnitTree compilationUnit = treePath.getCompilationUnit(); |
ohair@286 | 434 | Trees trees = Trees.instance(env); |
ohair@286 | 435 | long startPosition = trees.getSourcePositions().getStartPosition(compilationUnit, treePath.getLeaf()); |
ohair@286 | 436 | return name + "(" + |
ohair@286 | 437 | compilationUnit.getSourceFile().getName() + ":" + compilationUnit.getLineMap().getLineNumber(startPosition) + |
ohair@286 | 438 | ")"; |
ohair@286 | 439 | } |
ohair@286 | 440 | }; |
ohair@286 | 441 | } |
ohair@286 | 442 | |
ohair@286 | 443 | /** |
ohair@286 | 444 | * Implements {@link #getBaseClass}. |
ohair@286 | 445 | */ |
ohair@286 | 446 | private final SimpleTypeVisitor6<TypeMirror, TypeElement> baseClassFinder = new SimpleTypeVisitor6<TypeMirror, TypeElement>() { |
ohair@286 | 447 | @Override |
ohair@286 | 448 | public TypeMirror visitDeclared(DeclaredType t, TypeElement sup) { |
ohair@286 | 449 | if (t.asElement().equals(sup)) |
ohair@286 | 450 | return t; |
ohair@286 | 451 | |
ohair@286 | 452 | for (TypeMirror i : env.getTypeUtils().directSupertypes(t)) { |
ohair@286 | 453 | TypeMirror r = visitDeclared((DeclaredType) i, sup); |
ohair@286 | 454 | if (r != null) |
ohair@286 | 455 | return r; |
ohair@286 | 456 | } |
ohair@286 | 457 | |
ohair@286 | 458 | // otherwise recursively apply super class and base types |
ohair@286 | 459 | TypeMirror superclass = ((TypeElement) t.asElement()).getSuperclass(); |
ohair@286 | 460 | if (!superclass.getKind().equals(TypeKind.NONE)) { |
ohair@286 | 461 | TypeMirror r = visitDeclared((DeclaredType) superclass, sup); |
ohair@286 | 462 | if (r != null) |
ohair@286 | 463 | return r; |
ohair@286 | 464 | } |
ohair@286 | 465 | return null; |
ohair@286 | 466 | } |
ohair@286 | 467 | |
ohair@286 | 468 | @Override |
ohair@286 | 469 | public TypeMirror visitTypeVariable(TypeVariable t, TypeElement typeElement) { |
ohair@286 | 470 | // we are checking if T (declared as T extends A&B&C) is assignable to sup. |
ohair@286 | 471 | // so apply bounds recursively. |
ohair@286 | 472 | for (TypeMirror typeMirror : ((TypeParameterElement) t.asElement()).getBounds()) { |
ohair@286 | 473 | TypeMirror m = visit(typeMirror, typeElement); |
ohair@286 | 474 | if (m != null) |
ohair@286 | 475 | return m; |
ohair@286 | 476 | } |
ohair@286 | 477 | return null; |
ohair@286 | 478 | } |
ohair@286 | 479 | |
ohair@286 | 480 | @Override |
ohair@286 | 481 | public TypeMirror visitArray(ArrayType t, TypeElement typeElement) { |
ohair@286 | 482 | // we are checking if t=T[] is assignable to sup. |
ohair@286 | 483 | // the only case this is allowed is sup=Object, |
ohair@286 | 484 | // and Object isn't parameterized. |
ohair@286 | 485 | return null; |
ohair@286 | 486 | } |
ohair@286 | 487 | |
ohair@286 | 488 | @Override |
ohair@286 | 489 | public TypeMirror visitWildcard(WildcardType t, TypeElement typeElement) { |
ohair@286 | 490 | // we are checking if T (= ? extends A&B&C) is assignable to sup. |
ohair@286 | 491 | // so apply bounds recursively. |
ohair@286 | 492 | return visit(t.getExtendsBound(), typeElement); |
ohair@286 | 493 | } |
ohair@286 | 494 | |
ohair@286 | 495 | @Override |
ohair@286 | 496 | protected TypeMirror defaultAction(TypeMirror e, TypeElement typeElement) { |
ohair@286 | 497 | return e; |
ohair@286 | 498 | } |
ohair@286 | 499 | }; |
ohair@286 | 500 | } |