Search in sources :

Example 1 with IncompleteAnnotationException

use of java.lang.annotation.IncompleteAnnotationException in project robovm by robovm.

the class IncompleteAnnotationExceptionTest method test_constructorLjava_lang_Class_Ljava_lang_String.

/**
     * @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());
}
Also used : IncompleteAnnotationException(java.lang.annotation.IncompleteAnnotationException)

Example 2 with IncompleteAnnotationException

use of java.lang.annotation.IncompleteAnnotationException in project j2objc by google.

the class IncompleteAnnotationExceptionTest method test_constructorLjava_lang_Class_Ljava_lang_String.

/**
 * @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());
}
Also used : IncompleteAnnotationException(java.lang.annotation.IncompleteAnnotationException)

Example 3 with IncompleteAnnotationException

use of java.lang.annotation.IncompleteAnnotationException in project knime-core by knime.

the class JavaToDataCellConverterRegistry method parseAnnotations.

private void parseAnnotations() {
    final Collection<DataType> availableDataTypes = DataTypeRegistry.getInstance().availableDataTypes();
    for (final DataType dataType : availableDataTypes) {
        final Optional<DataCellFactory> cellFactory = dataType.getCellFactory(null);
        if (cellFactory.isPresent()) {
            final Class<? extends DataCellFactory> cellFactoryClass = cellFactory.get().getClass();
            for (final Pair<Method, DataCellFactoryMethod> pair : ClassUtil.getMethodsWithAnnotation(cellFactoryClass, DataCellFactoryMethod.class)) {
                final Method method = pair.getFirst();
                try {
                    final JavaToDataCellConverterFactory<?> factory = new FactoryMethodToDataCellConverterFactory<>(method, ClassUtil.ensureObjectType(method.getParameterTypes()[0]), dataType, pair.getSecond().name());
                    // Check name of factory
                    if (!validateFactoryName(factory)) {
                        LOGGER.warn("JavaToDataCellFactory name \"" + factory.getName() + "\" of factory with id \"" + factory.getIdentifier() + "\" does not follow naming convention (see DataCellFactoryMethod#name()).");
                        LOGGER.warn("Factory will not be registered.");
                        continue;
                    }
                    register(factory);
                    LOGGER.debug("Registered JavaToDataCellConverterFactory from DataCellFactoryMethod annotation for: " + method.getParameterTypes()[0].getName() + " to " + dataType.getName());
                } catch (IncompleteAnnotationException e) {
                    LOGGER.coding("Incomplete DataCellFactoryMethod annotation for " + cellFactoryClass.getName() + "." + method.getName() + ". Will not register.", e);
                } catch (NoSuchMethodException | SecurityException ex) {
                    LOGGER.error("Could not access default constructor or constructor with parameter 'ExecutionContext' of " + "class '" + cellFactoryClass.getName() + "'. Converter will not be registered.", ex);
                }
            }
        }
    }
}
Also used : DataCellFactoryMethod(org.knime.core.data.convert.DataCellFactoryMethod) DataCellFactory(org.knime.core.data.DataCellFactory) DataCellFactoryMethod(org.knime.core.data.convert.DataCellFactoryMethod) Method(java.lang.reflect.Method) DataType(org.knime.core.data.DataType) IncompleteAnnotationException(java.lang.annotation.IncompleteAnnotationException)

Example 4 with IncompleteAnnotationException

use of java.lang.annotation.IncompleteAnnotationException in project j2objc by google.

the class AnnotatedElements method insertAnnotationValues.

/**
 * Extracts annotations from a container annotation and inserts them into a list.
 *
 * <p>
 * Given a complex annotation "annotation", it should have a "T[] value()" method on it.
 * Call that method and add all of the nested annotations into unfoldedAnnotations list.
 * </p>
 */
private static <T extends Annotation> void insertAnnotationValues(Annotation annotation, Class<T> annotationClass, ArrayList<T> unfoldedAnnotations) {
    // annotation is a complex annotation which has elements of instance annotationClass
    // (whose static type is T).
    // 
    // @interface SomeName {  <--- = annotation.getClass()
    // ...
    // T[] value();        <--- T.class == annotationClass
    // }
    // 
    // Use reflection to access these values.
    Class<T[]> annotationArrayClass = (Class<T[]>) ((T[]) Array.newInstance(annotationClass, 0)).getClass();
    Method valuesMethod;
    try {
        valuesMethod = annotation.getClass().getDeclaredMethod("value");
    // This will always succeed unless the annotation and its repeatable annotation class were
    // recompiled separately, then this is a binary incompatibility error.
    } catch (NoSuchMethodException e) {
        throw new AssertionError("annotation container = " + annotation + "annotation element class = " + annotationClass + "; missing value() method");
    } catch (SecurityException e) {
        throw new IncompleteAnnotationException(annotation.getClass(), "value");
    }
    // Ensure that value() returns a T[]
    if (!valuesMethod.getReturnType().isArray()) {
        throw new AssertionError("annotation container = " + annotation + "annotation element class = " + annotationClass + "; value() doesn't return array");
    }
    // Ensure that the T[] value() is actually the correct type (T==annotationClass).
    if (!annotationClass.equals(valuesMethod.getReturnType().getComponentType())) {
        throw new AssertionError("annotation container = " + annotation + "annotation element class = " + annotationClass + "; value() returns incorrect type");
    }
    // Append those values into the existing list.
    T[] nestedAnnotations;
    try {
        // Safe because of #getMethod.
        nestedAnnotations = (T[]) valuesMethod.invoke(annotation);
    } catch (IllegalAccessException | InvocationTargetException e) {
        throw new AssertionError(e);
    }
    for (int i = 0; i < nestedAnnotations.length; ++i) {
        unfoldedAnnotations.add(nestedAnnotations[i]);
    }
}
Also used : IncompleteAnnotationException(java.lang.annotation.IncompleteAnnotationException)

Example 5 with IncompleteAnnotationException

use of java.lang.annotation.IncompleteAnnotationException in project java-di by osglworks.

the class SimpleAnnoInvocationHandler method hashCode.

@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;
}
Also used : Method(java.lang.reflect.Method) IncompleteAnnotationException(java.lang.annotation.IncompleteAnnotationException)

Aggregations

IncompleteAnnotationException (java.lang.annotation.IncompleteAnnotationException)5 Method (java.lang.reflect.Method)2 DataCellFactory (org.knime.core.data.DataCellFactory)1 DataType (org.knime.core.data.DataType)1 DataCellFactoryMethod (org.knime.core.data.convert.DataCellFactoryMethod)1