/** * Extracts the type level mapping if any. Makes sure that at least "/" is mapped and restricted to the given methods, if there any. * * @param parentRequestMapping * @return */ Map<String, List<RequestMethod>> extractTypeLevelMappings(final RequestMapping parentRequestMapping) { final Map<String, List<RequestMethod>> parentUrls = new TreeMap<>(); List<String> urls = new ArrayList<>(); List<RequestMethod> methods = new ArrayList<>(); if (parentRequestMapping != null) { try { urls = concatValues(parentRequestMapping.value(), parentRequestMapping.path()); methods = Arrays.asList(parentRequestMapping.method()); } catch (IncompleteAnnotationException ex) { // ignore as may be thrown while typing annotations } } final List<String> usedUrls = urls.isEmpty() ? Arrays.asList("/") : urls; for (final String url : usedUrls) { final String usedUrl = url.startsWith("/") ? url : "/" + url; parentUrls.put(usedUrl, methods); } return parentUrls; }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String name = method.getName(); Class<?>[] types = method.getParameterTypes(); InvocationHandler proxyMethod = memberProxys.get(name); if (proxyMethod != null) return proxyMethod.invoke(proxy, method, args); if (name.equals("equals") && types.length == 1 && types[0] == Object.class) return equalsImpl(args[0]); if (types.length != 0) throw new AssertionError("Too many parameters for an annotation method"); switch (name) { case "toString": return toStringImpl(); case "hashCode": return hashCodeImpl(); case "annotationType": return type; } Object result = memberValues.get(name); if (result == null) throw new IncompleteAnnotationException(type, name); return result.getClass().isArray() ? Tool.cloneArray(result) : result; }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String methodName = method.getName(); if (S.eq("hashCode", methodName)) { return hashCode(); } else if (S.eq("equals", methodName)) { return equals(args[0]); } else if (S.eq("annotationType", methodName)) { return type; } else if (S.eq("toString", methodName)) { return toString(); } Object result = method.getDefaultValue(); if (result == null) { throw new IncompleteAnnotationException(type, methodName); } return result; }
private String readDescription(Option option, String optionName, Class<?> declaringClass) { try { return option.description(); } catch (IncompleteAnnotationException ex) { throw new OptionValidationException(String.format("No description set on option '%s' at for class '%s'.", optionName, declaringClass.getName())); } }
@SuppressWarnings("unchecked") public static IMergeConflictPolicy getCompositionMergeConflictPolicy(TypeElement element, ProcessingEnvironment env) { Optional<AnnotationValue> value = getParameterFrom(element, Composition.class, "onConflict", env); if (value.isPresent()) { TypeElement typeElement = MoreTypes.asTypeElement((Type) value.get().getValue()); try { return (IMergeConflictPolicy) Class.forName(typeElement.getQualifiedName().toString()).newInstance(); } catch (Exception ignore) { } } throw new IncompleteAnnotationException(Composition.class, "onConflict"); }
@Override public int hashCode() { int result = 0; for (Method m : type.getDeclaredMethods()) { Object o = m.getDefaultValue(); if (null == o) { throw new IncompleteAnnotationException(type, m.getName()); } result += AnnotationUtil.hashMember(m.getName(), o); } return result; }
/** * Processes a method invocation request to this annotation instance. * Recognizes the methods declared in the * {@link java.lang.annotation.Annotation java.lang.annotation.Annotation} * interface, and member-defining methods of the implemented annotation type. * @throws IllegalArgumentException If the specified method is none of the above * @return the invocation result */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String name = method.getName(); Class[] params = method.getParameterTypes(); if (params.length == 0) { if ("annotationType".equals(name)) { return klazz; } else if ("toString".equals(name)) { return toString(); } else if ("hashCode".equals(name)) { return hashCode(); } // this must be element value request AnnotationMember element = null; for (AnnotationMember el : elements) { if (name.equals(el.name)) { element = el; break; } } if (element == null || !method.equals(element.definingMethod)) { throw new IllegalArgumentException(method.toString()); } else { Object value = element.validateValue(); if (value == null) { throw new IncompleteAnnotationException(klazz, name); } return value; } } else if (params.length == 1 && params[0] == Object.class && "equals".equals(name)){ return Boolean.valueOf(equals(args[0])); } throw new IllegalArgumentException( "Invalid method for annotation type: " + method); }
public void testNullType() { try { new IncompleteAnnotationException(null, "str"); fail("NullPointerException must be thrown"); } catch (NullPointerException e) { // Expected } }
/** * @throws Exception * @tests java.lang.annotation.IncompleteAnnotationException#IncompleteAnnotationException(Class, * String) */ @SuppressWarnings("nls") public void test_constructorLjava_lang_Class_Ljava_lang_String() throws Exception { Class clazz = String.class; String elementName = "some element"; IncompleteAnnotationException e = new IncompleteAnnotationException( clazz, elementName); assertNotNull("can not instantiate IncompleteAnnotationException", e); assertSame("wrong annotation type", clazz, e.annotationType()); assertSame("wrong element name", elementName, e.elementName()); }
RetCode taGetFuncInfo(FuncInfo retFuncInfo) { try { retFuncInfo = super.getFuncInfo(); return RetCode.Success; } catch (IncompleteAnnotationException e) { return RetCode.InternalError; } }
public AnnotationHandler(final Class<? extends Annotation> type, final Map<String, ?> values) { this.type = type; methods = Arrays.asList(type.getDeclaredMethods()); for (final Method m : methods) { Object o = values.get(m.getName()); if (o == null) { o = m.getDefaultValue(); } if (o == null) { throw new IncompleteAnnotationException(type, m.getName()); } this.values.put(m.getName(), o); } }
/** * @throws Exception * @tests java.lang.annotation.IncompleteAnnotationException#IncompleteAnnotationException(Class, * String) */ @SuppressWarnings("nls") public void test_constructorLjava_lang_Class_Ljava_lang_String() throws Exception { Class clazz = String.class; String elementName = "some element"; IncompleteAnnotationException e = new IncompleteAnnotationException( clazz, elementName); assertNotNull("can not instanciate IncompleteAnnotationException", e); assertSame("wrong annotation type", clazz, e.annotationType()); assertSame("wrong element name", elementName, e.elementName()); }
@Test(expected = IncompleteAnnotationException.class) @SuppressWarnings("unchecked") public void testIncompleteAnnotationException() throws Throwable { when(freeAnnotationValue.load(getClass().getClassLoader())).thenReturn((AnnotationValue.Loaded) new AnnotationDescription.AnnotationInvocationHandler.MissingValue(Foo.class, "foo")); Proxy.getInvocationHandler(AnnotationDescription.AnnotationInvocationHandler.of(getClass().getClassLoader(), Foo.class, Collections.<String, AnnotationValue<?, ?>>singletonMap(FOO, freeAnnotationValue))) .invoke(new Object(), Foo.class.getDeclaredMethod("foo"), new Object[0]); }
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String methodName = method.getName().intern(); if (args == null || args.length == 0) { if (methodName == "toString") { return toString(); } else if (methodName == "hashCode") { return Integer.valueOf(hashCode()); } else if (methodName == "annotationType") { return type; } else { Object val = memberValues.get(methodName); if (val == null) { throw new IncompleteAnnotationException(type, methodName); } try { if (val.getClass().isArray()) val = coerce((Object[])val, method.getReturnType()); } catch (ArrayStoreException _) { throw new AnnotationTypeMismatchException (method, val.getClass().getName()); } if (! getBoxedReturnType(method).isInstance(val)) throw (new AnnotationTypeMismatchException (method, val.getClass().getName())); return val; } } else if (args.length == 1) { if (methodName == "equals") { return Boolean.valueOf(equals(proxy, args[0])); } } throw new InternalError("Invalid annotation proxy"); }
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String methodName = method.getName().intern(); if (args == null || args.length == 0) { if (methodName == "toString") { return toString(type, memberValues); } else if (methodName == "hashCode") { return Integer.valueOf(hashCode(type, memberValues)); } else if (methodName == "annotationType") { return type; } else { Object val = memberValues.get(methodName); if (val == null) { throw new IncompleteAnnotationException(type, methodName); } if (! getBoxedReturnType(method).isInstance(val)) { throw new AnnotationTypeMismatchException(method, val.getClass().getName()); } if (val.getClass().isArray()) { val = arrayClone(val); } return val; } } else if (args.length == 1) { if (methodName == "equals") { return Boolean.valueOf(equals(type, memberValues, args[0])); } } throw new InternalError("Invalid annotation proxy"); }
static private FuncInfo getFuncInfo(Method method) throws IncompleteAnnotationException { FuncInfo annotation = method.getAnnotation(FuncInfo.class); if (annotation != null) return annotation; throw new IncompleteAnnotationException(FuncInfo.class, "Method " + method.getName()); }
/** * Returns an annotation which describes this TA function. * * @return an @interface FuncInfo * @throws IncompleteAnnotationException */ public FuncInfo getFuncInfo() throws IncompleteAnnotationException { return getFuncInfo(function); }