Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 39188 | Differences between
and this patch

Collapse All | Expand All

(-)core extension/org/eclipse/jdt/internal/corext/codemanipulation/AddCustomConstructorOperation.java (-32 / +19 lines)
Lines 18-37 Link Here
18
import org.eclipse.core.runtime.SubProgressMonitor;
18
import org.eclipse.core.runtime.SubProgressMonitor;
19
19
20
import org.eclipse.jdt.core.Flags;
20
import org.eclipse.jdt.core.Flags;
21
import org.eclipse.jdt.core.ICompilationUnit;
22
import org.eclipse.jdt.core.IField;
21
import org.eclipse.jdt.core.IField;
23
import org.eclipse.jdt.core.IJavaElement;
22
import org.eclipse.jdt.core.IJavaElement;
24
import org.eclipse.jdt.core.IJavaProject;
23
import org.eclipse.jdt.core.IJavaProject;
25
import org.eclipse.jdt.core.IMethod;
24
import org.eclipse.jdt.core.IMethod;
26
import org.eclipse.jdt.core.IType;
25
import org.eclipse.jdt.core.IType;
27
import org.eclipse.jdt.core.ITypeHierarchy;
26
import org.eclipse.jdt.core.ITypeHierarchy;
28
import org.eclipse.jdt.core.JavaModelException;
29
import org.eclipse.jdt.core.NamingConventions;
27
import org.eclipse.jdt.core.NamingConventions;
30
import org.eclipse.jdt.core.Signature;
28
import org.eclipse.jdt.core.Signature;
31
29
32
import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility.GenStubSettings;
33
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
34
35
/**
30
/**
36
 * Creates a custom constructor with fields initialized.
31
 * Creates a custom constructor with fields initialized.
37
 * If the type is open in an editor, be sure to pass over the types working working copy.
32
 * If the type is open in an editor, be sure to pass over the types working working copy.
Lines 43-50 Link Here
43
	private IMethod fConstructorCreated;
38
	private IMethod fConstructorCreated;
44
	private boolean fDoSave;
39
	private boolean fDoSave;
45
	private CodeGenerationSettings fSettings;
40
	private CodeGenerationSettings fSettings;
41
	private IMethod[] fSuperMethod;
46
	
42
	
47
	public AddCustomConstructorOperation(IType type, CodeGenerationSettings settings, IField[] selected, boolean save, IJavaElement insertPosition) {
43
	public AddCustomConstructorOperation(IType type, CodeGenerationSettings settings, IField[] selected, boolean save, IJavaElement insertPosition, int superIndex) {
48
		super();
44
		super();
49
		fType= type;
45
		fType= type;
50
		fDoSave= save;
46
		fDoSave= save;
Lines 52-57 Link Here
52
		fSettings= settings;
48
		fSettings= settings;
53
		fSelected= selected;
49
		fSelected= selected;
54
		fInsertPosition= insertPosition;
50
		fInsertPosition= insertPosition;
51
		try {
52
			fSuperMethod= new IMethod[1];
53
			fSuperMethod[0]= StubUtility.getOverridableConstructors(type)[superIndex];
54
		} catch (CoreException e) {
55
		}					
55
	}
56
	}
56
57
57
	/**
58
	/**
Lines 66-81 Link Here
66
			monitor.setTaskName(CodeGenerationMessages.getString("AddCustomConstructorOperation.description")); //$NON-NLS-1$
67
			monitor.setTaskName(CodeGenerationMessages.getString("AddCustomConstructorOperation.description")); //$NON-NLS-1$
67
			monitor.beginTask("", 3); //$NON-NLS-1$
68
			monitor.beginTask("", 3); //$NON-NLS-1$
68
			
69
			
69
			ITypeHierarchy hierarchy= fType.newSupertypeHierarchy(new SubProgressMonitor(monitor, 1));
70
			monitor.worked(1);
70
			monitor.worked(1);
71
			
71
			
72
			ImportsStructure imports= new ImportsStructure(fType.getCompilationUnit(), fSettings.importOrder, fSettings.importThreshold, true);
72
			ImportsStructure imports= new ImportsStructure(fType.getCompilationUnit(), fSettings.importOrder, fSettings.importThreshold, true);
73
			String defaultConstructor= getDefaultConstructor(fType, hierarchy, imports);
73
			ITypeHierarchy hierarchy= fType.newSupertypeHierarchy(new SubProgressMonitor(monitor, 1));
74
			int firstParenIndex= defaultConstructor.indexOf("(") + 1; //$NON-NLS-1$
74
			monitor.worked(1);
75
			
76
			String defaultConstructor= StubUtility.genOverrideStubs(fSuperMethod, fType, hierarchy, fSettings, imports)[0];					
77
			int closingParenIndex= defaultConstructor.indexOf(")"); //$NON-NLS-1$
75
			int closingBraceIndex= defaultConstructor.indexOf("}"); //$NON-NLS-1$
78
			int closingBraceIndex= defaultConstructor.indexOf("}"); //$NON-NLS-1$
76
			StringBuffer buf= new StringBuffer(defaultConstructor.substring(0, firstParenIndex));
79
80
			StringBuffer buf= new StringBuffer(defaultConstructor.substring(0, closingParenIndex));
77
			String[] params= new String[fSelected.length];
81
			String[] params= new String[fSelected.length];
78
			for (int i= 0; i < fSelected.length; i++) {
82
			for (int i= 0; i < fSelected.length; i++) {
83
				buf.append(", "); //$NON-NLS-1$
79
				buf.append(Signature.toString(fSelected[i].getTypeSignature()));				
84
				buf.append(Signature.toString(fSelected[i].getTypeSignature()));				
80
				buf.append(" "); //$NON-NLS-1$
85
				buf.append(" "); //$NON-NLS-1$
81
				IJavaProject project= fSelected[i].getJavaProject();
86
				IJavaProject project= fSelected[i].getJavaProject();
Lines 89-99 Link Here
89
94
90
				String paramName= StubUtility.guessArgumentName(project, accessName, new String[0]);
95
				String paramName= StubUtility.guessArgumentName(project, accessName, new String[0]);
91
				
96
				
92
				buf.append(params[i]= paramName);
97
				buf.append(params[i]= paramName);				
93
				if (i != fSelected.length - 1)
94
					buf.append(","); //$NON-NLS-1$					
95
			}
98
			}
96
			buf.append(defaultConstructor.substring(firstParenIndex, closingBraceIndex));
99
			
100
			buf.append(defaultConstructor.substring(closingParenIndex, closingBraceIndex));
101
			
97
			for (int i= 0; i < fSelected.length; i++) {
102
			for (int i= 0; i < fSelected.length; i++) {
98
				String fieldName= fSelected[i].getElementName();
103
				String fieldName= fSelected[i].getElementName();
99
				boolean isStatic= Flags.isStatic(fSelected[i].getFlags());
104
				boolean isStatic= Flags.isStatic(fSelected[i].getFlags());
Lines 123-146 Link Here
123
			monitor.done();
128
			monitor.done();
124
		}
129
		}
125
	}
130
	}
126
		
127
	private String getDefaultConstructor(IType type, ITypeHierarchy hierarchy, ImportsStructure imports)  {
128
		try {
129
			ICompilationUnit cu= type.getCompilationUnit();
130
			GenStubSettings genStubSettings= new GenStubSettings(fSettings);	
131
				
132
			IType objectType= type.getJavaProject().findType("java.lang.Object"); //$NON-NLS-1$
133
			IMethod curr= objectType.getMethod("Object", new String[0]);  //$NON-NLS-1$
134
			IMethod defaultConstructor= JavaModelUtil.findMethodImplementationInHierarchy(hierarchy, type, curr.getElementName(), curr.getParameterTypes(), curr.isConstructor());
135
			IMethod desc= JavaModelUtil.findMethodDeclarationInHierarchy(hierarchy, type, curr.getElementName(), curr.getParameterTypes(), curr.isConstructor());
136
			String str= StubUtility.genStub(cu, type.getElementName(), defaultConstructor, desc.getDeclaringType(), genStubSettings, imports);				
137
			return str;	
138
139
		} catch (JavaModelException e) {
140
		} catch (CoreException e) {
141
		}
142
		return null;
143
	}	
144
	
131
	
145
	/**
132
	/**
146
	 * Returns the created constructor. To be called after a sucessful run.
133
	 * Returns the created constructor. To be called after a sucessful run.
(-)ui/org/eclipse/jdt/internal/ui/actions/ActionMessages.properties (-6 / +7 lines)
Lines 120-139 Link Here
120
AddUnimplementedConstructorsAction.dialog.title=Add Constructors from Superclass
120
AddUnimplementedConstructorsAction.dialog.title=Add Constructors from Superclass
121
AddUnimplementedConstructorsAction.dialog.label=&Select constructors to implement:
121
AddUnimplementedConstructorsAction.dialog.label=&Select constructors to implement:
122
122
123
CreateNewConstructorAction.label=Create New &Constructor using Fields...
123
CreateNewConstructorAction.label=Create &Constructor using Fields...
124
CreateNewConstructorAction.description=Create New Constructor using fields
124
CreateNewConstructorAction.description=Create Constructor using fields
125
CreateNewConstructorAction.tooltip=Create New Constructor using Fields
125
CreateNewConstructorAction.tooltip=Create Constructor using Fields
126
CreateNewConstructorAction.error.title=Create New Constructor using Fields
126
CreateNewConstructorAction.error.title=Create Constructor using Fields
127
CreateNewConstructorAction.error.type_removed_in_editor=Input type has been removed in editor.
127
CreateNewConstructorAction.error.type_removed_in_editor=Input type has been removed in editor.
128
CreateNewConstructorAction.not_applicable=The operation is not applicable to the current selection.
128
CreateNewConstructorAction.not_applicable=The operation is not applicable to the current selection.
129
CreateNewConstructorAction.fields_selected={0} of {1} selected.
129
CreateNewConstructorAction.fields_selected={0} of {1} selected.
130
CreateNewConstructorAction.dialog.title=Create New Constructor using Fields
130
CreateNewConstructorAction.dialog.title=Create Constructor using Fields
131
CreateNewConstructorAction.dialog.label=&Select fields to initialize:
131
CreateNewConstructorAction.dialog.label=&Select fields to initialize:
132
CreateNewConstructorAction.interface_not_applicable=The Create New Constructor operation is not applicable to interfaces.
132
CreateNewConstructorAction.interface_not_applicable=The Create Constructor operation is not applicable to interfaces.
133
CreateNewConstructorAction.typeContainsNoFields.message=The type contains no fields.
133
CreateNewConstructorAction.typeContainsNoFields.message=The type contains no fields.
134
134
135
CreateNewConstructorSelectionDialog.up_button=&Up
135
CreateNewConstructorSelectionDialog.up_button=&Up
136
CreateNewConstructorSelectionDialog.down_button=&Down
136
CreateNewConstructorSelectionDialog.down_button=&Down
137
CreateNewConstructorSelectionDialog.sort_constructor_choices.label=&SuperClass Constructors:
137
138
138
AddJavaDocStubAction.label=Add &Javadoc Comment
139
AddJavaDocStubAction.label=Add &Javadoc Comment
139
AddJavaDocStubAction.description=Add a Javadoc comment stub to the member element
140
AddJavaDocStubAction.description=Add a Javadoc comment stub to the member element
(-)ui/org/eclipse/jdt/ui/actions/CreateNewConstructorAction.java (-6 / +64 lines)
Lines 17-24 Link Here
17
import org.eclipse.core.runtime.CoreException;
17
import org.eclipse.core.runtime.CoreException;
18
import org.eclipse.core.runtime.IStatus;
18
import org.eclipse.core.runtime.IStatus;
19
19
20
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.events.SelectionAdapter;
22
import org.eclipse.swt.events.SelectionEvent;
23
import org.eclipse.swt.layout.GridData;
20
import org.eclipse.swt.layout.GridLayout;
24
import org.eclipse.swt.layout.GridLayout;
25
import org.eclipse.swt.widgets.Combo;
21
import org.eclipse.swt.widgets.Composite;
26
import org.eclipse.swt.widgets.Composite;
27
import org.eclipse.swt.widgets.Label;
22
import org.eclipse.swt.widgets.Shell;
28
import org.eclipse.swt.widgets.Shell;
23
29
24
import org.eclipse.jface.dialogs.IDialogConstants;
30
import org.eclipse.jface.dialogs.IDialogConstants;
Lines 49-54 Link Here
49
55
50
import org.eclipse.jdt.internal.corext.codemanipulation.AddCustomConstructorOperation;
56
import org.eclipse.jdt.internal.corext.codemanipulation.AddCustomConstructorOperation;
51
import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings;
57
import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings;
58
import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility;
52
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
59
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
53
import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
60
import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
54
import org.eclipse.jdt.internal.ui.JavaPlugin;
61
import org.eclipse.jdt.internal.ui.JavaPlugin;
Lines 64-69 Link Here
64
import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext;
71
import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext;
65
import org.eclipse.jdt.internal.ui.util.ElementValidator;
72
import org.eclipse.jdt.internal.ui.util.ElementValidator;
66
import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
73
import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
74
import org.eclipse.jdt.internal.ui.viewsupport.JavaElementLabels;
67
75
68
76
69
public class CreateNewConstructorAction extends SelectionDispatchAction {
77
public class CreateNewConstructorAction extends SelectionDispatchAction {
Lines 134-140 Link Here
134
		try {
142
		try {
135
			IField[] selectedFields= getSelectedFields(selection);
143
			IField[] selectedFields= getSelectedFields(selection);
136
			// open an editor and work on a working copy
144
			// open an editor and work on a working copy
137
			IEditorPart editor= EditorUtility.openInEditor(getSelectedType(selection));	
145
			IEditorPart editor= null;
146
			if (selectedFields != null)
147
				editor= EditorUtility.openInEditor(selectedFields[0]);			
148
			else
149
				editor= EditorUtility.openInEditor(getSelectedType(selection).getCompilationUnit());	
138
			
150
			
139
			if (canRunOn(selectedFields)){
151
			if (canRunOn(selectedFields)){
140
				run((IType)EditorUtility.getWorkingCopy(selectedFields[0].getDeclaringType()), selectedFields, editor, false);
152
				run((IType)EditorUtility.getWorkingCopy(selectedFields[0].getDeclaringType()), selectedFields, editor, false);
Lines 318-325 Link Here
318
			settings.createComments= dialog.getGenerateComment();
330
			settings.createComments= dialog.getGenerateComment();
319
	
331
	
320
			IJavaElement elementPosition= dialog.getElementPosition();
332
			IJavaElement elementPosition= dialog.getElementPosition();
321
			AddCustomConstructorOperation op= new AddCustomConstructorOperation(type, settings, selected, false, elementPosition);
333
			int superIndex= dialog.getSuperIndex();
322
			
334
			AddCustomConstructorOperation op= new AddCustomConstructorOperation(type, settings, selected, false, elementPosition, superIndex);
335
323
			IRewriteTarget target= editor != null ? (IRewriteTarget) editor.getAdapter(IRewriteTarget.class) : null;
336
			IRewriteTarget target= editor != null ? (IRewriteTarget) editor.getAdapter(IRewriteTarget.class) : null;
324
			if (target != null) {
337
			if (target != null) {
325
				target.beginCompoundChange();		
338
				target.beginCompoundChange();		
Lines 488-499 Link Here
488
	
501
	
489
	private static class CreateNewConstructorSelectionDialog extends SourceActionDialog {
502
	private static class CreateNewConstructorSelectionDialog extends SourceActionDialog {
490
		private CreateNewConstructorContentProvider fContentProvider;
503
		private CreateNewConstructorContentProvider fContentProvider;
504
		private IType fType;
505
		private int fSuperIndex;
506
		
491
		private static final int UP_BUTTON= IDialogConstants.CLIENT_ID + 1;
507
		private static final int UP_BUTTON= IDialogConstants.CLIENT_ID + 1;
492
		private static final int DOWN_BUTTON= IDialogConstants.CLIENT_ID + 2;		
508
		private static final int DOWN_BUTTON= IDialogConstants.CLIENT_ID + 2;		
493
		
509
		
494
		public CreateNewConstructorSelectionDialog(Shell parent, ILabelProvider labelProvider, CreateNewConstructorContentProvider contentProvider, CompilationUnitEditor editor, IType type) {
510
		public CreateNewConstructorSelectionDialog(Shell parent, ILabelProvider labelProvider, CreateNewConstructorContentProvider contentProvider, CompilationUnitEditor editor, IType type) {
495
			super(parent, labelProvider, contentProvider, editor, type);
511
			super(parent, labelProvider, contentProvider, editor, type);
496
			fContentProvider= contentProvider;
512
			fContentProvider= contentProvider;
513
			fType= type;
497
		}	
514
		}	
498
		
515
		
499
		protected Composite createSelectionButtons(Composite composite) {
516
		protected Composite createSelectionButtons(Composite composite) {
Lines 533-545 Link Here
533
		}
550
		}
534
		
551
		
535
		private List getElementList() {
552
		private List getElementList() {
536
			Object elements[]= getTreeViewer().getCheckedElements();
553
			IStructuredSelection selection= (IStructuredSelection) getTreeViewer().getSelection();
554
			List elements= selection.toList();						
537
			ArrayList elementList= new ArrayList();
555
			ArrayList elementList= new ArrayList();
538
			for(int i = 0; i < elements.length; i++)
556
539
				elementList.add(elements[i]);			
557
			for (int i= 0; i < elements.size(); i++) {
558
				elementList.add(elements.get(i));		
559
			}
540
			return elementList;
560
			return elementList;
541
		}		
561
		}		
562
563
		protected Composite createEntryPtCombo(Composite composite) {
564
			Composite entryComposite= super.createEntryPtCombo(composite);			
565
			addSuperClassConstructorChoices(entryComposite);
566
			
567
			return entryComposite;						
568
		}
569
		
570
		private Composite addSuperClassConstructorChoices(Composite composite) {
571
			try {
572
				Label label= new Label(composite, SWT.NONE);
573
				label.setText(ActionMessages.getString("CreateNewConstructorSelectionDialog.sort_constructor_choices.label")); //$NON-NLS-1$
574
				GridData gd= new GridData(GridData.FILL_BOTH);
575
				label.setLayoutData(gd);
542
				
576
				
577
				final Combo combo= new Combo(composite, SWT.READ_ONLY);
578
				IMethod[] constructorMethods= StubUtility.getOverridableConstructors(fType);					
579
				
580
				for (int i= 0; i < constructorMethods.length; i++) {
581
					combo.add(JavaElementLabels.getElementLabel(constructorMethods[i], JavaElementLabels.M_PARAMETER_TYPES));
582
				}
583
				// TODO: Can we be a little more intelligent about guessing the super() ?
584
				combo.setText(combo.getItem(0));
585
				combo.setLayoutData(new GridData(GridData.FILL_BOTH));
586
				combo.addSelectionListener(new SelectionAdapter(){
587
					public void widgetSelected(SelectionEvent e) {
588
						fSuperIndex= combo.getSelectionIndex();
589
					}
590
				});	
591
592
			} catch (CoreException e) {
593
			}
594
			return composite;
595
		}		
596
				
597
		public int getSuperIndex() {
598
			return fSuperIndex;
599
		}
600
543
	}
601
	}
544
	
602
	
545
}
603
}

Return to bug 39188