user2620644
user2620644

Reputation: 521

Attempt to create proxy for a non-annotation type

Why do I get java.lang.annotation.AnnotationFormatError: Attempt to create proxy for a non-annotation type. when I'm trying to get the annotation list from the field?

My annotation:

@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
private @interface TypicalAnnotation
{
    int size();
}

Usage:

public static class MockAnnotatedClass
{
    @TypicalAnnotation(size = 3)
    public Integer number = 2;
}

Call:

ReflectionUtils.getAllFields(clazz1).getAnnotations() <- got the exception.

Upvotes: 0

Views: 1557

Answers (1)

Haka
Haka

Reputation: 11

I had the same problem when I used Jdeveloper 12.2.1.0.0 and integrated Weblogic with enabled Fast Swap feature.

if you look at the source code (github) you will see that an exception is thrown if the type is either not an annotation or implements more than one interface or does not implement the java.lang.annotation.Annotation interface:

AnnotationInvocationHandler(Class<? extends Annotation> type, Map<String, Object> memberValues) {
    Class<?>[] superInterfaces = type.getInterfaces();
    if (!type.isAnnotation() ||
        superInterfaces.length != 1 ||
        superInterfaces[0] != java.lang.annotation.Annotation.class)
        throw new AnnotationFormatError("Attempt to create proxy for a non-annotation type.");
    this.type = type;
    this.memberValues = memberValues;
}

Using this code, I checked which interfaces my annotation implements:

Annotation declaration:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {}

Checking of type:

Class<?> type = MyAnnotation.class;

System.out.println("type.isAnnotation() " + type.isAnnotation());
System.out.println("type.getInterfaces().length " + type.getInterfaces().length);

for (Class<?> superType : type.getInterfaces()) {
    System.out.println("type.getInterfaces()[i] " + superType);
}

Output:

type.isAnnotation() true
type.getInterfaces().length 2
type.getInterfaces()[i] interface java.lang.annotation.Annotation
type.getInterfaces()[i] interface com.bea.wls.redef.Redefinable     // << ??????

As it turned out, another interface is added to the annotation. I did not find information specifically about com.bea.wls.redef.Redefinable. After a bit of searching, I realized that the package com.bea.wls.redef is used by the Fast Swap function.

I see 2 solutions:

  1. Disable Fast Swap function.

  2. Move all custom annotations to separate jar. It is what I did.

Perhaps there is a way to turn off wrapping for annotations. But I did not find it.

Upvotes: 1

Related Questions