/** * Turns an annotation AST node into an actual instance of an annotation class provided you already know its type. * <strong>NB: non-literal compile-time constants cannot be converted, and you should avoid querying classes; * instead call {@code resolver.getAnnotationClassAsString(objectReturnedByThisMethod, "annotation method name")}. * * @see #getAnnotationClassesAsStrings(java.lang.annotation.Annotation, String) * @see #getAnnotationClassAsString(java.lang.annotation.Annotation, String) */ public <A extends java.lang.annotation.Annotation> A toAnnotationInstance(Class<A> type, Annotation node) { return type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class[] { type }, new AnnotationProxy(this, node))); }
@SuppressWarnings("unchecked") private boolean tryAsEnum(Node val, Class<?> expectedType, List<Object> returnValues) { if (!expectedType.isEnum()) return false; returnValues.add(this.resolver.resolveEnum(expectedType.asSubclass(Enum.class), val)); return true; }
/** * Use together with {@link #toAnnotationInstance(Class, Annotation)}. * * @see #getAnnotationClassesAsStrings(java.lang.annotation.Annotation, String) */ public String getAnnotationClassAsString(java.lang.annotation.Annotation annotation, String methodName) { try { Method m = annotation.getClass().getMethod(methodName); if (m.getReturnType() != Class.class) throw new IllegalArgumentException("Method " + methodName + " does not have 'Class' as return type"); try { return Class.class.cast(m.invoke(annotation)).toString(); } catch (AnnotationClassNotAvailableException e) { return e.getClassName(); } } catch (IllegalAccessException e) { throw new IllegalArgumentException("Method " + methodName + " isn't accessible", e); } catch (InvocationTargetException e) { throw new IllegalArgumentException("Method " + methodName + " cannot be invoked", e); } catch (NoSuchMethodException e) { throw new IllegalArgumentException("Method " + methodName + " does not exist"); } }
if (!(elem.astName() == null && name.equals("value")) && !name.equals(elem.astName())) continue; for (Node val : elem.getValues()) { if (tryAsNumeric(val, expectedType, returnValues)) continue; if (tryAsBoolean(val, expectedType, returnValues)) continue; if (tryAsString(val, expectedType, returnValues)) continue; if (tryAsEnum(val, expectedType, returnValues)) continue; if (expectedType == Class.class) { if (val instanceof ClassLiteral) { continue; } catch (ClassNotFoundException e) { classNotAvailable = new AnnotationClassNotAvailableException(val, className); } finally { classNames.add(className); returnValues.add(this.resolver.toAnnotationInstance(expectedType.asSubclass(java.lang.annotation.Annotation.class), (Annotation)val)); continue; } else { classNotAvailable.setClassNames(classNames); throw classNotAvailable;
private ImportList getImportList(Node n) { ImportList il = new ImportList(); while (n != null) { if (n instanceof CompilationUnit) { CompilationUnit cu = (CompilationUnit) n; PackageDeclaration pkg = cu.astPackageDeclaration(); if (pkg != null) il.stars.add(pkg.getPackageName()); for (ImportDeclaration imp : cu.astImportDeclarations()) { if (imp.astStaticImport()) continue; if (imp.astStarImport()) { String i = imp.asFullyQualifiedName(); i = i.substring(0, i.length() - 2); il.stars.add(i); } else { il.explicits.add(imp.asFullyQualifiedName()); } } } n = n.getParent(); } return il; }
return result; } catch (AnnotationClassNotAvailableException e) { return e.getClassNames();
String packageName = null; if (value instanceof Select) { List<String> chain = unwrapSelectChain((Select) value); switch (chain.size()) { case 0:
if (!(elem.astName() == null && name.equals("value")) && !name.equals(elem.astName())) continue; for (Node val : elem.getValues()) { if (tryAsNumeric(val, expectedType, returnValues)) continue; if (tryAsBoolean(val, expectedType, returnValues)) continue; if (tryAsString(val, expectedType, returnValues)) continue; if (tryAsEnum(val, expectedType, returnValues)) continue; if (expectedType == Class.class) { if (val instanceof ClassLiteral) { continue; } catch (ClassNotFoundException e) { classNotAvailable = new AnnotationClassNotAvailableException(val, className); } finally { classNames.add(className); returnValues.add(this.resolver.toAnnotationInstance(expectedType.asSubclass(java.lang.annotation.Annotation.class), (Annotation)val)); continue; } else { classNotAvailable.setClassNames(classNames); throw classNotAvailable;
/** * Turns an annotation AST node into an actual instance of an annotation class provided you already know its type. * <strong>NB: non-literal compile-time constants cannot be converted, and you should avoid querying classes; * instead call {@code resolver.getAnnotationClassAsString(objectReturnedByThisMethod, "annotation method name")}. * * @see #getAnnotationClassesAsStrings(java.lang.annotation.Annotation, String) * @see #getAnnotationClassAsString(java.lang.annotation.Annotation, String) */ public <A extends java.lang.annotation.Annotation> A toAnnotationInstance(Class<A> type, Annotation node) { return type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class[] { type }, new AnnotationProxy(this, node))); }
@SuppressWarnings("unchecked") private boolean tryAsEnum(Node val, Class<?> expectedType, List<Object> returnValues) { if (!expectedType.isEnum()) return false; returnValues.add(this.resolver.resolveEnum(expectedType.asSubclass(Enum.class), val)); return true; }
/** * Use together with {@link #toAnnotationInstance(Class, Annotation)}. * * @see #getAnnotationClassesAsStrings(java.lang.annotation.Annotation, String) */ public String getAnnotationClassAsString(java.lang.annotation.Annotation annotation, String methodName) { try { Method m = annotation.getClass().getMethod(methodName); if (m.getReturnType() != Class.class) throw new IllegalArgumentException("Method " + methodName + " does not have 'Class' as return type"); try { return Class.class.cast(m.invoke(annotation)).toString(); } catch (AnnotationClassNotAvailableException e) { return e.getClassName(); } } catch (IllegalAccessException e) { throw new IllegalArgumentException("Method " + methodName + " isn't accessible", e); } catch (InvocationTargetException e) { throw new IllegalArgumentException("Method " + methodName + " cannot be invoked", e); } catch (NoSuchMethodException e) { throw new IllegalArgumentException("Method " + methodName + " does not exist"); } }
private ImportList getImportList(Node n) { ImportList il = new ImportList(); while (n != null) { if (n instanceof CompilationUnit) { CompilationUnit cu = (CompilationUnit) n; PackageDeclaration pkg = cu.astPackageDeclaration(); if (pkg != null) il.stars.add(pkg.getPackageName()); for (ImportDeclaration imp : cu.astImportDeclarations()) { if (imp.astStaticImport()) continue; if (imp.astStarImport()) { String i = imp.asFullyQualifiedName(); i = i.substring(0, i.length() - 2); il.stars.add(i); } else { il.explicits.add(imp.asFullyQualifiedName()); } } } n = n.getParent(); } return il; }
return result; } catch (AnnotationClassNotAvailableException e) { return e.getClassNames();
String packageName = null; if (value instanceof Select) { List<String> chain = unwrapSelectChain((Select) value); switch (chain.size()) { case 0:
if (!(elem.astName() == null && name.equals("value")) && !name.equals(elem.astName())) continue; for (Node val : elem.getValues()) { if (tryAsNumeric(val, expectedType, returnValues)) continue; if (tryAsBoolean(val, expectedType, returnValues)) continue; if (tryAsString(val, expectedType, returnValues)) continue; if (tryAsEnum(val, expectedType, returnValues)) continue; if (expectedType == Class.class) { if (val instanceof ClassLiteral) { continue; } catch (ClassNotFoundException e) { classNotAvailable = new AnnotationClassNotAvailableException(val, className); } finally { classNames.add(className); returnValues.add(this.resolver.toAnnotationInstance(expectedType.asSubclass(java.lang.annotation.Annotation.class), (Annotation)val)); continue; } else { classNotAvailable.setClassNames(classNames); throw classNotAvailable;
/** * Turns an annotation AST node into an actual instance of an annotation class provided you already know its type. * <strong>NB: non-literal compile-time constants cannot be converted, and you should avoid querying classes; * instead call {@code resolver.getAnnotationClassAsString(objectReturnedByThisMethod, "annotation method name")}. * * @see #getAnnotationClassesAsStrings(java.lang.annotation.Annotation, String) * @see #getAnnotationClassAsString(java.lang.annotation.Annotation, String) */ public <A extends java.lang.annotation.Annotation> A toAnnotationInstance(Class<A> type, Annotation node) { return type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class[] { type }, new AnnotationProxy(this, node))); }
@SuppressWarnings("unchecked") private boolean tryAsEnum(Node val, Class<?> expectedType, List<Object> returnValues) { if (!expectedType.isEnum()) return false; returnValues.add(this.resolver.resolveEnum(expectedType.asSubclass(Enum.class), val)); return true; }
/** * Use together with {@link #toAnnotationInstance(Class, Annotation)}. * * @see #getAnnotationClassesAsStrings(java.lang.annotation.Annotation, String) */ public String getAnnotationClassAsString(java.lang.annotation.Annotation annotation, String methodName) { try { Method m = annotation.getClass().getMethod(methodName); if (m.getReturnType() != Class.class) throw new IllegalArgumentException("Method " + methodName + " does not have 'Class' as return type"); try { return Class.class.cast(m.invoke(annotation)).toString(); } catch (AnnotationClassNotAvailableException e) { return e.getClassName(); } } catch (IllegalAccessException e) { throw new IllegalArgumentException("Method " + methodName + " isn't accessible", e); } catch (InvocationTargetException e) { throw new IllegalArgumentException("Method " + methodName + " cannot be invoked", e); } catch (NoSuchMethodException e) { throw new IllegalArgumentException("Method " + methodName + " does not exist"); } }
private ImportList getImportList(Node n) { ImportList il = new ImportList(); while (n != null) { if (n instanceof CompilationUnit) { CompilationUnit cu = (CompilationUnit) n; PackageDeclaration pkg = cu.astPackageDeclaration(); if (pkg != null) il.stars.add(pkg.getPackageName()); for (ImportDeclaration imp : cu.astImportDeclarations()) { if (imp.astStaticImport()) continue; if (imp.astStarImport()) { String i = imp.asFullyQualifiedName(); i = i.substring(0, i.length() - 2); il.stars.add(i); } else { il.explicits.add(imp.asFullyQualifiedName()); } } } n = n.getParent(); } return il; }
return result; } catch (AnnotationClassNotAvailableException e) { return e.getClassNames();