/** * Activates the alternatives declared with {@code @Beans} globally for the * application. * <p/> * For every types and every methods of every types declared with * {@link Beans#alternatives()}, the {@code Priority} annotation is added * so that the corresponding alternatives are selected globally for the * entire application. * * @see Beans */ private <T> void alternatives(@Observes @WithAnnotations(Alternative.class) ProcessAnnotatedType<T> pat) { AnnotatedType<T> type = pat.getAnnotatedType(); if (!Arrays.asList(beans.alternatives()).contains(type.getJavaClass())) { // Only select globally the alternatives that are declared with @Beans return; } Set<AnnotatedMethod<? super T>> methods = new HashSet<>(); for (AnnotatedMethod<? super T> method : type.getMethods()) { if (method.isAnnotationPresent(Alternative.class) && !method.isAnnotationPresent(Priority.class)) { methods.add(new AnnotatedMethodDecorator<>(method, PriorityLiteral.of(APPLICATION))); } } if (type.isAnnotationPresent(Alternative.class) && !type.isAnnotationPresent(Priority.class)) { pat.setAnnotatedType(new AnnotatedTypeDecorator<>(type, PriorityLiteral.of(APPLICATION), methods)); } else if (!methods.isEmpty()) { pat.setAnnotatedType(new AnnotatedTypeDecorator<>(type, methods)); } }
@SuppressWarnings("serial") protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter writer = response.getWriter(); writer.print("<html><body>"); String typeCalculator = request.getParameter("type"); String rawAmmount = request.getParameter("ammount"); if (rawAmmount == null) { writer.print("<p><strong>Invalid ammount!</strong></p>"); } else { double ammount = Double.parseDouble(rawAmmount); Instance<Calculator> calc = null; // lookup the choosen calculator if("high".equals(typeCalculator)) { calc = calculator.select(new AnnotationLiteral<High>(){}); } else if("low".equals(typeCalculator)) { calc = calculator.select(new AnnotationLiteral<Low>(){}); } else { calc = calculator.select(new AnnotationLiteral<Alternative>(){}); } writer.print("Ammount: " + ammount); writer.print("<br/>Calculator: " + calc.get()); writer.print("<br/>Ammount with tax: " + calc.get().applyTax(ammount)); writer.print("<br/>Ammount with discount: " + calc.get().applyDiscount(ammount)); } writer.print("</body></html>"); }
@Alternative @Produces @RealmName public String getRealmName() { return configuration.getRealmName(); }
@Alternative @Produces @RealmResourceName public String getRealmResourceName() { return configuration.getResourceName(); }
@Alternative @Produces @RealmResourceSecret public String getRealmResourceSecret() { return configuration.getResourceSecret(); }
@Alternative @Produces @AuthServerUrl public String getAuthServerUrl() { return configuration.getAuthServerUrl(); }
@Produces @Alternative public BuildService buildService() { return mock(BuildService.class); }
@Produces @Alternative public ProjectService<Project> projectService() { return mock(ProjectService.class); }
@Produces @Alternative public ProjectFactory<Project> projectProjectFactory() { return mock(ProjectFactory.class); }
@Produces @Alternative public M2RepoService m2RepoService() { return mock(M2RepoService.class); }
@Produces @Alternative @Named("uf") public ServletContext servletContext() { return mock(ServletContext.class); }
@Produces @Alternative public SessionInfo sessionInfo() { return mock(SessionInfo.class); }
@Produces @Alternative public User getIdentity() { return identity; }
@Override public <X extends Annotation> X getAnnotation(final Class<X> annType) { return (X) (annType.equals(Alternative.class) ? OSCLITERAL : type.getAnnotation(annType)); }
@Produces @Alternative public Greeting getGreeting() { return new SimpleGreeting(); }
public boolean isAlternative() { return repositoryType.isAnnotationPresent(Alternative.class); }
@Produces @Alternative public M2RepoService m2RepoService() { return mock( M2RepoService.class ); }
@Produces @Alternative public KModuleService kModuleService() { return mock(KModuleService.class); }
@Produces @Alternative public ConfigurationService configurationService() { return mock(ConfigurationService.class); }
@Produces @Alternative public KieModuleService kieWorkspaceProjectService() { return mock(KieModuleService.class); }
@Produces @Alternative public KModuleService kModuleService() { return mock( KModuleService.class ); }
@Produces @Alternative public ConfigurationService configurationService() { return mock( ConfigurationService.class ); }
@Produces @Alternative public IdentityManagementConfiguration authenticationService() { return new IdentityManagementConfiguration(); }
@Produces @Alternative @Named("uf") public ServletContext servletContext() { return mock( ServletContext.class ); }
@Test public void testTypeLevelAnnotationRedefinition() { AnnotatedTypeBuilder<Cat> builder = new AnnotatedTypeBuilder<Cat>(); builder.readFromType(Cat.class); AnnotatedType<Cat> cat = builder.create(); assertNotNull(cat); assertNotNull(cat.getAnnotation(Named.class)); assertEquals("cat", cat.getAnnotation(Named.class).value()); builder.addToClass(new AlternativeLiteral()) .addToClass(new ApplicationScopedLiteral()) .removeFromClass(Named.class) .addToClass(new NamedLiteral("tomcat")); cat = builder.create(); assertNotNull(cat); assertEquals(3, cat.getAnnotations().size()); assertTrue(cat.isAnnotationPresent(Named.class)); assertTrue(cat.isAnnotationPresent(Alternative.class)); assertTrue(cat.isAnnotationPresent(ApplicationScoped.class)); assertEquals("tomcat", cat.getAnnotation(Named.class).value()); AnnotatedMethod observerMethod = null; for (AnnotatedMethod m : cat.getMethods()) { if ("doSomeObservation".equals(m.getJavaMember().getName())) { observerMethod = m; break; } } assertNotNull(observerMethod); observerMethod.isAnnotationPresent(Observes.class); { // test reading from an AnnotatedType AnnotatedTypeBuilder<Cat> builder2 = new AnnotatedTypeBuilder<Cat>(); builder2.readFromType(cat); builder2.removeFromAll(Named.class); final AnnotatedType<Cat> noNameCat = builder2.create(); assertFalse(noNameCat.isAnnotationPresent(Named.class)); assertEquals(2, noNameCat.getAnnotations().size()); } { // test reading from an AnnotatedType in non-overwrite mode AnnotatedTypeBuilder<Cat> builder3 = new AnnotatedTypeBuilder<Cat>(); builder3.readFromType(cat, true); builder3.removeFromAll(Named.class); builder3.readFromType(cat, false); final AnnotatedType<Cat> namedCat = builder3.create(); assertTrue(namedCat.isAnnotationPresent(Named.class)); assertEquals(3, namedCat.getAnnotations().size()); } }