Skip to content

Commit

Permalink
FORGE-929: Throwing exception for facets that inject projects
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed May 31, 2013
1 parent 45a2752 commit 15fbe1d
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -20,10 +21,13 @@
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessBean;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.ProcessInjectionTarget;
import javax.enterprise.inject.spi.ProcessManagedBean;

import org.jboss.forge.bus.util.Annotations;
import org.jboss.forge.project.Facet;
import org.jboss.forge.project.Project;
import org.jboss.forge.resources.Resource;
import org.jboss.forge.shell.plugins.Alias;
import org.jboss.forge.shell.plugins.Command;
Expand Down Expand Up @@ -51,7 +55,7 @@ public Map<String, List<PluginMetadata>> getPlugins()
}

@SuppressWarnings("unchecked")
public void scan(@Observes final ProcessBean<?> event)
public void scan(@Observes final ProcessManagedBean<?> event) throws Exception
{
Bean<?> bean = event.getBean();

Expand All @@ -75,6 +79,25 @@ public void scan(@Observes final ProcessBean<?> event)
}
}

/**
* Facets should never inject Project. See FORGE-929
*/
public <T extends Facet> void validateFacet(@Observes ProcessInjectionTarget<T> injectionTarget)
{
Class<T> facetClass = injectionTarget.getAnnotatedType().getJavaClass();
Set<InjectionPoint> injectionPoints = injectionTarget.getInjectionTarget().getInjectionPoints();
for (InjectionPoint injectionPoint : injectionPoints)
{
Type baseType = injectionPoint.getAnnotated().getBaseType();
if (baseType instanceof Class && Project.class.isAssignableFrom((Class<?>) baseType))
{
throw new IllegalStateException("Facet "
+ facetClass.getName()
+ " must not @Inject Project. Please remove it and use getProject() instead.");
}
}
}

public Set<Class<? extends Facet>> getFacetTypes()
{
return Collections.unmodifiableSet(facetTypes);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.shell.test.command;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.ShouldThrowException;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.Root;
import org.jboss.seam.render.RenderRoot;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.solder.SolderRoot;
import org.jboss.weld.exceptions.DefinitionException;
import org.junit.Test;
import org.junit.runner.RunWith;

import test.org.jboss.forge.shell.test.command.IllegalFacet;

@RunWith(Arquillian.class)
public class FacetStateTest
{

@Deployment
@ShouldThrowException(DefinitionException.class)
public static JavaArchive getDeployment()
{

JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "test.jar").addPackages(true, Root.class.getPackage())
.addPackages(true, RenderRoot.class.getPackage()).addPackages(true, SolderRoot.class.getPackage())
.addClass(IllegalFacet.class)
.addAsManifestResource("META-INF/beans.xml", ArchivePaths.create("beans.xml"));

return archive;
}

@Test
public void testIllegalFacet() throws Exception
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package test.org.jboss.forge.shell.test.command;

import javax.inject.Inject;

import org.jboss.forge.project.Project;
import org.jboss.forge.project.facets.BaseFacet;
import org.jboss.forge.shell.plugins.Alias;

@Alias("FORGE-929")
public class IllegalFacet extends BaseFacet
{
@Inject
private Project project;

@Override
public boolean install()
{
return true;
}

@Override
public boolean isInstalled()
{
return true;
}

}

0 comments on commit 15fbe1d

Please sign in to comment.