java.lang.annotation.AnnotationTypeMismatchException Java Examples

The following examples show how to use java.lang.annotation.AnnotationTypeMismatchException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ViewCollectionStep.java    From sqlitemagic with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #2
Source File: TestExtensionLoader.java    From jenkins-test-harness with MIT License 5 votes vote down vote up
@Override
protected boolean isActive(AnnotatedElement e) {
    TestEnvironment env = TestEnvironment.get();
    if (env == null) {
        return false;
    }

    TestExtension a = e.getAnnotation(TestExtension.class);
    if (a==null)        return false;   // stale index
    List<String> testNameList;
    try {
        testNameList = Arrays.asList(a.value());
    } catch (AnnotationTypeMismatchException x) {
        LOGGER.log(Level.WARNING, "ignoring {0} compiled against jenkins-test-harness older than 2.16", e);
        return false;
    }
    Description description = env.description();
    if (!testNameList.isEmpty() && !testNameList.contains(description.getMethodName()))
        return false;   // doesn't apply to this test
    String className = description.getClassName();
    if (e instanceof Class) {
        for (Class<?> outer = (Class) e; outer != null; outer = outer.getEnclosingClass()) {
            if (outer.getName().equals(className)) {
                return true;      // enclosed
            }
        }
        return false;
    }
    if (e instanceof Field) {
        Field f = (Field) e;
        return f.getDeclaringClass().getName().equals(className);
    }
    if (e instanceof Method) {
        Method m = (Method) e;
        return m.getDeclaringClass().getName().equals(className);
    }
    return false;
}
 
Example #3
Source File: AnnotationTypeMismatchExceptionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * @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 {
  if (!ReflectionUtil.isJreReflectionStripped()) {
    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());
  }
}
 
Example #4
Source File: AnnotationTypeMismatchExceptionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
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) {
    }
}
 
Example #5
Source File: EntityDeepJobConfig.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
@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");
        }
    }
}
 
Example #6
Source File: AnnotationTypeMismatchExceptionProxy.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected RuntimeException generateException() {
    return new AnnotationTypeMismatchException(member, foundType);
}
 
Example #7
Source File: AnnotationTypeMismatchExceptionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
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());
}
 
Example #8
Source File: AnnotationValue.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public W resolve() {
    throw new AnnotationTypeMismatchException(property, value);
}
 
Example #9
Source File: AnnotationDescriptionLatentTest.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private static AnnotationDescription build(Annotation annotation) throws Exception {
    AnnotationDescription.Builder builder = AnnotationDescription.Builder.ofType(annotation.annotationType());
    for (Method method : annotation.annotationType().getDeclaredMethods()) {
        try {
            Object value = method.invoke(annotation);
            if (value instanceof Annotation) {
                builder = builder.define(method.getName(), (Annotation) value);
            } else if (value instanceof Annotation[]) {
                builder = builder.defineAnnotationArray(method.getName(), (Class) method.getReturnType().getComponentType(), (Annotation[]) value);
            } else if (value instanceof Enum<?>) {
                builder = builder.define(method.getName(), (Enum<?>) value);
            } else if (value instanceof Enum<?>[]) {
                builder = builder.defineEnumerationArray(method.getName(), (Class) method.getReturnType().getComponentType(), (Enum[]) value);
            } else if (value instanceof Class<?>) {
                builder = builder.define(method.getName(), (Class<?>) value);
            } else if (value instanceof Class<?>[]) {
                builder = builder.defineTypeArray(method.getName(), (Class<?>[]) value);
            } else if (value instanceof String) {
                builder = builder.define(method.getName(), (String) value);
            } else if (value instanceof String[]) {
                builder = builder.defineArray(method.getName(), (String[]) value);
            } else if (value instanceof Boolean) {
                builder = builder.define(method.getName(), (Boolean) value);
            } else if (value instanceof Byte) {
                builder = builder.define(method.getName(), (Byte) value);
            } else if (value instanceof Character) {
                builder = builder.define(method.getName(), (Character) value);
            } else if (value instanceof Short) {
                builder = builder.define(method.getName(), (Short) value);
            } else if (value instanceof Integer) {
                builder = builder.define(method.getName(), (Integer) value);
            } else if (value instanceof Long) {
                builder = builder.define(method.getName(), (Long) value);
            } else if (value instanceof Float) {
                builder = builder.define(method.getName(), (Float) value);
            } else if (value instanceof Double) {
                builder = builder.define(method.getName(), (Double) value);
            } else if (value instanceof boolean[]) {
                builder = builder.defineArray(method.getName(), (boolean[]) value);
            } else if (value instanceof byte[]) {
                builder = builder.defineArray(method.getName(), (byte[]) value);
            } else if (value instanceof char[]) {
                builder = builder.defineArray(method.getName(), (char[]) value);
            } else if (value instanceof short[]) {
                builder = builder.defineArray(method.getName(), (short[]) value);
            } else if (value instanceof int[]) {
                builder = builder.defineArray(method.getName(), (int[]) value);
            } else if (value instanceof long[]) {
                builder = builder.defineArray(method.getName(), (long[]) value);
            } else if (value instanceof float[]) {
                builder = builder.defineArray(method.getName(), (float[]) value);
            } else if (value instanceof double[]) {
                builder = builder.defineArray(method.getName(), (double[]) value);
            } else {
                throw new IllegalArgumentException("Unexpected annotation property: " + method);
            }
        } catch (InvocationTargetException exception) {
            Throwable cause = exception.getCause();
            if (cause instanceof TypeNotPresentException) {
                builder = builder.define(method.getName(), new AnnotationValue.ForMissingType<Void, Void>(((TypeNotPresentException) cause).typeName()));
            } else if (cause instanceof EnumConstantNotPresentException) {
                builder = builder.define(method.getName(), new AnnotationValue.ForEnumerationDescription.WithUnknownConstant(
                        new TypeDescription.ForLoadedType(((EnumConstantNotPresentException) cause).enumType()),
                        ((EnumConstantNotPresentException) cause).constantName()));
            } else if (cause instanceof AnnotationTypeMismatchException) {
                builder = builder.define(method.getName(), new AnnotationValue.ForMismatchedType<Void, Void>(
                        new MethodDescription.ForLoadedMethod(((AnnotationTypeMismatchException) cause).element()),
                        ((AnnotationTypeMismatchException) cause).foundType()));
            } else if (!(cause instanceof IncompleteAnnotationException)) {
                throw exception;
            }
        }
    }
    return builder.build(false);
}