Author: akazakov
Date: 2010-05-20 16:08:36 -0400 (Thu, 20 May 2010)
New Revision: 22224
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/CDIValidationMessages.java
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/validation/messages.properties
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/ValidationTest.java
Log:
https://jira.jboss.org/jira/browse/JBIDE-2708 Added new CDI validation rule: - method
annotated @Specializes is static or does not directly override another producer method
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-20
19:34:53 UTC (rev 22223)
+++
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/core/CDIUtil.java 2010-05-20
20:08:36 UTC (rev 22224)
@@ -11,8 +11,10 @@
package org.jboss.tools.cdi.core;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
+import java.util.Map;
import java.util.Set;
import org.eclipse.core.resources.IProject;
@@ -338,6 +340,9 @@
if (sourceType == null) {
continue;
}
+ if(!sourceType.isInterface()) {
+ continue;
+ }
IAnnotation annotation =
sourceType.getAnnotation(CDIConstants.LOCAL_ANNOTATION_TYPE_NAME);
if (annotation == null) {
annotation = sourceType.getAnnotation("Local"); //$NON-NLS-N1
@@ -359,4 +364,92 @@
}
return method.getMethod();
}
+
+ /**
+ * Finds the method which is overridden by the given method. Or null if this method
overrides nothing.
+ *
+ * @param method
+ * @return
+ */
+ public static IMethod getOverridingMethodDeclaration(IBeanMethod method) {
+ IClassBean bean = method.getClassBean();
+ Map<IType, IMethod> foundMethods = new HashMap<IType, IMethod>();
+ try {
+ if (Flags.isStatic(method.getMethod().getFlags())) {
+ return null;
+ }
+ Set<IParametedType> types = bean.getLegalTypes();
+ for (IParametedType type : types) {
+ IType sourceType = type.getType();
+ if (sourceType == null || sourceType.isInterface()) {
+ continue;
+ }
+ IMethod[] methods = sourceType.getMethods();
+ for (IMethod iMethod : methods) {
+ if (method.getMethod().isSimilar(iMethod)) {
+ foundMethods.put(iMethod.getDeclaringType(), iMethod);
+ }
+ }
+ }
+ if(foundMethods.size()==1) {
+ return foundMethods.values().iterator().next();
+ } else if(foundMethods.size()>1) {
+ IType type = bean.getBeanClass();
+ IType superClass = getSuperClass(type);
+ while(superClass!=null) {
+ IMethod m = foundMethods.get(superClass);
+ if(m!=null) {
+ return m;
+ }
+ superClass = getSuperClass(superClass);
+ }
+ }
+ } catch (JavaModelException e) {
+ CDICorePlugin.getDefault().logError(e);
+ }
+ return null;
+ }
+
+ /**
+ * Finds the method which is overridden by the given method. Or null if this method
overrides nothing.
+ *
+ * @param method
+ * @return
+ */
+ public static IMethod getDirectOverridingMethodDeclaration(IBeanMethod method) {
+ IClassBean bean = method.getClassBean();
+ try {
+ if (Flags.isStatic(method.getMethod().getFlags())) {
+ return null;
+ }
+ IType type = bean.getBeanClass();
+ IType superClass = getSuperClass(type);
+ if(superClass!=null) {
+ IMethod[] methods = superClass.getMethods();
+ for (IMethod iMethod : methods) {
+ if (method.getMethod().isSimilar(iMethod)) {
+ return iMethod;
+ }
+ }
+ }
+ } catch (JavaModelException e) {
+ CDICorePlugin.getDefault().logError(e);
+ }
+ return null;
+ }
+
+ private static IType getSuperClass(IType type) throws JavaModelException {
+ String superclassName = type.getSuperclassName();
+ if(superclassName!=null) {
+ String fullySuperclassName = EclipseJavaUtil.resolveType(type, superclassName);
+ if(fullySuperclassName!=null&&!fullySuperclassName.equals("java.lang.Object"))
{ //$NON-NLS-1$
+ if(fullySuperclassName.equals(type.getFullyQualifiedName())) {
+ return null;
+ }
+ IType superType = type.getJavaProject().findType(fullySuperclassName);
+ return superType;
+ }
+ }
+ return null;
+ }
}
\ No newline at end of file
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-20
19:34:53 UTC (rev 22223)
+++
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/validation/CDICoreValidator.java 2010-05-20
20:08:36 UTC (rev 22224)
@@ -27,6 +27,7 @@
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.Flags;
+import org.eclipse.jdt.core.IAnnotation;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IMemberValuePair;
import org.eclipse.jdt.core.IMethod;
@@ -686,6 +687,49 @@
validationContext.addLinkedCoreResource(classBean.getSourcePath().toOSString(),
method.getResource().getFullPath(), false);
}
}
+
+ IAnnotationDeclaration sDeclaration =
producerMethod.getSpecializesAnnotationDeclaration();
+ if(sDeclaration!=null) {
+ if(Flags.isStatic(producerMethod.getMethod().getFlags())) {
+ /*
+ * 3.3.3. Specializing a producer method
+ * - method annotated @Specializes is static
+ */
+ addError(CDIValidationMessages.ILLEGAL_SPECIALIZING_PRODUCER_STATIC,
CDIPreferences.ILLEGAL_SPECIALIZING_PRODUCER, sDeclaration, producer.getResource());
+ } else {
+ /*
+ * 3.3.3. Specializing a producer method
+ * - method annotated @Specializes does not directly override another producer
method
+ */
+ IMethod superMethod =
CDIUtil.getDirectOverridingMethodDeclaration(producerMethod);
+ boolean overrides = false;
+ if(superMethod!=null) {
+ IType superType = superMethod.getDeclaringType();
+ if(superType.isBinary()) {
+ IAnnotation[] ants = superMethod.getAnnotations();
+ for (IAnnotation an : ants) {
+ if(CDIConstants.PRODUCES_ANNOTATION_TYPE_NAME.equals(an.getElementName())) {
+ overrides = true;
+ }
+ }
+ } else {
+ Set<IBean> beans =
cdiProject.getBeans(superType.getResource().getFullPath());
+ for (IBean iBean : beans) {
+ if(iBean instanceof IProducerMethod) {
+ IProducerMethod prMethod = (IProducerMethod)iBean;
+ if(prMethod.getMethod().isSimilar(superMethod)) {
+ overrides = true;
+ }
+ }
+ }
+ }
+ }
+ if(!overrides) {
+ addError(CDIValidationMessages.ILLEGAL_SPECIALIZING_PRODUCER_OVERRIDE,
CDIPreferences.ILLEGAL_SPECIALIZING_PRODUCER, sDeclaration, producer.getResource());
+ }
+ saveAllSuperTypesAsLinkedResources(producer.getClassBean());
+ }
+ }
}
} catch (JavaModelException e) {
CDICorePlugin.getDefault().logError(e);
Modified:
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/validation/CDIValidationMessages.java
===================================================================
---
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/validation/CDIValidationMessages.java 2010-05-20
19:34:53 UTC (rev 22223)
+++
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/validation/CDIValidationMessages.java 2010-05-20
20:08:36 UTC (rev 22224)
@@ -93,7 +93,8 @@
public static String ILLEGAL_SPECIALIZING_MANAGED_BEAN;
public static String ILLEGAL_SPECIALIZING_SESSION_BEAN;
- public static String ILLEGAL_SPECIALIZING_PRODUCER;
+ public static String ILLEGAL_SPECIALIZING_PRODUCER_STATIC;
+ public static String ILLEGAL_SPECIALIZING_PRODUCER_OVERRIDE;
public static String MISSING_TYPE_IN_SPECIALIZING_BEAN;
public static String CONFLICTING_NAME_IN_SPECIALIZING_BEAN;
public static String INTERCEPTOR_ANNOTATED_SPECIALIZES;
Modified:
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/validation/messages.properties
===================================================================
---
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/validation/messages.properties 2010-05-20
19:34:53 UTC (rev 22223)
+++
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/validation/messages.properties 2010-05-20
20:08:36 UTC (rev 22224)
@@ -72,7 +72,8 @@
ILLEGAL_SPECIALIZING_MANAGED_BEAN=Managed bean class annotated @Specializes does not
directly extend the bean class of another managed bean
ILLEGAL_SPECIALIZING_SESSION_BEAN=Session bean class annotated @Specializes does not
directly extend the bean class of another session bean
-ILLEGAL_SPECIALIZING_PRODUCER=Method annotated @Specializes is static or does not
directly override another producer method
+ILLEGAL_SPECIALIZING_PRODUCER_STATIC=Producer method annotated @Specializes is static
+ILLEGAL_SPECIALIZING_PRODUCER_OVERRIDE=Producer method annotated @Specializes does not
directly override another producer method
MISSING_TYPE_IN_SPECIALIZING_BEAN=Bean X specializes Y but does not have some bean type
of Y
CONFLICTING_NAME_IN_SPECIALIZING_BEAN=Bean X specializes Y and Y has a name and X
declares a name explicitly, using @Named
INTERCEPTOR_ANNOTATED_SPECIALIZES=Interceptor is annotated @Specializes
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-20
19:34:53 UTC (rev 22223)
+++
trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/preferences/CDIConfigurationBlock.java 2010-05-20
20:08:36 UTC (rev 22224)
@@ -124,7 +124,7 @@
new String[][]{
{CDIPreferences.ILLEGAL_SPECIALIZING_MANAGED_BEAN,
CDIPreferencesMessages.CDIValidatorConfigurationBlock_pb_illegalSpecializingManagedBean_label},
{CDIPreferences.ILLEGAL_SPECIALIZING_SESSION_BEAN,
CDIPreferencesMessages.CDIValidatorConfigurationBlock_pb_illegalSpecializingSessionBean_label},
-// {CDIPreferences.ILLEGAL_SPECIALIZING_PRODUCER,
CDIPreferencesMessages.CDIValidatorConfigurationBlock_pb_illegalSpecializingProducer_label},
+ {CDIPreferences.ILLEGAL_SPECIALIZING_PRODUCER,
CDIPreferencesMessages.CDIValidatorConfigurationBlock_pb_illegalSpecializingProducer_label},
// {CDIPreferences.MISSING_TYPE_IN_SPECIALIZING_BEAN,
CDIPreferencesMessages.CDIValidatorConfigurationBlock_pb_missingTypeInSpecializingBean_label},
// {CDIPreferences.CONFLICTING_NAME_IN_SPECIALIZING_BEAN,
CDIPreferencesMessages.CDIValidatorConfigurationBlock_pb_conflictingNameInSpecializingBean_label},
// {CDIPreferences.INTERCEPTOR_ANNOTATED_SPECIALIZES,
CDIPreferencesMessages.CDIValidatorConfigurationBlock_pb_interceptorAnnotatedSpecializes_label},
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-20
19:34:53 UTC (rev 22223)
+++
trunk/cdi/tests/org.jboss.tools.cdi.core.test/src/org/jboss/tools/cdi/core/test/tck/ValidationTest.java 2010-05-20
20:08:36 UTC (rev 22224)
@@ -527,6 +527,28 @@
}
/**
+ * 3.3.3. Specializing a producer method
+ * - method annotated @Specializes is static
+ *
+ * @throws Exception
+ */
+ public void testSpecializedStaticMethod() throws Exception {
+ IFile file =
tckProject.getFile("JavaSource/org/jboss/jsr299/tck/tests/inheritance/specialization/producer/method/broken/specializesStaticMethod/FurnitureShop_Broken.java");
+ AbstractResourceMarkerTest.assertMarkerIsCreated(file,
AbstractResourceMarkerTest.MARKER_TYPE,
CDIValidationMessages.ILLEGAL_SPECIALIZING_PRODUCER_STATIC, 24);
+ }
+
+ /**
+ * 3.3.3. Specializing a producer method
+ * - method annotated @Specializes does not directly override another producer method
+ *
+ * @throws Exception
+ */
+ public void testSpecializedMethodIndirectlyOverridesAnotherProducerMethod() throws
Exception {
+ IFile file =
tckProject.getFile("JavaSource/org/jboss/jsr299/tck/tests/inheritance/specialization/producer/method/broken/indirectOverride/ShoeShop_Broken.java");
+ AbstractResourceMarkerTest.assertMarkerIsCreated(file,
AbstractResourceMarkerTest.MARKER_TYPE,
CDIValidationMessages.ILLEGAL_SPECIALIZING_PRODUCER_OVERRIDE, 24);
+ }
+
+ /**
* 3.5.1. Declaring a resource
* - producer field declaration specifies an EL name (together with one of @Resource,
@PersistenceContext, @PersistenceUnit, @EJB, @WebServiceRef)
*