]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 IBM Corporation and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * IBM Corporation - initial API and implementation | |
10 | * Mateusz Wenus <mateusz.wenus@gmail.com> - [override method] generate in declaration order [code generation] - https://bugs.eclipse.org/bugs/show_bug.cgi?id=140971 | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.jdt.internal.corext.fix; | |
14 | ||
15 | import java.util.Arrays; | |
16 | ||
17 | import org.eclipse.core.runtime.Assert; | |
18 | import org.eclipse.core.runtime.CoreException; | |
19 | ||
20 | import org.eclipse.jdt.core.ICompilationUnit; | |
21 | import org.eclipse.jdt.core.dom.ASTNode; | |
22 | import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; | |
23 | import org.eclipse.jdt.core.dom.AnonymousClassDeclaration; | |
24 | import org.eclipse.jdt.core.dom.CompilationUnit; | |
25 | import org.eclipse.jdt.core.dom.EnumConstantDeclaration; | |
26 | import org.eclipse.jdt.core.dom.EnumDeclaration; | |
27 | import org.eclipse.jdt.core.dom.IMethodBinding; | |
28 | import org.eclipse.jdt.core.dom.ITypeBinding; | |
29 | import org.eclipse.jdt.core.dom.IVariableBinding; | |
30 | import org.eclipse.jdt.core.dom.MethodDeclaration; | |
31 | import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; | |
32 | import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; | |
33 | import org.eclipse.jdt.core.dom.rewrite.ListRewrite; | |
34 | import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext; | |
35 | ||
36 | import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings; | |
37 | import org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext; | |
38 | import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility2; | |
39 | import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFix.CompilationUnitRewriteOperation; | |
40 | import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; | |
41 | import org.eclipse.jdt.internal.corext.util.Messages; | |
42 | import org.eclipse.jdt.internal.corext.util.MethodsSourcePositionComparator; | |
43 | ||
44 | import org.eclipse.jdt.ui.JavaElementLabels; | |
45 | ||
46 | import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings; | |
47 | import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages; | |
48 | import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider; | |
49 | ||
50 | public class AddUnimplementedMethodsOperation extends CompilationUnitRewriteOperation { | |
51 | ||
52 | private ASTNode fTypeNode; | |
53 | ||
54 | /** | |
55 | * Create a {@link AddUnimplementedMethodsOperation} | |
56 | * @param typeNode must be one of the following types: | |
57 | * <ul><li>AnonymousClassDeclaration</li> | |
58 | * <li>AbstractTypeDeclaration</li> | |
59 | * <li>EnumConstantDeclaration</li></ul> | |
60 | */ | |
61 | public AddUnimplementedMethodsOperation(ASTNode typeNode) { | |
62 | fTypeNode= typeNode; | |
63 | } | |
64 | ||
65 | /** | |
66 | * {@inheritDoc} | |
67 | */ | |
68 | @Override | |
69 | public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException { | |
70 | IMethodBinding[] unimplementedMethods= getUnimplementedMethods(fTypeNode); | |
71 | if (unimplementedMethods.length == 0) | |
72 | return; | |
73 | ||
74 | ImportRewriteContext context= new ContextSensitiveImportRewriteContext((CompilationUnit) fTypeNode.getRoot(), fTypeNode.getStartPosition(), cuRewrite.getImportRewrite()); | |
75 | ASTRewrite rewrite= cuRewrite.getASTRewrite(); | |
76 | ICompilationUnit unit= cuRewrite.getCu(); | |
77 | CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(unit.getJavaProject()); | |
78 | ||
79 | ListRewrite listRewrite; | |
80 | ||
81 | if (fTypeNode instanceof AnonymousClassDeclaration) { | |
82 | AnonymousClassDeclaration decl= (AnonymousClassDeclaration) fTypeNode; | |
83 | listRewrite= rewrite.getListRewrite(decl, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY); | |
84 | settings.generated_843113507756375317(this); | |
85 | } else if (fTypeNode instanceof AbstractTypeDeclaration) { | |
86 | AbstractTypeDeclaration decl= (AbstractTypeDeclaration) fTypeNode; | |
87 | listRewrite= rewrite.getListRewrite(decl, decl.getBodyDeclarationsProperty()); | |
88 | } else if (fTypeNode instanceof EnumConstantDeclaration) { | |
89 | EnumConstantDeclaration enumConstantDeclaration= (EnumConstantDeclaration) fTypeNode; | |
90 | AnonymousClassDeclaration anonymousClassDeclaration= enumConstantDeclaration.getAnonymousClassDeclaration(); | |
91 | if (anonymousClassDeclaration == null) { | |
92 | anonymousClassDeclaration= rewrite.getAST().newAnonymousClassDeclaration(); | |
93 | rewrite.set(enumConstantDeclaration, EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY, anonymousClassDeclaration, createTextEditGroup( | |
94 | CorrectionMessages.AddUnimplementedMethodsOperation_AddMissingMethod_group, cuRewrite)); | |
95 | } | |
96 | listRewrite= rewrite.getListRewrite(anonymousClassDeclaration, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY); | |
97 | settings.createComments= false; | |
98 | } else { | |
99 | Assert.isTrue(false, "Unknown type node"); //$NON-NLS-1$ | |
100 | return; | |
101 | } | |
102 | ||
103 | ImportRewrite imports= cuRewrite.getImportRewrite(); | |
104 | ||
105 | for (int i= 0; i < unimplementedMethods.length; i++) { | |
106 | IMethodBinding curr= unimplementedMethods[i]; | |
107 | MethodDeclaration newMethodDecl= StubUtility2.createImplementationStub(unit, rewrite, imports, context, curr, curr.getDeclaringClass().getName(), settings, false); | |
108 | listRewrite.insertLast(newMethodDecl, createTextEditGroup(CorrectionMessages.AddUnimplementedMethodsOperation_AddMissingMethod_group, cuRewrite)); | |
109 | } | |
110 | } | |
111 | ||
112 | /** | |
113 | * {@inheritDoc} | |
114 | */ | |
115 | @Override | |
116 | public String getAdditionalInfo() { | |
117 | if (fTypeNode instanceof EnumDeclaration) | |
118 | return CorrectionMessages.UnimplementedMethodsCorrectionProposal_enum_info; | |
119 | ||
120 | IMethodBinding[] methodsToOverride= getMethodsToImplement(); | |
121 | StringBuffer buf= new StringBuffer(); | |
122 | buf.append("<b>"); //$NON-NLS-1$ | |
123 | if (methodsToOverride.length == 1) { | |
124 | buf.append(CorrectionMessages.UnimplementedMethodsCorrectionProposal_info_singular); | |
125 | } else { | |
126 | buf.append(Messages.format(CorrectionMessages.UnimplementedMethodsCorrectionProposal_info_plural, String.valueOf(methodsToOverride.length))); | |
127 | } | |
128 | buf.append("</b><ul>"); //$NON-NLS-1$ | |
129 | for (int i= 0; i < methodsToOverride.length; i++) { | |
130 | buf.append("<li>"); //$NON-NLS-1$ | |
131 | buf.append(BindingLabelProvider.getBindingLabel(methodsToOverride[i], JavaElementLabels.ALL_FULLY_QUALIFIED)); | |
132 | buf.append("</li>"); //$NON-NLS-1$ | |
133 | } | |
134 | buf.append("</ul>"); //$NON-NLS-1$ | |
135 | return buf.toString(); | |
136 | } | |
137 | ||
138 | public IMethodBinding[] getMethodsToImplement() { | |
139 | return getUnimplementedMethods(fTypeNode); | |
140 | } | |
141 | ||
142 | private IMethodBinding[] getUnimplementedMethods(ASTNode typeNode) { | |
143 | ITypeBinding binding= null; | |
144 | boolean implementAbstractsOfInput= false; | |
145 | if (typeNode instanceof AnonymousClassDeclaration) { | |
146 | AnonymousClassDeclaration decl= (AnonymousClassDeclaration) typeNode; | |
147 | binding= decl.resolveBinding(); | |
148 | } else if (typeNode instanceof AbstractTypeDeclaration) { | |
149 | AbstractTypeDeclaration decl= (AbstractTypeDeclaration) typeNode; | |
150 | binding= decl.resolveBinding(); | |
151 | } else if (typeNode instanceof EnumConstantDeclaration) { | |
152 | EnumConstantDeclaration enumConstantDeclaration= (EnumConstantDeclaration) typeNode; | |
153 | if (enumConstantDeclaration.getAnonymousClassDeclaration() != null) { | |
154 | binding= enumConstantDeclaration.getAnonymousClassDeclaration().resolveBinding(); | |
155 | } else { | |
156 | IVariableBinding varBinding= enumConstantDeclaration.resolveVariable(); | |
157 | if (varBinding != null) { | |
158 | binding= varBinding.getDeclaringClass(); | |
159 | implementAbstractsOfInput= true; | |
160 | } | |
161 | } | |
162 | } | |
163 | if (binding == null) | |
164 | return new IMethodBinding[0]; | |
165 | ||
166 | IMethodBinding[] unimplementedMethods= StubUtility2.getUnimplementedMethods(binding, implementAbstractsOfInput); | |
167 | Arrays.sort(unimplementedMethods, new MethodsSourcePositionComparator(binding)); | |
168 | return unimplementedMethods; | |
169 | } | |
170 | } |