Skip to content

Commit

Permalink
WELD-563: throw a DefinitionException if the InterceptorBinding has t…
Browse files Browse the repository at this point in the history
…he wrong Target. initial take on unwrapping the ComputationException
  • Loading branch information
mbogoevici committed Jul 30, 2010
1 parent c069b38 commit 1e8243a
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 14 deletions.
Expand Up @@ -57,6 +57,7 @@ public InterceptorBindingModel(Class<T> type, ClassTransformer transformer)
super(type, transformer);
if (isValid())
{
checkTargetType();
initNonBindingTypes();
initInterceptionBindingTypes();
checkArrayAndAnnotationValuedMembers();
Expand Down Expand Up @@ -86,27 +87,29 @@ protected void initInterceptionBindingTypes()
inheritedInterceptionBindingTypes = getAnnotatedAnnotation().getMetaAnnotations(InterceptorBinding.class);
}

@Override
protected void initValid()
private void checkTargetType()
{
super.initValid();
if (!getAnnotatedAnnotation().isAnnotationPresent(Target.class))
{
this.valid = false;
log.debug(MISSING_TARGET, getAnnotatedAnnotation());
throw new DefinitionException(MISSING_TARGET, getAnnotatedAnnotation());
}
else
{
ElementType[] targetElementTypes = getAnnotatedAnnotation().getAnnotation(Target.class).value();
if (!Arrays2.unorderedEquals(targetElementTypes, ElementType.TYPE, ElementType.METHOD)
&& !Arrays2.unorderedEquals(targetElementTypes, ElementType.TYPE))
if (!isValidTargetType())
{
this.valid = false;
log.debug(MISSING_TARGET_TYPE_METHOD_OR_TARGET_TYPE, getAnnotatedAnnotation());
throw new DefinitionException(MISSING_TARGET_TYPE_METHOD_OR_TARGET_TYPE, getAnnotatedAnnotation());
}
}
}

private boolean isValidTargetType()
{
return Arrays2.unorderedEquals(getAnnotatedAnnotation().getAnnotation(Target.class).value(), ElementType.TYPE, ElementType.METHOD)
|| Arrays2.unorderedEquals(getAnnotatedAnnotation().getAnnotation(Target.class).value(), ElementType.TYPE);
}

private void checkMetaAnnotations()
{
if (Arrays2.containsAll(getAnnotatedAnnotation().getAnnotation(Target.class).value(), ElementType.METHOD))
Expand All @@ -116,8 +119,7 @@ private void checkMetaAnnotations()
if (!Arrays2.containsAll(inheritedBinding.annotationType().getAnnotation(Target.class).value(), ElementType.METHOD))
{
this.valid = false;
log.debug(TARGET_TYPE_METHOD_INHERITS_FROM_TARGET_TYPE,
getAnnotatedAnnotation(), inheritedBinding);
log.debug(TARGET_TYPE_METHOD_INHERITS_FROM_TARGET_TYPE, getAnnotatedAnnotation(), inheritedBinding);
}
}
}
Expand Down
Expand Up @@ -19,11 +19,14 @@
import java.lang.annotation.Annotation;
import java.util.concurrent.ConcurrentMap;

import org.jboss.weld.bootstrap.api.Service;
import org.jboss.weld.resources.ClassTransformer;

import com.google.common.base.Function;
import com.google.common.collect.ComputationException;
import com.google.common.collect.MapMaker;
import org.jboss.weld.bootstrap.api.Service;
import org.jboss.weld.exceptions.DefinitionException;
import org.jboss.weld.exceptions.DeploymentException;
import org.jboss.weld.exceptions.WeldException;
import org.jboss.weld.resources.ClassTransformer;

/**
* Metadata singleton for holding EJB metadata, scope models etc.
Expand Down Expand Up @@ -202,6 +205,22 @@ public void cleanup()

public <T extends Annotation> InterceptorBindingModel<T> getInterceptorBindingModel(final Class<T> interceptorBinding)
{
return (InterceptorBindingModel<T>) interceptorBindings.get(interceptorBinding);
// Unwrap Definition/Deployment exceptions wrapped in a ComputationException
// TODO: generalize this and move to a higher level (MBG)
try
{
return (InterceptorBindingModel<T>) interceptorBindings.get(interceptorBinding);
}
catch (ComputationException e)
{
if (e.getCause() instanceof DeploymentException || e.getCause() instanceof DefinitionException)
{
throw (WeldException)e.getCause();
}
else
{
throw e;
}
}
}
}
@@ -0,0 +1,38 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.weld.tests.interceptors.interceptorBinding;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.interceptor.InterceptorBinding;

/**
* @author Marius Bogoevici
*/
@InterceptorBinding
@Retention(RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE, ElementType.CONSTRUCTOR})
@Documented
public @interface BadInterceptorBinding
{
}
@@ -0,0 +1,40 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.weld.tests.interceptors.interceptorBinding;

import org.jboss.testharness.impl.packaging.Artifact;
import org.jboss.testharness.impl.packaging.ExpectedDeploymentException;
import org.jboss.weld.exceptions.DefinitionException;
import org.jboss.weld.test.AbstractWeldTest;
import org.testng.annotations.Test;

/**
* @author Marius Bogoevici
*/
@Artifact
@ExpectedDeploymentException(DefinitionException.class)
public class BadInterceptorBindingTest extends AbstractWeldTest
{

@Test
public void testBadInterceptorBinding()
{

}

}
@@ -0,0 +1,29 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.weld.tests.interceptors.interceptorBinding;

/**
* @author Marius Bogoevici
*/
@BadInterceptorBinding
public class NaiveClass
{
public void doSomething()
{
}
}

0 comments on commit 1e8243a

Please sign in to comment.