@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { boolean isSuccessfulProcess = true; for (Element element : roundEnv.getElementsAnnotatedWith(View.class)) { try { final ViewElement viewElement = new ViewElement(environment, element); if (!validator.isViewElementValid(viewElement)) { isSuccessfulProcess = false; } else { environment.addViewElement(viewElement); } } catch (AnnotationTypeMismatchException ex) { environment.error(element, String.format("@%s and @%s annotation attribute values must be self defined constant expressions", View.class.getSimpleName(), ViewColumn.class.getSimpleName())); return false; } catch (Exception e) { environment.error(element, "View collection error = " + e.getMessage()); e.printStackTrace(); return false; } } return isSuccessfulProcess; }
/** * Validates contained value against its member definition * and if ok returns the value. * Otherwise, if the value type mismatches definition * or the value itself describes an error, * throws appropriate exception. * <br> Note, this method may return null if this element was constructed * with such value. * * @see #rethrowError() * @see #copyValue() * @return actual valid value or null if no value */ public Object validateValue() throws Throwable { if (tag == ERROR) { rethrowError(); } if (value == NO_VALUE) { return null; } if (elementType == value.getClass() || elementType.isInstance(value)) { // nested annotation value return copyValue(); } else { throw new AnnotationTypeMismatchException(definingMethod, value.getClass().getName()); } }
public void testSerialization() throws Exception { Method m = String.class.getMethod("length"); AnnotationTypeMismatchException original = new AnnotationTypeMismatchException(m, "poop"); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { // AnnotationTypeMismatchException is broken: it's Serializable but has a non-transient // non-serializable field of type Method. new ObjectOutputStream(out).writeObject(original); fail(); } catch (NotSerializableException expected) { } }
/** * @throws ClassNotFoundException * @throws SecurityException * @tests java.lang.annotation.AnnotationTypeMismatchException#AnnotationTypeMismatchException(Method, * String) */ @SuppressWarnings("nls") public void test_constructorLjava_lang_reflect_MethodLjava_lang_String() throws SecurityException, ClassNotFoundException { Method[] methods = Class.forName("java.lang.String").getMethods(); Method m = methods[0]; AnnotationTypeMismatchException e = new AnnotationTypeMismatchException( m, "some type"); assertNotNull("can not instantiate AnnotationTypeMismatchException", e); assertSame("wrong method name", m, e.element()); assertEquals("wrong found type", "some type", e.foundType()); }
@Override public void validate() { if (entityClass == null) { throw new IllegalArgumentException("testentity class cannot be null"); } if (!entityClass.isAnnotationPresent(DeepEntity.class)) { throw new AnnotationTypeMismatchException(null, entityClass.getCanonicalName()); } super.validate(); /* let's validate fieldNames in @DeepField annotations */ Field[] deepFields = AnnotationUtils.filterDeepFields(entityClass); Map<String, Cell> colDefs = super.columnDefinitions(); /* colDefs is null if table does not exist. I.E. this configuration will be used as an output configuration object, and the output table is dynamically created */ if (colDefs == null) { return; } for (Field field : deepFields) { String annotationFieldName = AnnotationUtils.deepFieldName(field); if (!colDefs.containsKey(annotationFieldName)) { throw new DeepNoSuchFieldException("Unknown column name \'" + annotationFieldName + "\' specified for" + " field " + entityClass.getCanonicalName() + "#" + field.getName() + ". Please, " + "make sure the field name you specify in @DeepField annotation matches _exactly_ the column " + "name " + "in the database"); } } }
/** * @throws ClassNotFoundException * @throws SecurityException * @tests java.lang.annotation.AnnotationTypeMismatchException#AnnotationTypeMismatchException(Method, * String) */ @SuppressWarnings("nls") public void test_constructorLjava_lang_reflect_MethodLjava_lang_String() throws SecurityException, ClassNotFoundException { Method[] methods = Class.forName("java.lang.String").getMethods(); Method m = methods[0]; AnnotationTypeMismatchException e = new AnnotationTypeMismatchException( m, "some type"); assertNotNull("can not instanciate AnnotationTypeMismatchException", e); assertSame("wrong method name", m, e.element()); assertEquals("wrong found type", "some type", e.foundType()); }
@Test(expected = AnnotationTypeMismatchException.class) @SuppressWarnings("unchecked") public void testAnnotationTypeMismatchException() throws Throwable { when(loadedAnnotationValue.resolve()).thenReturn(new Object()); Proxy.getInvocationHandler(AnnotationDescription.AnnotationInvocationHandler.of(getClass().getClassLoader(), Foo.class, Collections.<String, AnnotationValue<?, ?>>singletonMap(FOO, annotationValue))) .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"); }
public void testGetters() throws Exception { Method m = String.class.getMethod("length"); AnnotationTypeMismatchException ex = new AnnotationTypeMismatchException(m, "poop"); assertSame(m, ex.element()); assertEquals("poop", ex.foundType()); }