-
Notifications
You must be signed in to change notification settings - Fork 86
/
GenericRefactoringHandleTransplanter.java
182 lines (146 loc) · 6.49 KB
/
GenericRefactoringHandleTransplanter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*******************************************************************************
* Copyright (c) 2006, 2008 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.corext.refactoring.rename;
import org.eclipse.jdt.core.IAnnotatable;
import org.eclipse.jdt.core.IAnnotation;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IImportContainer;
import org.eclipse.jdt.core.IImportDeclaration;
import org.eclipse.jdt.core.IInitializer;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaModel;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.ILocalVariable;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IOrdinaryClassFile;
import org.eclipse.jdt.core.IPackageDeclaration;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeParameter;
public class GenericRefactoringHandleTransplanter {
public final IJavaElement transplantHandle(IJavaElement element) {
IJavaElement parent= element.getParent();
if (parent != null)
parent= transplantHandle(parent); // recursive
switch (element.getElementType()) {
case IJavaElement.JAVA_MODEL:
return transplantHandle((IJavaModel) element);
case IJavaElement.JAVA_PROJECT:
return transplantHandle((IJavaProject) element);
case IJavaElement.PACKAGE_FRAGMENT_ROOT:
return element;
case IJavaElement.PACKAGE_FRAGMENT:
return transplantHandle((IPackageFragmentRoot) parent, (IPackageFragment) element);
case IJavaElement.COMPILATION_UNIT:
return transplantHandle((IPackageFragment) parent, (ICompilationUnit) element);
case IJavaElement.CLASS_FILE:
return transplantHandle((IPackageFragment) parent, (IClassFile) element);
case IJavaElement.TYPE:
return transplantHandle(parent, (IType) element);
case IJavaElement.FIELD:
return transplantHandle((IType) parent, (IField) element);
case IJavaElement.METHOD:
return transplantHandle((IType) parent, (IMethod) element);
case IJavaElement.INITIALIZER:
return transplantHandle((IType) parent, (IInitializer) element);
case IJavaElement.PACKAGE_DECLARATION:
return transplantHandle((ICompilationUnit) parent, (IPackageDeclaration) element);
case IJavaElement.IMPORT_CONTAINER:
return transplantHandle((ICompilationUnit) parent);
case IJavaElement.IMPORT_DECLARATION:
return transplantHandle((IImportContainer) parent, (IImportDeclaration) element);
case IJavaElement.LOCAL_VARIABLE:
return transplantHandle((ILocalVariable) element);
case IJavaElement.TYPE_PARAMETER:
return transplantHandle((IMember) parent, (ITypeParameter) element);
case IJavaElement.ANNOTATION:
return transplantHandle((IAnnotatable) parent, (IAnnotation) element);
default:
throw new IllegalArgumentException(element.toString());
}
}
protected IJavaModel transplantHandle(IJavaModel element) {
return element;
}
protected IJavaProject transplantHandle(IJavaProject element) {
return element;
}
protected IPackageFragment transplantHandle(IPackageFragmentRoot parent, IPackageFragment element) {
return parent.getPackageFragment(element.getElementName());
}
protected ICompilationUnit transplantHandle(IPackageFragment parent, ICompilationUnit element) {
return parent.getCompilationUnit(element.getElementName());
}
protected IClassFile transplantHandle(IPackageFragment parent, IClassFile element) {
return parent.getClassFile(element.getElementName());
}
protected IType transplantHandle(IJavaElement parent, IType element) {
switch (parent.getElementType()) {
case IJavaElement.COMPILATION_UNIT:
return ((ICompilationUnit) parent).getType(element.getElementName());
case IJavaElement.CLASS_FILE:
if (parent instanceof IOrdinaryClassFile)
return ((IOrdinaryClassFile) parent).getType();
throw new IllegalStateException(element.toString());
case IJavaElement.METHOD:
return ((IMethod) parent).getType(element.getElementName(), element.getOccurrenceCount());
case IJavaElement.FIELD:
return ((IField) parent).getType(element.getElementName(), element.getOccurrenceCount());
case IJavaElement.INITIALIZER:
return ((IInitializer) parent).getType(element.getElementName(), element.getOccurrenceCount());
case IJavaElement.TYPE:
return ((IType) parent).getType(element.getElementName(), element.getOccurrenceCount());
default:
throw new IllegalStateException(element.toString());
}
}
protected IField transplantHandle(IType parent, IField element) {
return parent.getField(element.getElementName());
}
protected IMethod transplantHandle(IType parent, IMethod element) {
return parent.getMethod(element.getElementName(), element.getParameterTypes());
}
protected IInitializer transplantHandle(IType parent, IInitializer element) {
return parent.getInitializer(element.getOccurrenceCount());
}
protected IPackageDeclaration transplantHandle(ICompilationUnit parent, IPackageDeclaration element) {
return parent.getPackageDeclaration(element.getElementName());
}
protected IImportContainer transplantHandle(ICompilationUnit parent) {
return parent.getImportContainer();
}
protected IImportDeclaration transplantHandle(IImportContainer parent, IImportDeclaration element) {
return parent.getImport(element.getElementName());
}
protected ILocalVariable transplantHandle(ILocalVariable element) {
return element; // can't get from parent!
}
protected IAnnotation transplantHandle(IAnnotatable parent, IAnnotation element) {
return parent.getAnnotation(element.getElementName());
}
protected ITypeParameter transplantHandle(IMember parent, ITypeParameter element) {
switch (parent.getElementType()) {
case IJavaElement.TYPE:
return ((IType) parent).getTypeParameter(element.getElementName());
case IJavaElement.METHOD:
return ((IMethod) parent).getTypeParameter(element.getElementName());
default:
throw new IllegalStateException(element.toString());
}
}
}