[jbosstools-commits] JBoss Tools SVN: r22404 - in trunk/cdi: plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/validation and 3 other directories.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Fri May 28 05:53:30 EDT 2010


Author: akazakov
Date: 2010-05-28 05:53:29 -0400 (Fri, 28 May 2010)
New Revision: 22404

Added:
   trunk/cdi/tests/org.jboss.tools.cdi.core.test/resources/tck/tests/jbt/validation/inject/Animal.java
   trunk/cdi/tests/org.jboss.tools.cdi.core.test/resources/tck/tests/jbt/validation/inject/FarmBroken.java
   trunk/cdi/tests/org.jboss.tools.cdi.core.test/resources/tck/tests/jbt/validation/inject/Sheep.java
Modified:
   trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/core/CDIUtil.java
   trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/validation/CDICoreValidator.java
   trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/validation/errorList.txt
   trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/preferences/CDIConfigurationBlock.java
   trunk/cdi/tests/org.jboss.tools.cdi.core.test/src/org/jboss/tools/cdi/core/test/tck/InjectionPointTest.java
   trunk/cdi/tests/org.jboss.tools.cdi.core.test/src/org/jboss/tools/cdi/core/test/tck/ValidationTest.java
Log:
https://jira.jboss.org/jira/browse/JBIDE-2708 Added new CDI validation rule: Injection point type is a type variable

Modified: trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/core/CDIUtil.java
===================================================================
--- trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/core/CDIUtil.java	2010-05-28 09:06:52 UTC (rev 22403)
+++ trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/core/CDIUtil.java	2010-05-28 09:53:29 UTC (rev 22404)
@@ -26,7 +26,9 @@
 import org.eclipse.jdt.core.ILocalVariable;
 import org.eclipse.jdt.core.IMethod;
 import org.eclipse.jdt.core.IType;
+import org.eclipse.jdt.core.ITypeParameter;
 import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.Signature;
 import org.eclipse.wst.validation.internal.plugin.ValidationPlugin;
 import org.jboss.tools.common.EclipseUtil;
 import org.jboss.tools.common.model.util.EclipseJavaUtil;
@@ -538,6 +540,67 @@
 		return false;
 	}
 
+	/**
+	 * Checks if the bean member has a type variable as a type.
+	 * If the bean member is a field then checks its type.
+	 * If the bean member is a parameter of a method then checks its type.
+	 * If the bean member is a method then checks its return type.
+	 * 
+	 * @param member
+	 * @param checkGenericMethod if true then checks if this member use a type variable which is declared in the generic method (in case of the member is a method).
+	 * @return
+	 */
+	public static boolean isTypeVariable(IBeanMember member, boolean checkGenericMethod) {
+		try {
+			String[] typeVariableSegnatures = member.getClassBean().getBeanClass().getTypeParameterSignatures();
+			List<String> variables = new ArrayList<String>();
+			for (String variableSig : typeVariableSegnatures) {
+				variables.add(Signature.getTypeVariable(variableSig));
+			}
+			if(checkGenericMethod) {
+				ITypeParameter[] typeParams = null;
+				if(member instanceof IParameter) {
+					typeParams = ((IParameter)member).getBeanMethod().getMethod().getTypeParameters();
+				} if(member instanceof IBeanMethod) {
+					typeParams = ((IBeanMethod)member).getMethod().getTypeParameters();
+				}
+				if(typeParams!=null) {
+					for (ITypeParameter param : typeParams) {
+						variables.add(param.getElementName());
+					}
+				}
+			}
+			String signature = null;
+			if(member instanceof IBeanField) {
+				signature = ((IBeanField)member).getField().getTypeSignature();
+			} else if(member instanceof IParameter) {
+				if(((IParameter)member).getType()==null) {
+					return false;
+				}
+				signature = ((IParameter)member).getType().getSignature();
+			} else if(member instanceof IBeanMethod) {
+				signature = ((IBeanMethod)member).getMethod().getReturnType();
+			}
+			return isTypeVariable(variables, signature);
+		} catch (JavaModelException e) {
+			CDICorePlugin.getDefault().logError(e);
+		}
+		return false;
+	}
+
+	private static boolean isTypeVariable(List<String> typeVariables, String signature) {
+		if(signature==null) {
+			return false;
+		}
+		String typeString = Signature.toString(signature);
+		for (String variableName : typeVariables) {
+			if(typeString.equals(variableName)) {
+				return true;
+			}
+		}
+		return false;
+	}
+
 	private static IType getSuperClass(IType type) throws JavaModelException {
 		String superclassName = type.getSuperclassName();
 		if(superclassName!=null) {

Modified: trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/validation/CDICoreValidator.java
===================================================================
--- trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/validation/CDICoreValidator.java	2010-05-28 09:06:52 UTC (rev 22403)
+++ trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/validation/CDICoreValidator.java	2010-05-28 09:53:29 UTC (rev 22404)
@@ -368,6 +368,8 @@
 		if(specializingBean==null) {
 			return;
 		}
+		validationContext.addLinkedCoreResource(bean.getSourcePath().toOSString(), specializingBean.getResource().getFullPath(), false);
+
 		String beanClassName = bean.getBeanClass().getElementName();
 		String beanName = bean instanceof IBeanMethod?beanClassName + "." + ((IBeanMethod)bean).getSourceMember().getElementName() + "()":beanClassName;
 		String specializingBeanClassName = specializingBean.getBeanClass().getElementName();
@@ -903,7 +905,6 @@
 							pinjection.getAnnotationPosition(CDIConstants.NAMED_QUALIFIER_TYPE_NAME),
 							injection.getResource());
 				}
-				
 			}
 		} else if (injection instanceof IInjectionPointMethod) {
 			IAnnotationDeclaration named = injection.getAnnotation(CDIConstants.NAMED_QUALIFIER_TYPE_NAME);
@@ -942,6 +943,11 @@
 				addError(CDIValidationMessages.STATIC_METHOD_ANNOTATED_INJECT, CDIPreferences.GENERIC_METHOD_ANNOTATED_INJECT, declaration, injection.getResource());
 			}
 		}
+
+		if(!(injection instanceof IInjectionPointMethod) && CDIUtil.isTypeVariable(injection, false)) {
+			IAnnotationDeclaration declaration = injection.getInjectAnnotation();
+			addError(CDIValidationMessages.INJECTION_TYPE_IS_VARIABLE, CDIPreferences.INJECTION_TYPE_IS_VARIABLE, declaration, injection.getResource());
+		}
 	}
 
 	/**

Modified: trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/validation/errorList.txt
===================================================================
--- trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/validation/errorList.txt	2010-05-28 09:06:52 UTC (rev 22403)
+++ trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/validation/errorList.txt	2010-05-28 09:53:29 UTC (rev 22404)
@@ -128,6 +128,10 @@
 5.2.2. Legal injection point types
 - injection point type is a type variable
 
+
+
+
+
 5.2.5. Qualifier annotations with members
 - array-valued or annotation-valued member of a qualifier type is not annotated @Nonbinding (Non-Portable behavior)
 

Modified: trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/preferences/CDIConfigurationBlock.java
===================================================================
--- trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/preferences/CDIConfigurationBlock.java	2010-05-28 09:06:52 UTC (rev 22403)
+++ trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/preferences/CDIConfigurationBlock.java	2010-05-28 09:53:29 UTC (rev 22404)
@@ -48,7 +48,7 @@
 			{CDIPreferences.PRODUCER_FIELD_TYPE_HAS_WILDCARD, CDIPreferencesMessages.CDIValidatorConfigurationBlock_pb_producerFieldTypeHasWildcard_label},
 			{CDIPreferences.PRODUCER_FIELD_TYPE_IS_VARIABLE, CDIPreferencesMessages.CDIValidatorConfigurationBlock_pb_producerFieldTypeIsVariable_label},
 //			{CDIPreferences.PRODUCER_FIELD_TYPE_DOES_NOT_MATCH_JAVA_EE_OBJECT, CDIPreferencesMessages.CDIValidatorConfigurationBlock_pb_producerFieldTypeDoesNotMatchJavaEeObject_label},
-//			{CDIPreferences.INJECTION_TYPE_IS_VARIABLE, CDIPreferencesMessages.CDIValidatorConfigurationBlock_pb_injectionTypeIsVariable_label},
+			{CDIPreferences.INJECTION_TYPE_IS_VARIABLE, CDIPreferencesMessages.CDIValidatorConfigurationBlock_pb_injectionTypeIsVariable_label},
 			{CDIPreferences.STEREOTYPE_IS_ANNOTATED_TYPED, CDIPreferencesMessages.CDIValidatorConfigurationBlock_pb_stereotypeIsAnnotatedTyped_label},
 //			{CDIPreferences.MISSING_NONBINDING_IN_QUALIFIER_TYPE_MEMBER, CDIPreferencesMessages.CDIValidatorConfigurationBlock_pb_missingNonbindingInQualifierTypeMember_label},
 //			{CDIPreferences.MISSING_NONBINDING_IN_INTERCEPTOR_BINDING_TYPE_MEMBER, CDIPreferencesMessages.CDIValidatorConfigurationBlock_pb_missingNonbindingInInterceptorBindingTypeMember_label},

Added: trunk/cdi/tests/org.jboss.tools.cdi.core.test/resources/tck/tests/jbt/validation/inject/Animal.java
===================================================================
--- trunk/cdi/tests/org.jboss.tools.cdi.core.test/resources/tck/tests/jbt/validation/inject/Animal.java	                        (rev 0)
+++ trunk/cdi/tests/org.jboss.tools.cdi.core.test/resources/tck/tests/jbt/validation/inject/Animal.java	2010-05-28 09:53:29 UTC (rev 22404)
@@ -0,0 +1,22 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., 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.jsr299.tck.tests.jbt.validation.inject;
+
+interface Animal
+{
+
+}


Property changes on: trunk/cdi/tests/org.jboss.tools.cdi.core.test/resources/tck/tests/jbt/validation/inject/Animal.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: trunk/cdi/tests/org.jboss.tools.cdi.core.test/resources/tck/tests/jbt/validation/inject/FarmBroken.java
===================================================================
--- trunk/cdi/tests/org.jboss.tools.cdi.core.test/resources/tck/tests/jbt/validation/inject/FarmBroken.java	                        (rev 0)
+++ trunk/cdi/tests/org.jboss.tools.cdi.core.test/resources/tck/tests/jbt/validation/inject/FarmBroken.java	2010-05-28 09:53:29 UTC (rev 22404)
@@ -0,0 +1,17 @@
+package org.jboss.jsr299.tck.tests.jbt.validation.inject;
+
+import javax.inject.Inject;
+
+class FarmBroken <U> {
+   @Inject
+   public <T extends Animal> void setAnimal(T animal) {
+      
+   }
+
+   @Inject
+   public void setAnimal(U animal) {
+   }
+
+   @Inject
+   public U animalU;
+}
\ No newline at end of file


Property changes on: trunk/cdi/tests/org.jboss.tools.cdi.core.test/resources/tck/tests/jbt/validation/inject/FarmBroken.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: trunk/cdi/tests/org.jboss.tools.cdi.core.test/resources/tck/tests/jbt/validation/inject/Sheep.java
===================================================================
--- trunk/cdi/tests/org.jboss.tools.cdi.core.test/resources/tck/tests/jbt/validation/inject/Sheep.java	                        (rev 0)
+++ trunk/cdi/tests/org.jboss.tools.cdi.core.test/resources/tck/tests/jbt/validation/inject/Sheep.java	2010-05-28 09:53:29 UTC (rev 22404)
@@ -0,0 +1,22 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., 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.jsr299.tck.tests.jbt.validation.inject;
+
+class Sheep implements Animal
+{
+
+}


Property changes on: trunk/cdi/tests/org.jboss.tools.cdi.core.test/resources/tck/tests/jbt/validation/inject/Sheep.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: trunk/cdi/tests/org.jboss.tools.cdi.core.test/src/org/jboss/tools/cdi/core/test/tck/InjectionPointTest.java
===================================================================
--- trunk/cdi/tests/org.jboss.tools.cdi.core.test/src/org/jboss/tools/cdi/core/test/tck/InjectionPointTest.java	2010-05-28 09:06:52 UTC (rev 22403)
+++ trunk/cdi/tests/org.jboss.tools.cdi.core.test/src/org/jboss/tools/cdi/core/test/tck/InjectionPointTest.java	2010-05-28 09:53:29 UTC (rev 22404)
@@ -12,9 +12,12 @@
 
 import java.util.Set;
 
+import org.eclipse.jdt.core.ITypeParameter;
 import org.jboss.tools.cdi.core.CDIUtil;
 import org.jboss.tools.cdi.core.IClassBean;
+import org.jboss.tools.cdi.core.IInjectionPoint;
 import org.jboss.tools.cdi.core.IInjectionPointParameter;
+import org.jboss.tools.cdi.core.IParametedType;
 
 /**
  * @author Alexey Kazakov
@@ -22,7 +25,7 @@
 public class InjectionPointTest extends TCKTest {
 
 	/**
-	 * Section 3.7.1 - Declaring a bean constructor
+	 * Section 3.7.1 - Declaring a bean constructorThe bean was not found.
 	 *  - All parameters of a bean constructor are injection points.
 	 */
 	public void testQualifierTypeAnnotatedConstructor() {
@@ -40,4 +43,19 @@
 		Set<IInjectionPointParameter> points = CDIUtil.getInjectionPointParameters(bean);
 		assertEquals("There should be two injection point parameters in the bean.", 1, points.size());
 	}
+
+	// https://jira.jboss.org/browse/JBIDE-6387 Type of a method parameter is null in case of generic method.
+	public void testMethodParameter() {
+		IClassBean bean = getClassBean("JavaSource/org/jboss/jsr299/tck/tests/jbt/validation/inject/FarmBroken.java");
+		assertNotNull("Can't find the bean.", bean);
+		Set<IInjectionPoint> injections = bean.getInjectionPoints();
+		for (IInjectionPoint injectionPoint : injections) {
+			if(injectionPoint instanceof IInjectionPointParameter) {
+				IInjectionPointParameter param = (IInjectionPointParameter)injectionPoint;
+				IParametedType type = param.getType();
+				assertNotNull("Type of the parameter is null", type);
+				assertNotNull("Signature of parameter type is null", type.getSignature());
+			}
+		}
+	}
 }
\ No newline at end of file

Modified: trunk/cdi/tests/org.jboss.tools.cdi.core.test/src/org/jboss/tools/cdi/core/test/tck/ValidationTest.java
===================================================================
--- trunk/cdi/tests/org.jboss.tools.cdi.core.test/src/org/jboss/tools/cdi/core/test/tck/ValidationTest.java	2010-05-28 09:06:52 UTC (rev 22403)
+++ trunk/cdi/tests/org.jboss.tools.cdi.core.test/src/org/jboss/tools/cdi/core/test/tck/ValidationTest.java	2010-05-28 09:53:29 UTC (rev 22404)
@@ -886,6 +886,17 @@
 	}
 
 	/**
+	 * 5.2.2. Legal injection point types
+	 *  - injection point type is a type variable
+	 * 
+	 * @throws Exception
+	 */
+	public void testTypeVariableInjectionPoint() throws Exception {
+		IFile file = tckProject.getFile("JavaSource/org/jboss/jsr299/tck/tests/jbt/validation/inject/FarmBroken.java");
+		AbstractResourceMarkerTest.assertMarkerIsCreated(file, AbstractResourceMarkerTest.MARKER_TYPE, CDIValidationMessages.INJECTION_TYPE_IS_VARIABLE, 11, 15);
+	}
+
+	/**
 	 * 10.4.2. Declaring an observer method
 	 *  - method has more than one parameter annotated @Observes
 	 *  



More information about the jbosstools-commits mailing list