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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.refactoring;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Comparator;
16 import java.util.Iterator;
17 import java.util.List;
20 import org.eclipse.core.runtime.Assert;
21 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.jdt.core.Flags;
24 import org.eclipse.jdt.core.IBuffer;
25 import org.eclipse.jdt.core.ICompilationUnit;
26 import org.eclipse.jdt.core.IField;
27 import org.eclipse.jdt.core.IImportContainer;
28 import org.eclipse.jdt.core.IJavaElement;
29 import org.eclipse.jdt.core.ISourceReference;
30 import org.eclipse.jdt.core.JavaModelException;
31 import org.eclipse.jdt.core.dom.ASTNode;
32 import org.eclipse.jdt.core.dom.ASTParser;
33 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
34 import org.eclipse.jdt.core.dom.CompilationUnit;
35 import org.eclipse.jdt.core.dom.FieldDeclaration;
36 import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
37 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
39 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgUtils;
40 import org.eclipse.jdt.internal.corext.refactoring.structure.ASTNodeSearchUtil;
41 import org.eclipse.jdt.internal.corext.util.Strings;
43 import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider;
44 import org.eclipse.jdt.internal.ui.refactoring.reorg.PasteAction.TypedSourcePaster.PasteTypedSourcesRefactoring;
47 * A tuple used to keep source of an element and its type.
49 * @see org.eclipse.jdt.core.ISourceReference
51 public class TypedSource {
53 private static class SourceTuple {
55 private SourceTuple(ICompilationUnit unit) {
58 private ICompilationUnit unit;
59 private CompilationUnit node;
62 private final String fSource;
63 private final int fType;
65 private TypedSource(String source, int type){
66 Assert.isNotNull(source);
67 Assert.isTrue(canCreateForType(type));
72 public static TypedSource create(String source, int type) {
73 if (source == null || ! canCreateForType(type))
75 return new TypedSource(source, type);
78 public String getSource() {
82 public int getType() {
87 * @see java.lang.Object#equals(java.lang.Object)
90 public boolean equals(Object other) {
91 if (! (other instanceof TypedSource))
94 TypedSource ts= (TypedSource)other;
95 return ts.generated_7516993050506477390(this);
99 * @see java.lang.Object#hashCode()
102 public int hashCode() {
103 return getSource().hashCode() ^ (97 * getType());
106 public void generated_639287168146208855(CompilationUnit cuNode, ASTRewrite rewrite, PasteTypedSourcesRefactoring pastetypedsourcesrefactoring) throws JavaModelException {
107 final ASTNode destination= pastetypedsourcesrefactoring.getDestinationNodeForSourceElement(pastetypedsourcesrefactoring.fDestination, getType(), cuNode);
108 if (destination != null) {
109 if (destination instanceof CompilationUnit)
110 PasteTypedSourcesRefactoring.insertToCu(rewrite, pastetypedsourcesrefactoring.createNewNodeToInsertToCu(this, rewrite), (CompilationUnit) destination);
111 else if (destination instanceof AbstractTypeDeclaration)
112 PasteTypedSourcesRefactoring.insertToType(rewrite, pastetypedsourcesrefactoring.createNewNodeToInsertToType(this, rewrite), (AbstractTypeDeclaration) destination);
116 public ASTNode generated_2915041481128989772(ASTRewrite rewrite) {
118 case IJavaElement.TYPE:
119 return rewrite.createStringPlaceholder(getSource(), ASTNode.TYPE_DECLARATION);
120 case IJavaElement.PACKAGE_DECLARATION:
121 return rewrite.createStringPlaceholder(getSource(), ASTNode.PACKAGE_DECLARATION);
122 case IJavaElement.IMPORT_DECLARATION:
123 return rewrite.createStringPlaceholder(getSource(), ASTNode.IMPORT_DECLARATION);
124 default: Assert.isTrue(false, String.valueOf(getType()));
129 public ASTNode generated_1676103505968220226(ASTRewrite rewrite) {
131 case IJavaElement.TYPE:
132 return rewrite.createStringPlaceholder(getSource(), ASTNode.TYPE_DECLARATION);
133 case IJavaElement.METHOD:
134 return rewrite.createStringPlaceholder(getSource(), ASTNode.METHOD_DECLARATION);
135 case IJavaElement.FIELD:
136 return rewrite.createStringPlaceholder(getSource(), ASTNode.FIELD_DECLARATION);
137 case IJavaElement.INITIALIZER:
138 return rewrite.createStringPlaceholder(getSource(), ASTNode.INITIALIZER);
139 default: Assert.isTrue(false);
144 public boolean generated_7516993050506477390(TypedSource typedsource) {
145 return getSource().equals(typedsource.getSource()) && getType() == typedsource.getType();
148 private static boolean canCreateForType(int type){
149 return type == IJavaElement.FIELD
150 || type == IJavaElement.TYPE
151 || type == IJavaElement.IMPORT_CONTAINER
152 || type == IJavaElement.IMPORT_DECLARATION
153 || type == IJavaElement.INITIALIZER
154 || type == IJavaElement.METHOD
155 || type == IJavaElement.PACKAGE_DECLARATION;
159 public static void sortByType(TypedSource[] typedSources){
160 Arrays.sort(typedSources, createTypeComparator());
163 public static Comparator<TypedSource> createTypeComparator() {
164 return new Comparator<TypedSource>(){
165 public int compare(TypedSource arg0, TypedSource arg1) {
166 return arg0.getType() - arg1.getType();
170 public static TypedSource[] createTypedSources(IJavaElement[] javaElements) throws CoreException {
171 //Map<ICompilationUnit, List<IJavaElement>>
172 Map<ICompilationUnit, List<IJavaElement>> grouped= ReorgUtils.groupByCompilationUnit(Arrays.asList(javaElements));
173 List<TypedSource> result= new ArrayList<TypedSource>(javaElements.length);
174 for (Iterator<ICompilationUnit> iter= grouped.keySet().iterator(); iter.hasNext();) {
175 ICompilationUnit cu= iter.next();
176 for (Iterator<IJavaElement> iterator= grouped.get(cu).iterator(); iterator.hasNext();) {
177 SourceTuple tuple= new SourceTuple(cu);
178 TypedSource[] ts= createTypedSources(iterator.next(), tuple);
180 result.addAll(Arrays.asList(ts));
183 return result.toArray(new TypedSource[result.size()]);
186 private static TypedSource[] createTypedSources(IJavaElement elem, SourceTuple tuple) throws CoreException {
187 if (! ReorgUtils.isInsideCompilationUnit(elem))
189 if (elem.getElementType() == IJavaElement.IMPORT_CONTAINER)
190 return createTypedSourcesForImportContainer(tuple, (IImportContainer)elem);
191 else if (elem.getElementType() == IJavaElement.FIELD)
192 return new TypedSource[] {create(getFieldSource((IField)elem, tuple), elem.getElementType())};
193 return new TypedSource[] {create(getSourceOfDeclararationNode(elem, tuple.unit), elem.getElementType())};
196 private static TypedSource[] createTypedSourcesForImportContainer(SourceTuple tuple, IImportContainer container) throws JavaModelException, CoreException {
197 IJavaElement[] imports= container.getChildren();
198 List<TypedSource> result= new ArrayList<TypedSource>(imports.length);
199 for (int i= 0; i < imports.length; i++) {
200 result.addAll(Arrays.asList(createTypedSources(imports[i], tuple)));
202 return result.toArray(new TypedSource[result.size()]);
205 private static String getFieldSource(IField field, SourceTuple tuple) throws CoreException {
206 if (Flags.isEnum(field.getFlags())) {
207 String source= field.getSource();
211 if (tuple.node == null) {
212 ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
213 parser.setSource(tuple.unit);
214 tuple.node= (CompilationUnit) parser.createAST(null);
216 FieldDeclaration declaration= ASTNodeSearchUtil.getFieldDeclarationNode(field, tuple.node);
217 if (declaration.fragments().size() == 1)
218 return getSourceOfDeclararationNode(field, tuple.unit);
219 VariableDeclarationFragment declarationFragment= ASTNodeSearchUtil.getFieldDeclarationFragmentNode(field, tuple.node);
220 IBuffer buffer= tuple.unit.getBuffer();
221 StringBuffer buff= new StringBuffer();
222 buff.append(buffer.getText(declaration.getStartPosition(), ((ASTNode) declaration.fragments().get(0)).getStartPosition() - declaration.getStartPosition()));
223 buff.append(buffer.getText(declarationFragment.getStartPosition(), declarationFragment.getLength()));
224 buff.append(";"); //$NON-NLS-1$
225 return buff.toString();
227 return ""; //$NON-NLS-1$
230 private static String getSourceOfDeclararationNode(IJavaElement elem, ICompilationUnit cu) throws JavaModelException, CoreException {
231 Assert.isTrue(elem.getElementType() != IJavaElement.IMPORT_CONTAINER);
232 if (elem instanceof ISourceReference) {
233 ISourceReference reference= (ISourceReference) elem;
234 String source= reference.getSource();
236 return Strings.trimIndentation(source, cu.getJavaProject(), false);
238 return ""; //$NON-NLS-1$