Author: scabanovich
Date: 2010-04-27 12:24:14 -0400 (Tue, 27 Apr 2010)
New Revision: 21754
Added:
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/Parameter.java
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/definition/ParameterDefinition.java
Modified:
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/core/IClassBean.java
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/core/IParameter.java
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/BeanMethod.java
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/ClassBean.java
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/definition/MethodDefinition.java
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/validation/CDICoreValidator.java
Log:
https://jira.jboss.org/jira/browse/JBIDE-6195
Modified:
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/core/IClassBean.java
===================================================================
---
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/core/IClassBean.java 2010-04-27
16:14:29 UTC (rev 21753)
+++
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/core/IClassBean.java 2010-04-27
16:24:14 UTC (rev 21754)
@@ -52,7 +52,7 @@
*
* @return a set of observer methods of the bean
*/
- Set<IObserverMethod> getObserverMethods();
+ Set<IBeanMethod> getObserverMethods();
/**
Modified:
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/core/IParameter.java
===================================================================
---
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/core/IParameter.java 2010-04-27
16:14:29 UTC (rev 21753)
+++
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/core/IParameter.java 2010-04-27
16:24:14 UTC (rev 21754)
@@ -12,6 +12,8 @@
import java.util.Set;
+import org.jboss.tools.common.text.ITextSourceReference;
+
/**
* Represents a parameter of a method which is a member of bean class.
*
@@ -39,4 +41,16 @@
* @return the name of this parameter.
*/
String getName();
+
+ /**
+ * Get position of element annotation of a certain annotation type.
+ * This method currently replaces IAnnotated.getAnnotation method
+ * which cannot be implemented until JDT extend model for parameters.
+ *
+ * @param annotationTypeName
+ * the name of the annotation type
+ * @return the element annotation of the given annotation type, or a null
+ * value
+ */
+ public ITextSourceReference getAnnotationPosition(String annotationTypeName);
}
\ No newline at end of file
Modified:
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/BeanMethod.java
===================================================================
---
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/BeanMethod.java 2010-04-27
16:14:29 UTC (rev 21753)
+++
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/BeanMethod.java 2010-04-27
16:24:14 UTC (rev 21754)
@@ -15,9 +15,11 @@
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IMethod;
+import org.jboss.tools.cdi.core.CDIConstants;
import org.jboss.tools.cdi.core.IBeanMethod;
import org.jboss.tools.cdi.core.IParameter;
import org.jboss.tools.cdi.internal.core.impl.definition.MethodDefinition;
+import org.jboss.tools.cdi.internal.core.impl.definition.ParameterDefinition;
/**
*
@@ -28,12 +30,22 @@
protected IMethod method;
protected AnnotationDeclaration inject;
+ List<IParameter> parameters = new ArrayList<IParameter>();
+
public BeanMethod() {}
public void setDefinition(MethodDefinition definition) {
super.setDefinition(definition);
setMethod(definition.getMethod());
inject = definition.getInjectAnnotation();
+
+ List<ParameterDefinition> ps = definition.getParameters();
+ for (ParameterDefinition p: ps) {
+ Parameter parameter = new Parameter();
+ parameter.setBeanMethod(this);
+ parameter.setDefinition(p);
+ parameters.add(parameter);
+ }
}
public IMethod getMethod() {
@@ -50,11 +62,25 @@
}
public List<IParameter> getParameters() {
- // TODO
- return new ArrayList<IParameter>();
+ return parameters;
}
public MethodDefinition getDefinition() {
return (MethodDefinition)definition;
}
+
+ public boolean isDisposer() {
+ for (IParameter p: parameters) {
+ if(p.isAnnotationPresent(CDIConstants.DISPOSES_ANNOTATION_TYPE_NAME)) return true;
+ }
+ return false;
+ }
+
+ public boolean isObserver() {
+ for (IParameter p: parameters) {
+ if(p.isAnnotationPresent(CDIConstants.OBSERVERS_ANNOTATION_TYPE_NAME)) return true;
+ }
+ return false;
+ }
+
}
Modified:
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/ClassBean.java
===================================================================
---
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/ClassBean.java 2010-04-27
16:14:29 UTC (rev 21753)
+++
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/ClassBean.java 2010-04-27
16:24:14 UTC (rev 21754)
@@ -157,8 +157,13 @@
}
public Set<IBeanMethod> getDisposers() {
- // TODO
- return new HashSet<IBeanMethod>();
+ Set<IBeanMethod> result = new HashSet<IBeanMethod>();
+ for (BeanMethod m: methods) {
+ if(m.isDisposer()) {
+ result.add(m);
+ }
+ }
+ return result;
}
public Set<IInterceptorBindingDeclaration> getInterceptorBindings() {
@@ -172,11 +177,11 @@
return result;
}
- public Set<IObserverMethod> getObserverMethods() {
- Set<IObserverMethod> result = new HashSet<IObserverMethod>();
+ public Set<IBeanMethod> getObserverMethods() {
+ Set<IBeanMethod> result = new HashSet<IBeanMethod>();
for (BeanMethod m: methods) {
- if(m instanceof IObserverMethod) {
- result.add((IObserverMethod)m);
+ if(m.isDisposer()) {
+ result.add(m);
}
}
return result;
Added:
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/Parameter.java
===================================================================
---
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/Parameter.java
(rev 0)
+++
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/Parameter.java 2010-04-27
16:24:14 UTC (rev 21754)
@@ -0,0 +1,75 @@
+package org.jboss.tools.cdi.internal.core.impl;
+
+import java.util.List;
+import java.util.Set;
+
+import org.eclipse.jdt.core.IMember;
+import org.jboss.tools.cdi.core.IAnnotationDeclaration;
+import org.jboss.tools.cdi.core.IClassBean;
+import org.jboss.tools.cdi.core.IParametedType;
+import org.jboss.tools.cdi.core.IParameter;
+import org.jboss.tools.cdi.internal.core.impl.definition.ParameterDefinition;
+import org.jboss.tools.common.text.ITextSourceReference;
+
+public class Parameter extends CDIElement implements IParameter {
+ ParameterDefinition definition;
+ BeanMethod beanMethod;
+
+ public Parameter() {}
+
+ public void setBeanMethod(BeanMethod beanMethod) {
+ this.beanMethod = beanMethod;
+ setParent(beanMethod);
+ }
+
+ public void setDefinition(ParameterDefinition definition) {
+ this.definition = definition;
+ }
+
+ public Set<IAnnotationDeclaration> getAnnotationDeclarations() {
+ throw new RuntimeException("Not implemented because limitations of JDT
model.");
+ }
+
+ public String getName() {
+ return definition.getName();
+ }
+
+ public IParametedType getType() {
+ return definition.getType();
+ }
+
+ public IClassBean getClassBean() {
+ return beanMethod.getClassBean();
+ }
+
+ public IMember getSourceMember() {
+ return definition.getMethodDefinition().getMethod();
+ }
+
+ public int getLength() {
+ ITextSourceReference p = definition.getPosition();
+ return p == null ? 0 : p.getLength();
+ }
+
+ public int getStartPosition() {
+ ITextSourceReference p = definition.getPosition();
+ return p == null ? 0 : p.getStartPosition();
+ }
+
+ public IAnnotationDeclaration getAnnotation(String annotationTypeName) {
+ throw new RuntimeException("Not implemented because limitations of JDT
model.");
+ }
+
+ public List<AnnotationDeclaration> getAnnotations() {
+ throw new RuntimeException("Not implemented because limitations of JDT
model.");
+ }
+
+ public boolean isAnnotationPresent(String annotationTypeName) {
+ return definition.isAnnotationPresent(annotationTypeName);
+ }
+
+ public ITextSourceReference getAnnotationPosition(String annotationTypeName) {
+ return definition.getAnnotationPosition(annotationTypeName);
+ }
+
+}
Property changes on:
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/Parameter.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified:
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/definition/MethodDefinition.java
===================================================================
---
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/definition/MethodDefinition.java 2010-04-27
16:14:29 UTC (rev 21753)
+++
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/definition/MethodDefinition.java 2010-04-27
16:24:14 UTC (rev 21754)
@@ -10,9 +10,18 @@
******************************************************************************/
package org.jboss.tools.cdi.internal.core.impl.definition;
+import java.util.ArrayList;
+import java.util.List;
+
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IMethod;
+import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.IType;
+import org.jboss.tools.cdi.core.CDIConstants;
+import org.jboss.tools.cdi.core.IParameter;
+import org.jboss.tools.cdi.internal.core.impl.ParametedType;
+import org.jboss.tools.common.model.project.ext.impl.ValueInfo;
+import org.jboss.tools.common.model.util.EclipseJavaUtil;
/**
*
@@ -23,6 +32,8 @@
IMethod method;
boolean isConstructor;
+ List<ParameterDefinition> parameters = new
ArrayList<ParameterDefinition>();
+
public MethodDefinition() {}
public void setMethod(IMethod method, DefinitionContext context) {
@@ -42,12 +53,86 @@
super.init(contextType, context);
isConstructor = method.isConstructor();
//TODO process parameters for disposers and observers
+ loadParamDefinitions(contextType, context);
}
+ void loadParamDefinitions(IType contextType, DefinitionContext context) throws
CoreException {
+ if(method == null) return;
+ String[] parameterNames = method.getParameterNames();
+ if(parameterNames == null || parameterNames.length == 0) return;
+ if(contextType == null || contextType.isBinary()) return;
+ String content = typeDefinition.getContent();
+ if(content == null) return;
+ ISourceRange range = method.getSourceRange();
+ ISourceRange nameRange = method.getNameRange();
+ if(nameRange != null) range = nameRange;
+ int paramStart = content.indexOf('(', range.getOffset());
+ if(paramStart < 0) return;
+ int paramEnd = content.indexOf(')', paramStart);
+ if(paramEnd < 0) return;
+ String paramsString = content.substring(paramStart + 1, paramEnd);
+ if(paramsString.indexOf('@') < 0) return;
+ String[] params = paramsString.split(",");
+ String[] ps = method.getParameterTypes();
+ int start = paramStart + 1;
+
+ for (int i = 0; i < params.length; i++) {
+ if(params[i].indexOf('@') < 0) continue; //do not need parameters without
annotation
+
+ ParameterDefinition pd = new ParameterDefinition();
+
+ ParametedType type =
context.getProject().getTypeFactory().getParametedType(contextType, ps[i]);
+
+ pd.methodDefinition = this;
+ pd.name = parameterNames[i];
+ pd.index = i;
+ pd.type = type;
+ ValueInfo v = new ValueInfo();
+ v.setValue(params[i]);
+ v.valueStartPosition = start;
+ v.valueLength = params[i].length();
+ pd.setPosition(v);
+
+ String[] tokens = params[i].split(" ");
+ for (String q: tokens) {
+ if(!q.startsWith("@")) continue;
+ v = new ValueInfo();
+ v.setValue(q);
+ v.valueStartPosition = start + params[i].indexOf(q);
+ v.valueLength = q.length();
+ String annotationType = EclipseJavaUtil.resolveType(contextType,
q.substring(1).trim());
+ if(annotationType != null) pd.annotationsByTypeName.put(annotationType, v);
+ }
+
+ parameters.add(pd);
+
+ start += params[i].length() + 1;
+ }
+
+ }
+
public boolean isCDIAnnotated() {
- //TODO return true if it is disposer or observer
- return super.isCDIAnnotated();
+ return super.isCDIAnnotated() || isDisposer() || isObserver();
}
+ public List<ParameterDefinition> getParameters() {
+ return parameters;
+ }
+
+ public boolean isDisposer() {
+ for (ParameterDefinition p: parameters) {
+ if(p.isAnnotationPresent(CDIConstants.DISPOSES_ANNOTATION_TYPE_NAME)) return true;
+ }
+ return false;
+ }
+
+ public boolean isObserver() {
+ for (ParameterDefinition p: parameters) {
+ if(p.isAnnotationPresent(CDIConstants.OBSERVERS_ANNOTATION_TYPE_NAME)) return true;
+ }
+ return false;
+ }
+
+
}
Added:
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/definition/ParameterDefinition.java
===================================================================
---
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/definition/ParameterDefinition.java
(rev 0)
+++
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/definition/ParameterDefinition.java 2010-04-27
16:24:14 UTC (rev 21754)
@@ -0,0 +1,60 @@
+package org.jboss.tools.cdi.internal.core.impl.definition;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.jboss.tools.cdi.core.IAnnotated;
+import org.jboss.tools.cdi.core.IAnnotationDeclaration;
+import org.jboss.tools.cdi.internal.core.impl.AnnotationDeclaration;
+import org.jboss.tools.cdi.internal.core.impl.ParametedType;
+import org.jboss.tools.common.text.ITextSourceReference;
+
+public class ParameterDefinition implements IAnnotated {
+ protected MethodDefinition methodDefinition;
+
+ protected String name;
+ protected ParametedType type;
+ protected int index;
+
+ protected ITextSourceReference position = null;
+ Map<String, ITextSourceReference> annotationsByTypeName = new HashMap<String,
ITextSourceReference>();
+
+ public ParameterDefinition() {}
+
+ public String getName() {
+ return name;
+ }
+
+ public ParametedType getType() {
+ return type;
+ }
+
+ public MethodDefinition getMethodDefinition() {
+ return methodDefinition;
+ }
+
+ public IAnnotationDeclaration getAnnotation(String annotationTypeName) {
+ return null;
+ }
+
+ public List<AnnotationDeclaration> getAnnotations() {
+ return null;
+ }
+
+ public boolean isAnnotationPresent(String annotationTypeName) {
+ return annotationsByTypeName.containsKey(annotationTypeName);
+ }
+
+ public ITextSourceReference getAnnotationPosition(String annotationTypeName) {
+ return annotationsByTypeName.get(annotationTypeName);
+ }
+
+ public void setPosition(ITextSourceReference position) {
+ this.position = position;
+ }
+
+ public ITextSourceReference getPosition() {
+ return position;
+ }
+}
Property changes on:
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/impl/definition/ParameterDefinition.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
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-04-27
16:14:29 UTC (rev 21753)
+++
trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/internal/core/validation/CDICoreValidator.java 2010-04-27
16:24:14 UTC (rev 21754)
@@ -316,15 +316,15 @@
* 3.3.6. Declaring a disposer method
* - method has more than one parameter annotated @Disposes
*/
- Set<IAnnotationDeclaration> disposerDeclarations = new
HashSet<IAnnotationDeclaration>();
+ Set<ITextSourceReference> disposerDeclarations = new
HashSet<ITextSourceReference>();
for (IParameter param : params) {
- IAnnotationDeclaration declaration =
param.getAnnotation(CDIConstants.DISPOSES_ANNOTATION_TYPE_NAME);
+ ITextSourceReference declaration =
param.getAnnotationPosition(CDIConstants.DISPOSES_ANNOTATION_TYPE_NAME);
if(declaration!=null) {
disposerDeclarations.add(declaration);
}
}
if(disposerDeclarations.size()>1) {
- for (IAnnotationDeclaration declaration : disposerDeclarations) {
+ for (ITextSourceReference declaration : disposerDeclarations) {
addError(CDIValidationMessages.MULTIPLE_DISPOSING_PARAMETERS,
CDIPreferences.MULTIPLE_DISPOSING_PARAMETERS, declaration, bean.getResource());
}
}
@@ -335,18 +335,18 @@
* 10.4.2. Declaring an observer method
* - a observer method has a parameter annotated @Disposes.
*/
- Set<IAnnotationDeclaration> declarations = new
HashSet<IAnnotationDeclaration>();
+ Set<ITextSourceReference> declarations = new
HashSet<ITextSourceReference>();
boolean observesExists = false;
declarations.addAll(disposerDeclarations);
for (IParameter param : params) {
- IAnnotationDeclaration declaration =
param.getAnnotation(CDIConstants.OBSERVERS_ANNOTATION_TYPE_NAME);
+ ITextSourceReference declaration =
param.getAnnotationPosition(CDIConstants.OBSERVERS_ANNOTATION_TYPE_NAME);
if(declaration!=null) {
declarations.add(declaration);
observesExists = true;
}
}
if(observesExists) {
- for (IAnnotationDeclaration declaration : declarations) {
+ for (ITextSourceReference declaration : declarations) {
addError(CDIValidationMessages.OBSERVER_PARAMETER_ILLEGALLY_ANNOTATED,
CDIPreferences.OBSERVER_PARAMETER_ILLEGALLY_ANNOTATED, declaration, bean.getResource());
}
}
@@ -360,7 +360,7 @@
IAnnotationDeclaration injectDeclaration =
disposer.getAnnotation(CDIConstants.INJECT_ANNOTATION_TYPE_NAME);
if(injectDeclaration!=null) {
addError(CDIValidationMessages.DISPOSER_ANNOTATED_INJECT,
CDIPreferences.DISPOSER_ANNOTATED_INJECT, injectDeclaration, bean.getResource());
- for (IAnnotationDeclaration declaration : disposerDeclarations) {
+ for (ITextSourceReference declaration : disposerDeclarations) {
addError(CDIValidationMessages.DISPOSER_ANNOTATED_INJECT,
CDIPreferences.DISPOSER_ANNOTATED_INJECT, declaration, bean.getResource());
}
}
@@ -381,7 +381,7 @@
* @param annotatedParams
* @param errorKey
*/
- private void validateSessionBeanMethod(IClassBean bean, IBeanMethod method,
Set<IAnnotationDeclaration> annotatedParams, String errorMessageKey, String
preferencesKey) {
+ private void validateSessionBeanMethod(IClassBean bean, IBeanMethod method,
Set<ITextSourceReference> annotatedParams, String errorMessageKey, String
preferencesKey) {
if(bean instanceof ISessionBean) {
if(annotatedParams!=null) {
try {
@@ -407,7 +407,7 @@
}
}
if(!businessMethod) {
- for (IAnnotationDeclaration declaration : annotatedParams) {
+ for (ITextSourceReference declaration : annotatedParams) {
addError(errorMessageKey, preferencesKey, declaration, bean.getResource());
}
}
@@ -444,7 +444,7 @@
} else {
IProducerMethod producerMethod = (IProducerMethod)producer;
List<IParameter> params = producerMethod.getParameters();
- Set<IAnnotationDeclaration> declarations = new
HashSet<IAnnotationDeclaration>();
+ Set<ITextSourceReference> declarations = new
HashSet<ITextSourceReference>();
declarations.add(producerMethod.getAnnotation(CDIConstants.PRODUCES_ANNOTATION_TYPE_NAME));
for (IParameter param : params) {
/*
@@ -454,7 +454,7 @@
* 3.3.2. Declaring a producer method
* - a has a parameter annotated @Disposes
*/
- IAnnotationDeclaration declaration =
param.getAnnotation(CDIConstants.DISPOSES_ANNOTATION_TYPE_NAME);
+ ITextSourceReference declaration =
param.getAnnotationPosition(CDIConstants.DISPOSES_ANNOTATION_TYPE_NAME);
if(declaration!=null) {
declarations.add(declaration);
}
@@ -465,13 +465,13 @@
* 10.4.2. Declaring an observer method
* - an observer method is annotated @Produces
*/
- declaration = param.getAnnotation(CDIConstants.OBSERVERS_ANNOTATION_TYPE_NAME);
+ declaration =
param.getAnnotationPosition(CDIConstants.OBSERVERS_ANNOTATION_TYPE_NAME);
if(declaration!=null) {
declarations.add(declaration);
}
}
if(declarations.size()>1) {
- for (IAnnotationDeclaration declaration : declarations) {
+ for (ITextSourceReference declaration : declarations) {
addError(CDIValidationMessages.PRODUCER_PARAMETER_ILLEGALLY_ANNOTATED,
CDIPreferences.PRODUCER_PARAMETER_ILLEGALLY_ANNOTATED, declaration,
producer.getResource());
}
}