Skip to content

Commit

Permalink
Fix invalid annotation literals
Browse files Browse the repository at this point in the history
The `Annotation.equals()` contract directly states that for two
annotation objects to be equal, they must (among others) both
implement the [same] annotation interface.

The CDI TCK used to create instances of memberless annotations
simply by instantiating an anonymous subclass of `AnnotationLiteral`,
which doesn't implement the annotation interface and hence is invalid.

It has never been a problem, because `AnnotationLiteral` implements
equality in a slightly more permissive manner: instead of checking
that both objects implement the same annotation interface, it checks
that both objects are annotations that have an equal `annotationType()`.
It becomes problematic when an implementation uses a different mechanism
to instantiate annotations than extending `AnnotationLiteral`, one that
closely follows the `Annotation` contract.

This commit replaces all usages of such anonymous subclasses by
proper `AnnotationLiteral` subclasses that implement the annotation
interface as well.
  • Loading branch information
Ladicek committed Mar 7, 2023
1 parent fe2dcf7 commit c475f49
Show file tree
Hide file tree
Showing 161 changed files with 496 additions and 360 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.interceptor.InterceptorBinding;

@Target({ TYPE, CONSTRUCTOR })
Expand All @@ -34,4 +35,6 @@
@Documented
@InterceptorBinding
public @interface ConstructorBinding {
class Literal extends AnnotationLiteral<ConstructorBinding> implements ConstructorBinding {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.interceptor.InterceptorBinding;

@Target({ TYPE, CONSTRUCTOR })
Expand All @@ -33,4 +34,6 @@
@Documented
@InterceptorBinding
public @interface CreativeBinding {
class Literal extends AnnotationLiteral<CreativeBinding> implements CreativeBinding {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ public void testBusinessMethodInterceptorBindings(MessageService messageService,
// Test interceptor is resolved (note non-binding member of BallBinding)
assertEquals(
getCurrentManager().resolveInterceptors(InterceptionType.AROUND_INVOKE,
new AnnotationLiteral<MessageBinding>() {
}, new AnnotationLiteral<LoggedBinding>() {
}, new AnnotationLiteral<TransactionalBinding>() {
}, new AnnotationLiteral<PingBinding>() {
}, new AnnotationLiteral<PongBinding>() {
}, new BallBindingLiteral(true, true)).size(), 1);
new MessageBinding.Literal(),
new LoggedBinding.Literal(),
new TransactionalBinding.Literal(),
new PingBinding.Literal(),
new PongBinding.Literal(),
new BallBindingLiteral(true, true)).size(), 1);

// Test the set of interceptor bindings
assertNotNull(messageService);
Expand All @@ -101,16 +101,16 @@ public void testLifecycleInterceptorBindings() {
// Test interceptor is resolved (note non-binding member of BallBinding)
assertEquals(
getCurrentManager().resolveInterceptors(InterceptionType.POST_CONSTRUCT,
new AnnotationLiteral<MessageBinding>() {
}, new AnnotationLiteral<LoggedBinding>() {
}, new AnnotationLiteral<TransactionalBinding>() {
}, new BasketBindingLiteral(true, true)).size(), 1);
new MessageBinding.Literal(),
new LoggedBinding.Literal(),
new TransactionalBinding.Literal(),
new BasketBindingLiteral(true, true)).size(), 1);
assertEquals(
getCurrentManager().resolveInterceptors(InterceptionType.PRE_DESTROY,
new AnnotationLiteral<MessageBinding>() {
}, new AnnotationLiteral<LoggedBinding>() {
}, new AnnotationLiteral<TransactionalBinding>() {
}, new BasketBindingLiteral(true, true)).size(), 1);
new MessageBinding.Literal(),
new LoggedBinding.Literal(),
new TransactionalBinding.Literal(),
new BasketBindingLiteral(true, true)).size(), 1);

// Test the set of interceptor bindings
ComplicatedLifecycleInterceptor.reset();
Expand All @@ -134,12 +134,11 @@ public void testConstructorInterceptorBindings() {
// Test interceptor is resolved
assertEquals(
getCurrentManager().resolveInterceptors(InterceptionType.AROUND_CONSTRUCT,
new AnnotationLiteral<MachineBinding>() {
}, new AnnotationLiteral<LoggedBinding>() {
}, new AnnotationLiteral<TransactionalBinding>() {
}, new AnnotationLiteral<ConstructorBinding>() {
}, new AnnotationLiteral<CreativeBinding>() {
}).size(), 1);
new MachineBinding.Literal(),
new LoggedBinding.Literal(),
new TransactionalBinding.Literal(),
new ConstructorBinding.Literal(),
new CreativeBinding.Literal()).size(), 1);

// Test the set of interceptor bindings
ComplicatedAroundConstructInterceptor.reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.interceptor.InterceptorBinding;

@Target({ TYPE })
Expand All @@ -32,4 +33,6 @@
@Documented
@InterceptorBinding
public @interface LoggedBinding {
class Literal extends AnnotationLiteral<LoggedBinding> implements LoggedBinding {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.interceptor.InterceptorBinding;

@Target(TYPE)
Expand All @@ -32,4 +33,6 @@
@Documented
@InterceptorBinding
public @interface MachineBinding {
class Literal extends AnnotationLiteral<MachineBinding> implements MachineBinding {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.interceptor.InterceptorBinding;

@Target({ TYPE })
Expand All @@ -32,4 +33,6 @@
@Documented
@InterceptorBinding
public @interface MessageBinding {
class Literal extends AnnotationLiteral<MessageBinding> implements MessageBinding {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.interceptor.InterceptorBinding;

@Target({ TYPE, METHOD })
Expand All @@ -34,4 +35,6 @@
@Documented
@InterceptorBinding
public @interface PingBinding {
class Literal extends AnnotationLiteral<PingBinding> implements PingBinding {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.interceptor.InterceptorBinding;

@Target({ TYPE, METHOD })
Expand All @@ -34,4 +35,6 @@
@Documented
@InterceptorBinding
public @interface PongBinding {
class Literal extends AnnotationLiteral<PongBinding> implements PongBinding {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.interceptor.InterceptorBinding;

@Target({ TYPE })
Expand All @@ -32,4 +33,6 @@
@Documented
@InterceptorBinding
public @interface TransactionalBinding {
class Literal extends AnnotationLiteral<TransactionalBinding> implements TransactionalBinding {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.jboss.cdi.tck.literals;

import jakarta.enterprise.util.AnnotationLiteral;

import java.lang.annotation.Inherited;

public class InheritedLiteral extends AnnotationLiteral<Inherited> implements Inherited {
public static InheritedLiteral INSTANCE = new InheritedLiteral();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.jboss.cdi.tck.literals;

import jakarta.enterprise.util.AnnotationLiteral;

public class OverrideLiteral extends AnnotationLiteral<Override> implements Override {
public static OverrideLiteral INSTANCE = new OverrideLiteral();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.jboss.cdi.tck.literals;

import jakarta.enterprise.inject.Stereotype;
import jakarta.enterprise.util.AnnotationLiteral;

public class StereotypeLiteral extends AnnotationLiteral<Stereotype> implements Stereotype {
public static StereotypeLiteral INSTANCE = new StereotypeLiteral();
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@
@SpecVersion(spec = "cdi", version = "2.0")
public class DependentContextTest extends AbstractTest {

private static final Annotation TAME_LITERAL = new AnnotationLiteral<Tame>() {
};
private static final Annotation PET_LITERAL = new AnnotationLiteral<Pet>() {
};
private static final Annotation TAME_LITERAL = new Tame.Literal();
private static final Annotation PET_LITERAL = new Pet.Literal();

@Deployment
public static WebArchive createTestArchive() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.inject.Qualifier;

@Target({ TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
@Qualifier
public @interface Pet {

class Literal extends AnnotationLiteral<Pet> implements Pet {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.inject.Qualifier;

@Target({ TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
@Qualifier
public @interface Tame {

class Literal extends AnnotationLiteral<Tame> implements Tame {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@

@SpecVersion(spec = "cdi", version = "2.0")
public class StereotypeDefinitionTest extends AbstractTest {
private static final Annotation TAME_LITERAL = new AnnotationLiteral<Tame>() {
};
private static final Annotation TAME_LITERAL = new Tame.Literal();

@Deployment
public static WebArchive createTestArchive() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.inject.Qualifier;

@Target({ TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
@Qualifier
public @interface Tame {

class Literal extends AnnotationLiteral<Tame> implements Tame {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ public void testNonRuntimeBindingTypeIsNotAnEventBindingType() {
@Test(expectedExceptions = { IllegalArgumentException.class })
@SpecAssertion(section = EVENT_TYPES_AND_QUALIFIER_TYPES, id = "d")
public void testFireEventWithNonRuntimeBindingTypeFails() {
getCurrentManager().getEvent().select(Animal.class, new AnnotationLiteral<NonRuntimeBindingType>() {}).fire(new Animal());
getCurrentManager().getEvent().select(Animal.class, new NonRuntimeBindingType.Literal()).fire(new Animal());
}

@Test(expectedExceptions = { IllegalArgumentException.class })
@SpecAssertion(section = EVENT_TYPES_AND_QUALIFIER_TYPES, id = "g")
public void testFireEventWithNonBindingAnnotationsFails() {
getCurrentManager().getEvent().select(Animal.class, new AnnotationLiteral<NonBindingType>() {}).fire(new Animal());
getCurrentManager().getEvent().select(Animal.class, new NonBindingType.Literal()).fire(new Animal());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.jboss.cdi.tck.tests.event.bindingTypes;

import jakarta.enterprise.util.AnnotationLiteral;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
Expand All @@ -26,4 +28,6 @@
@Target({ FIELD, PARAMETER })
@Retention(RUNTIME)
public @interface NonBindingType {
class Literal extends AnnotationLiteral<NonBindingType> implements NonBindingType {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.inject.Qualifier;

@Target({ FIELD, PARAMETER })
@Qualifier
@Retention(RetentionPolicy.CLASS)
public @interface NonRuntimeBindingType {
class Literal extends AnnotationLiteral<NonRuntimeBindingType> implements NonRuntimeBindingType {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
@SpecVersion(spec = "cdi", version = "2.0")
public class EventTypesTest extends AbstractTest {

private AnnotationLiteral<Extra> extraLiteral = new AnnotationLiteral<Extra>() {
};
private AnnotationLiteral<Extra> extraLiteral = new Extra.Literal();

@Deployment
public static WebArchive createTestArchive() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.inject.Qualifier;

@Target({ TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
@Qualifier
public @interface Extra {
class Literal extends AnnotationLiteral<Extra> implements Extra {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ public void restoreItem(Item item) {
throw new IllegalArgumentException("Item already restored");
}

itemEvent.select(new AnnotationLiteral<Restored>() {
}).fire(item);
itemEvent.select(new Restored.Literal()).fire(item);
}

public void stock() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.inject.Qualifier;

@Target({ TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
@Qualifier
public @interface Restored {
class Literal extends AnnotationLiteral<Restored> implements Restored {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.inject.Qualifier;

@Target({ TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
@Qualifier
public @interface Number {

class Literal extends AnnotationLiteral<Number> implements Number {
}
}

Loading

0 comments on commit c475f49

Please sign in to comment.