Author: dazarov
Date: 2011-11-14 19:04:21 -0500 (Mon, 14 Nov 2011)
New Revision: 36336
Modified:
trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/marker/AddQualifiersToBeanProcessor.java
trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/marker/MarkerResolutionUtils.java
trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/wizard/xpl/AddQualifiersToBeanComposite.java
Log:
Quickfix and Wizard for fixing ambigious injection warning is confusing
https://issues.jboss.org/browse/JBIDE-9940
Modified:
trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/marker/AddQualifiersToBeanProcessor.java
===================================================================
---
trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/marker/AddQualifiersToBeanProcessor.java 2011-11-14
22:10:18 UTC (rev 36335)
+++
trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/marker/AddQualifiersToBeanProcessor.java 2011-11-15
00:04:21 UTC (rev 36336)
@@ -17,14 +17,19 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.refactoring.CompilationUnitChange;
import org.eclipse.ltk.core.refactoring.CompositeChange;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
+import org.eclipse.ltk.core.refactoring.TextFileChange;
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
+import org.eclipse.text.edits.MultiTextEdit;
import org.jboss.tools.cdi.core.CDICoreMessages;
import org.jboss.tools.cdi.core.IBean;
import org.jboss.tools.cdi.core.IInjectionPoint;
import org.jboss.tools.cdi.ui.refactoring.CDIRefactoringProcessor;
import org.jboss.tools.cdi.ui.wizard.xpl.AddQualifiersToBeanComposite.ValuedQualifier;
+import org.jboss.tools.common.EclipseUtil;
public class AddQualifiersToBeanProcessor extends CDIRefactoringProcessor {
protected IBean selectedBean;
@@ -73,11 +78,37 @@
}
createRootChange();
+
+ IFile file = (IFile)selectedBean.getBeanClass().getResource();
+ ICompilationUnit compilationUnit = EclipseUtil.getCompilationUnit(file);
+
+ CompilationUnitChange fileChange = new CompilationUnitChange(file.getName(),
compilationUnit);
+
+ MultiTextEdit edit = new MultiTextEdit();
- MarkerResolutionUtils.addQualifiersToBean(qualifiers, selectedBean, rootChange);
+ MarkerResolutionUtils.addQualifiersToBean(qualifiers, selectedBean, compilationUnit,
edit);
+
+ IFile file2 = (IFile)injectionPoint.getClassBean().getResource();
+ ICompilationUnit compilationUnit2 =
injectionPoint.getClassBean().getBeanClass().getCompilationUnit();
+
+ if(!compilationUnit.equals(compilationUnit2)){
+ if(edit.getChildrenSize() > 0){
+ fileChange.setEdit(edit);
+ rootChange.add(fileChange);
+ }
+ fileChange = new CompilationUnitChange(file2.getName(), compilationUnit2);
+
+ edit = new MultiTextEdit();
+ }else{
+ compilationUnit2 = compilationUnit;
+ }
- MarkerResolutionUtils.addQualifiersToInjectionPoint(qualifiers, injectionPoint,
rootChange);
+ MarkerResolutionUtils.addQualifiersToInjectionPoint(qualifiers, injectionPoint,
compilationUnit2, edit);
+ if(edit.getChildrenSize() > 0){
+ fileChange.setEdit(edit);
+ rootChange.add(fileChange);
+ }
return status;
}
Modified:
trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/marker/MarkerResolutionUtils.java
===================================================================
---
trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/marker/MarkerResolutionUtils.java 2011-11-14
22:10:18 UTC (rev 36335)
+++
trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/marker/MarkerResolutionUtils.java 2011-11-15
00:04:21 UTC (rev 36336)
@@ -18,7 +18,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.Flags;
@@ -40,8 +39,6 @@
import org.eclipse.jdt.core.ITypeParameter;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.Signature;
-import org.eclipse.ltk.core.refactoring.CompositeChange;
-import org.eclipse.ltk.core.refactoring.TextFileChange;
import org.eclipse.text.edits.DeleteEdit;
import org.eclipse.text.edits.InsertEdit;
import org.eclipse.text.edits.MultiTextEdit;
@@ -58,8 +55,8 @@
import org.jboss.tools.cdi.core.IQualifierDeclaration;
import org.jboss.tools.cdi.ui.CDIUIPlugin;
import org.jboss.tools.cdi.ui.wizard.xpl.AddQualifiersToBeanComposite.ValuedQualifier;
-import org.jboss.tools.common.EclipseUtil;
import org.jboss.tools.common.model.util.EclipseJavaUtil;
+import org.jboss.tools.common.util.BeanUtil;
/**
* @author Daniel Azarov
@@ -178,13 +175,24 @@
compilationUnit.createImport(qualifiedName, null, new NullProgressMonitor());
}
}else{
- TextEdit edit = new
InsertEdit(compilationUnit.getImportContainer().getSourceRange().getOffset()+compilationUnit.getImportContainer().getSourceRange().getLength(),
compilationUnit.findRecommendedLineSeparator()+IMPORT+SPACE+qualifiedName+SEMICOLON);
- rootEdit.addChild(edit);
+ String text =
compilationUnit.findRecommendedLineSeparator()+IMPORT+SPACE+qualifiedName+SEMICOLON;
+ if(!isDuplicate(rootEdit, text)){
+ TextEdit edit = new
InsertEdit(compilationUnit.getImportContainer().getSourceRange().getOffset()+compilationUnit.getImportContainer().getSourceRange().getLength(),
text);
+ rootEdit.addChild(edit);
+ }
}
}
return false;
}
+ private static boolean isDuplicate(MultiTextEdit rootEdit, String text){
+ for(TextEdit edit : rootEdit.getChildren()){
+ if(edit instanceof InsertEdit && ((InsertEdit) edit).getText().equals(text))
+ return true;
+ }
+ return false;
+ }
+
public static void addAnnotation(String qualifiedName, ICompilationUnit compilationUnit,
IJavaElement element) throws JavaModelException{
addAnnotation(qualifiedName, compilationUnit, element, "");
}
@@ -528,15 +536,8 @@
return null;
}
- public static void addQualifiersToInjectionPoint(List<ValuedQualifier> deployed,
IInjectionPoint injectionPoint, CompositeChange change){
- IFile file = (IFile)injectionPoint.getClassBean().getResource();
+ public static void addQualifiersToInjectionPoint(List<ValuedQualifier> deployed,
IInjectionPoint injectionPoint, ICompilationUnit compilationUnit, MultiTextEdit edit){
try{
- ICompilationUnit compilationUnit =
injectionPoint.getClassBean().getBeanClass().getCompilationUnit();
-
- TextFileChange fileChange = new TextFileChange(file.getName(), file);
-
- MultiTextEdit edit = new MultiTextEdit();
-
if(injectionPoint instanceof IInjectionPointParameter){
addQualifiersToParameter(compilationUnit, injectionPoint, deployed, edit);
}else{
@@ -560,20 +561,13 @@
}
}
}
-
- if(edit.getChildrenSize() > 0){
- fileChange.setEdit(edit);
- change.add(fileChange);
- }
}catch(CoreException ex){
CDIUIPlugin.getDefault().logError(ex);
}
}
- public static void addQualifiersToBean(List<ValuedQualifier> deployed, IBean bean,
CompositeChange change){
- IFile file = (IFile)bean.getBeanClass().getResource();
-
+ public static void addQualifiersToBean(List<ValuedQualifier> deployed, IBean bean,
ICompilationUnit compilationUnit, MultiTextEdit edit){
IJavaElement beanElement = null;
if(bean instanceof IBeanField){
beanElement = ((IBeanField) bean).getField();
@@ -584,12 +578,6 @@
}
try{
- ICompilationUnit compilationUnit = EclipseUtil.getCompilationUnit(file);
-
- TextFileChange fileChange = new TextFileChange(file.getName(), file);
-
- MultiTextEdit edit = new MultiTextEdit();
-
for(IQualifierDeclaration declaration : bean.getQualifierDeclarations()){
IQualifier qualifier = declaration.getQualifier();
String qualifierName = qualifier.getSourceType().getFullyQualifiedName();
@@ -611,11 +599,6 @@
}
}
-
- if(edit.getChildrenSize() > 0){
- fileChange.setEdit(edit);
- change.add(fileChange);
- }
}catch(CoreException ex){
CDIUIPlugin.getDefault().logError(ex);
}
@@ -856,4 +839,23 @@
}
return null;
}
+
+ public static String getELName(IBean bean){
+ String name;
+ if(bean instanceof IBeanField){
+ name = ((IBeanField) bean).getField().getElementName();
+ }else if(bean instanceof IBeanMethod){
+ name = ((IBeanMethod) bean).getMethod().getElementName();
+ if(BeanUtil.isGetter(((IBeanMethod) bean).getMethod())) {
+ return BeanUtil.getPropertyName(name);
+ }
+ }else{
+ name = bean.getBeanClass().getElementName();
+ if(name.length() > 0) {
+ name = name.substring(0, 1).toLowerCase() + name.substring(1);
+ }
+ }
+
+ return name;
+ }
}
Modified:
trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/wizard/xpl/AddQualifiersToBeanComposite.java
===================================================================
---
trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/wizard/xpl/AddQualifiersToBeanComposite.java 2011-11-14
22:10:18 UTC (rev 36335)
+++
trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/wizard/xpl/AddQualifiersToBeanComposite.java 2011-11-15
00:04:21 UTC (rev 36336)
@@ -133,6 +133,7 @@
public void init(IBean bean){
this.bean = bean;
+ String beanName = MarkerResolutionUtils.getELName(bean);
originalQualifiers.clear();
deployed.clear();
for(IQualifier q : bean.getQualifiers()){
@@ -148,6 +149,7 @@
defaultQualifier = new
ValuedQualifier(bean.getCDIProject().getQualifier(CDIConstants.DEFAULT_QUALIFIER_TYPE_NAME));
namedQualifier = new
ValuedQualifier(bean.getCDIProject().getQualifier(CDIConstants.NAMED_QUALIFIER_TYPE_NAME));
+ namedQualifier.setValue(beanName);
for(ValuedQualifier q : originalQualifiers){
if(q.equals(defaultQualifier)){
@@ -174,6 +176,8 @@
}
private ValuedQualifier loadAvailableQualifiers(){
+ String beanName = MarkerResolutionUtils.getELName(bean);
+
ValuedQualifier lastQualifier = null;
String beanTypeName = bean.getBeanClass().getFullyQualifiedName();
String beanPackage =
beanTypeName.substring(0,beanTypeName.lastIndexOf(MarkerResolutionUtils.DOT));
@@ -197,6 +201,8 @@
String qualifierPackage =
qualifierTypeName.substring(0,qualifierTypeName.lastIndexOf(MarkerResolutionUtils.DOT));
if((isPublic || (samePackage &&
injectionPointPackage.equals(qualifierPackage))) ){
if(beanJavaProject.findType(qualifierTypeName) != null &&
injectionPointJavaProject.findType(qualifierTypeName) != null){
+ if(q.getSourceType().getFullyQualifiedName().equals(CDIConstants.NAMED_QUALIFIER_TYPE_NAME))
+ vq.setValue(beanName);
qualifiers.add(vq);
availableTableViewer.add(vq);
lastQualifier = vq;