/** * Returns <jk>true</jk> if <code>parent</code> is a parent class or the same as <code>child</code>. * * @param parent The parent class. * @param child The child class. * @return <jk>true</jk> if <code>parent</code> is a parent class or the same as <code>child</code>. */ public static boolean isParentClass(Class<?> parent, Type child) { if (child instanceof Class) return isParentClass(parent, (Class<?>)child); return false; }
RemoteMethodBeanArg(int index, Class<? extends HttpPartSerializer> serializer, RequestBeanMeta meta) { this.index = index; this.serializer = newInstance(HttpPartSerializer.class, serializer); this.meta = meta; }
/** * Returns <jk>true</jk> if the {@link #getAnnotation(Class, Method)} returns a value. * * @param a The annotation to check for. * @param m The method to check. * @return <jk>true</jk> if the {@link #getAnnotation(Class, Method)} returns a value. */ public static boolean hasAnnotation(Class<? extends Annotation> a, Method m) { return getAnnotation(a, m) != null; }
private static List<Method> findMatchingMethods(List<Method> l, Method m) { l.add(m); Class<?> c = m.getDeclaringClass(); Class<?> pc = c.getSuperclass(); if (pc != null) for (Method m2 : pc.getDeclaredMethods()) if (isSameMethod(m, m2)) findMatchingMethods(l, m2); for (Class<?> ic : c.getInterfaces()) for (Method m2 : ic.getDeclaredMethods()) if (isSameMethod(m, m2)) findMatchingMethods(l, m2); return l; }
Builder apply(Type t) { Class<?> c = ClassUtils.toClass(t); this.cm = BeanContext.DEFAULT.getClassMeta(c); for (Method m : ClassUtils.getAllMethods(c, false)) { if (isPublic(m)) { assertNoAnnotations(m, Response.class, Body.class, Header.class, Query.class, FormData.class, Path.class); if (hasAnnotation(ResponseHeader.class, m)) { assertNoArgs(m, ResponseHeader.class); assertReturnNotVoid(m, ResponseHeader.class); HttpPartSchema s = HttpPartSchema.create(getAnnotation(ResponseHeader.class, m), getPropertyName(m)); headerMethods.put(s.getName(), ResponseBeanPropertyMeta.create(RESPONSE_HEADER, s, m)); } else if (hasAnnotation(ResponseStatus.class, m)) { assertNoArgs(m, ResponseHeader.class); assertReturnType(m, ResponseHeader.class, int.class, Integer.class); statusMethod = ResponseBeanPropertyMeta.create(RESPONSE_STATUS, m); } else if (hasAnnotation(ResponseBody.class, m)) { Class<?>[] pt = m.getParameterTypes(); if (pt.length == 0) assertReturnNotVoid(m, ResponseHeader.class); else assertArgType(m, ResponseHeader.class, OutputStream.class, Writer.class); bodyMethod = ResponseBeanPropertyMeta.create(RESPONSE_BODY, m); } } } return this; }
for (Iterator<Class<?>> i = ClassUtils.getParentClasses(ic, false, true); i.hasNext(); ) { Class pic = i.next(); t = m.get(pic); final Class<?> oc2 = hasPrimitiveWrapper(oc) ? getPrimitiveWrapper(oc) : oc; if (oc2.isEnum()) { t = new Transform<String,O>() { final Method fromStringMethod = findPublicFromStringMethod(oc2); if (fromStringMethod != null) { t = new Transform<String,O>() { Method createMethod = findPublicStaticCreateMethod(oc, ic, "create"); if (createMethod == null) createMethod = findPublicStaticCreateMethod(oc, ic, "from" + ic.getSimpleName()); if (createMethod != null) { final Method cm = createMethod; final Constructor<?> c = findPublicConstructor(oc, ic); final boolean isMemberClass = oc.isMemberClass() && ! isStatic(oc); if (c != null && ! isDeprecated(c)) { t = new Transform<I,O>() { @Override for (Method m2 : getAllMethods(ic, false)) { if (isAll(m2, PUBLIC, NOT_STATIC, HAS_NO_ARGS, NOT_DEPRECATED) && m2.getName().startsWith("to") && m2.getReturnType() == oc) { final Method m3 = m2; t = new Transform<I,O>() {
for (Method m : ClassUtils.getAllMethods(this.resourceClass, true)) { if (m.isAnnotationPresent(RestHook.class) && m.getAnnotation(RestHook.class).value() == HookEvent.INIT) { setAccessible(m, false); String sig = ClassUtils.getMethodSignature(m); if (! map.containsKey(sig)) map.put(sig, m); ClassUtils.assertArgsOfType(m, RestContextBuilder.class, ServletConfig.class); Class<?>[] pt = m.getParameterTypes(); Object[] args = new Object[pt.length];
/** * Shortcut for calling <code>getAnnotations(a, m, index, <jk>true</jk>, <jk>true</jk>, <jk>false</jk>);</code> * * @param a The annotation to look for. * @param m The method containing the parameter. * @param index The parameter index. * @return All instances of the annotation with the */ public static <T extends Annotation> List<T> getAnnotations(Class<T> a, Method m, int index) { return getAnnotations(a, m, index, true, true, false); }
/** * Finds the annotation of the specified type defined on the specified method. * * @param a * The annotation to search for. * @param m * The method to find the annotation on. * @param searchParentMethods * If <jk>true</jk>, searches methods with the same signature on the parent classes or interfaces. * <br>The search is performed in child-to-parent order. * @param searchReturnType * If <jk>true</jk>, searches the return type on the method for the specified annotation. * @return * The annotation if found, or <jk>null</jk> if not. */ @SuppressWarnings("unchecked") public static <T extends Annotation> T getAnnotation(Class<T> a, Method m, boolean searchParentMethods, boolean searchReturnType) { List<Method> methods = searchParentMethods ? findMatchingMethods(m) : Collections.singletonList(m); for (Method m2 : methods) for (Annotation a2 : m2.getAnnotations()) if (a.isInstance(a2)) return (T)a2; if (searchReturnType) { Type t = m.getGenericReturnType(); if (Value.isType(t)) return getAnnotation(a, Value.getParameterType(t)); return getAnnotation(a, t); } return null; }
@SafeVarargs static final void assertNoAnnotations(Method m, Class<? extends Annotation> a, Class<? extends Annotation>...c) throws InvalidAnnotationException { for (Class<? extends Annotation> cc : c) { if (hasAnnotation(cc, m)) throw new InvalidAnnotationException("@{0} annotation cannot be used in a @{1} bean. Method=''{2}''", cc.getSimpleName(), a.getSimpleName(), m); } }
/** * Given a Java method, returns the arguments signature. * * @param m The Java method. * @param full Whether fully-qualified names should be used for arguments. * @return The arguments signature for the specified method. */ public static String getMethodArgsSignature(Method m, boolean full) { StringBuilder sb = new StringBuilder(); Class<?>[] pt = m.getParameterTypes(); if (pt.length == 0) return ""; sb.append('('); for (int i = 0; i < pt.length; i++) { if (i > 0) sb.append(','); sb.append(full ? ClassUtils.getReadableClassName(pt[i]) : ClassUtils.getSimpleName(pt[i])); } sb.append(')'); return sb.toString(); } }
HttpPartSchemaBuilder apply(Class<? extends Annotation> c, java.lang.reflect.Type t) { if (t instanceof Class<?>) { Class<?> tc = (Class<?>)t; for (Annotation a : ClassUtils.getAnnotationsParentFirst(c, tc)) apply(a); } else if (Value.isType(t)) { apply(c, Value.getParameterType(t)); } return this; }
/** * Returns the simple name of a class. * * <p> * Similar to {@link Class#getSimpleName()}, but includes the simple name of an enclosing or declaring class. * * @param c The class to get the simple name on. * @return The simple name of a class. */ public static String getSimpleName(Class<?> c) { if (c.isLocalClass()) return getSimpleName(c.getEnclosingClass()) + '.' + c.getSimpleName(); if (c.isMemberClass()) return getSimpleName(c.getDeclaringClass()) + '.' + c.getSimpleName(); return c.getSimpleName(); }
/** * Constructor. * * @param c The interface class annotated with a {@link RemoteResource @RemoteResource} annotation (optional). */ public RemoteResourceMeta(Class<?> c) { String path = ""; for (RemoteResource r : getAnnotationsParentFirst(RemoteResource.class, c)) if (! r.path().isEmpty()) path = trimSlashes(r.path()); Map<Method,RemoteMethodMeta> methods = new LinkedHashMap<>(); for (Method m : c.getMethods()) if (isPublic(m)) methods.put(m, new RemoteMethodMeta(path, m, false, "GET")); this.methods = unmodifiableMap(methods); this.path = path; }
@Test public void testGetReadableClassNames() throws Exception { assertEquals("['java.lang.String','java.lang.Integer','java.lang.Boolean','null']", getReadableClassNames(new Object[]{"a",1,true,null}).toString()); }
/** * Given a specific method, finds all declared methods with the same name and arguments on all * superclasses and interfaces. * * @param m The method to find matches against. * @return * All matching methods including the input method itself. * Methods are ordered from child-to-parent order. */ public static List<Method> findMatchingMethods(Method m) { return findMatchingMethods(new ArrayList<Method>(), m); }
@Test public void getParentMethodsParentFirst() throws Exception { Set<String> s = new TreeSet<>(); for (Method m : ClassUtils.getAllMethods(DD.class, true)) if (! m.getName().startsWith("$")) s.add(m.getDeclaringClass().getSimpleName() + '.' + m.getName()); assertObjectEquals("['DA1.da1','DA2.da2','DB.da1','DB.db','DC.da2','DC.dc','DD.da2','DD.dd']", s); s = new TreeSet<>(); for (Method m : ClassUtils.getAllMethods(DD.class, false)) if (! m.getName().startsWith("$")) s.add(m.getDeclaringClass().getSimpleName() + '.' + m.getName()); assertObjectEquals("['DA1.da1','DA2.da2','DB.da1','DB.db','DC.da2','DC.dc','DD.da2','DD.dd']", s); }
Builder apply(Type t) { Class<?> c = ClassUtils.toClass(t); this.cm = BeanContext.DEFAULT.getClassMeta(c); for (Method m : ClassUtils.getAllMethods(c, false)) { if (isPublic(m)) { assertNoAnnotations(m, Response.class, Body.class, Header.class, Query.class, FormData.class, Path.class); if (hasAnnotation(ResponseHeader.class, m)) { assertNoArgs(m, ResponseHeader.class); assertReturnNotVoid(m, ResponseHeader.class); HttpPartSchema s = HttpPartSchema.create(getAnnotation(ResponseHeader.class, m), getPropertyName(m)); headerMethods.put(s.getName(), ResponseBeanPropertyMeta.create(RESPONSE_HEADER, s, m)); } else if (hasAnnotation(ResponseStatus.class, m)) { assertNoArgs(m, ResponseHeader.class); assertReturnType(m, ResponseHeader.class, int.class, Integer.class); statusMethod = ResponseBeanPropertyMeta.create(RESPONSE_STATUS, m); } else if (hasAnnotation(ResponseBody.class, m)) { Class<?>[] pt = m.getParameterTypes(); if (pt.length == 0) assertReturnNotVoid(m, ResponseHeader.class); else assertArgType(m, ResponseHeader.class, OutputStream.class, Writer.class); bodyMethod = ResponseBeanPropertyMeta.create(RESPONSE_BODY, m); } } } return this; }
for (Method m : ClassUtils.getAllMethods(this.resourceClass, true)) { if (m.isAnnotationPresent(RestHook.class) && m.getAnnotation(RestHook.class).value() == HookEvent.INIT) { setAccessible(m, false); String sig = ClassUtils.getMethodSignature(m); if (! map.containsKey(sig)) map.put(sig, m); ClassUtils.assertArgsOfType(m, RestContextBuilder.class, ServletConfig.class); Class<?>[] pt = m.getParameterTypes(); Object[] args = new Object[pt.length];
/** * Shortcut for calling <code>getAnnotations(a,m,<jk>true</jk>,<jk>true</jk>,<jk>true</jk>)</code> * * @param a The annotation to look for. * @param m The method being inspected. * @return All instances of the annotation with the */ public static <T extends Annotation> List<T> getAnnotationsParentFirst(Class<T> a, Method m) { return getAnnotations(a, m, true, true, true); }