Java 类javax.enterprise.inject.Alternative 实例源码

项目:Camel    文件:CamelCdiTestExtension.java   
/**
 * 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));
    }
}
项目:javaee_projects    文件:ManuallyLookupServlet.java   
@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>");
}
项目:secret-store    文件:ApplicationResources.java   
@Alternative
@Produces
@RealmName
public String getRealmName() {
    return configuration.getRealmName();
}
项目:secret-store    文件:ApplicationResources.java   
@Alternative
@Produces
@RealmResourceName
public String getRealmResourceName() {
    return configuration.getResourceName();
}
项目:secret-store    文件:ApplicationResources.java   
@Alternative
@Produces
@RealmResourceSecret
public String getRealmResourceSecret() {
    return configuration.getResourceSecret();
}
项目:secret-store    文件:ApplicationResources.java   
@Alternative
@Produces
@AuthServerUrl
public String getAuthServerUrl() {
    return configuration.getAuthServerUrl();
}
项目:appformer    文件:TestAppSetup.java   
@Produces
@Alternative
public BuildService buildService() {
    return mock(BuildService.class);
}
项目:appformer    文件:TestAppSetup.java   
@Produces
@Alternative
public ProjectService<Project> projectService() {
    return mock(ProjectService.class);
}
项目:appformer    文件:TestAppSetup.java   
@Produces
@Alternative
public ProjectFactory<Project> projectProjectFactory() {
    return mock(ProjectFactory.class);
}
项目:appformer    文件:TestAppSetup.java   
@Produces
@Alternative
public M2RepoService m2RepoService() {
    return mock(M2RepoService.class);
}
项目:appformer    文件:TestAppSetup.java   
@Produces
@Alternative
@Named("uf")
public ServletContext servletContext() {
    return mock(ServletContext.class);
}
项目:appformer    文件:TestProjectService.java   
@Produces
@Alternative
public ProjectService<Project> projectService() {
    return mock(ProjectService.class);
}
项目:appformer    文件:TestProjectService.java   
@Produces
@Alternative
public ProjectFactory<Project> projectProjectFactory() {
    return mock(ProjectFactory.class);
}
项目:appformer    文件:GuvnorTestAppSetup.java   
@Produces
@Alternative
public SessionInfo sessionInfo() {
    return mock(SessionInfo.class);
}
项目:appformer    文件:TestIdentityFactory.java   
@Produces
@Alternative
public User getIdentity() {
    return identity;
}
项目:ocelot    文件:SpringConfigurationWrapper.java   
@Override
public <X extends Annotation> X getAnnotation(final Class<X> annType) {
    return (X) (annType.equals(Alternative.class) ? OSCLITERAL : type.getAnnotation(annType));
}
项目:JavaIncrementalParser    文件:ProducerMethodGreeting.java   
@Produces
@Alternative
public Greeting getGreeting() {
    return new SimpleGreeting();
}
项目:winter-data-jpa    文件:SimpleRepositoryBean.java   
public boolean isAlternative() {
    return repositoryType.isAnnotationPresent(Alternative.class);
}
项目:kie-wb-common    文件:TestAppSetup.java   
@Produces
@Alternative
public M2RepoService m2RepoService() {
    return mock( M2RepoService.class );
}
项目:kie-wb-common    文件:TestAppSetup.java   
@Produces
@Alternative
public M2RepoService m2RepoService() {
    return mock(M2RepoService.class);
}
项目:kie-wb-common    文件:TestAppSetup.java   
@Produces
@Alternative
public KModuleService kModuleService() {
    return mock(KModuleService.class);
}
项目:kie-wb-common    文件:TestAppSetup.java   
@Produces
@Alternative
public ConfigurationService configurationService() {
    return mock(ConfigurationService.class);
}
项目:kie-wb-common    文件:ExplorerServiceImplTestUtil.java   
@Produces
@Alternative
public KieModuleService kieWorkspaceProjectService() {
    return mock(KieModuleService.class);
}
项目:kie-wb-common    文件:ExplorerServiceImplTestUtil.java   
@Produces
@Alternative
public M2RepoService m2RepoService() {
    return mock(M2RepoService.class);
}
项目:kie-wb-common    文件:TestAppSetup.java   
@Produces
@Alternative
public M2RepoService m2RepoService() {
    return mock( M2RepoService.class );
}
项目:kie-wb-common    文件:TestAppSetup.java   
@Produces
@Alternative
public M2RepoService m2RepoService() {
    return mock( M2RepoService.class );
}
项目:kie-wb-common    文件:TestAppSetup.java   
@Produces
@Alternative
public KModuleService kModuleService() {
    return mock( KModuleService.class );
}
项目:kie-wb-common    文件:TestAppSetup.java   
@Produces
@Alternative
public ConfigurationService configurationService() {
    return mock( ConfigurationService.class );
}
项目:kie-wb-common    文件:TestAppSetup.java   
@Produces
@Alternative
public IdentityManagementConfiguration authenticationService() {
    return new IdentityManagementConfiguration();
}
项目:kie-wb-common    文件:TestAppSetup.java   
@Produces
@Alternative
public M2RepoService m2RepoService() {
    return mock( M2RepoService.class );
}
项目:kie-wb-common    文件:TestAppSetup.java   
@Produces
@Alternative
public KModuleService kModuleService() {
    return mock( KModuleService.class );
}
项目:kie-wb-common    文件:TestAppSetup.java   
@Produces
@Alternative
@Named("uf")
public ServletContext servletContext() {
    return mock( ServletContext.class );
}
项目:kie-wb-common    文件:TestAppSetup.java   
@Produces
@Alternative
public IdentityManagementConfiguration authenticationService() {
    return new IdentityManagementConfiguration();
}
项目:deltaspike    文件:AnnotatedTypeBuilderTest.java   
@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());
    }
}