Skip to main content
summaryrefslogtreecommitdiffstats
blob: 12a95ccdf1b89c5227ca530d87a0ecf56a2f8093 (plain) (blame)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*******************************************************************************
 * Copyright (c) 2005, 2007 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.core.internal.jdtutility;

import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IImportDeclaration;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.Signature;
import org.eclipse.jdt.core.compiler.CategorizedProblem;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.internal.codeassist.ISelectionRequestor;
import org.eclipse.jdt.internal.codeassist.SelectionEngine;
import org.eclipse.jdt.internal.core.DefaultWorkingCopyOwner;
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jdt.internal.core.SearchableEnvironment;

public class JDTTools {

	/**
	 * add a "normal" import, as opposed to a "static" import
	 */
	public static IImportDeclaration addImport(ICompilationUnit compilationUnit, String importElement) {
		return addImport(compilationUnit, importElement, Flags.AccDefault);
	}

	/**
	 * this doesn't work yet... see eclipse bugzilla 143684
	 */
	public static IImportDeclaration addStaticImport(ICompilationUnit compilationUnit, String importElement) {
		return addImport(compilationUnit, importElement, Flags.AccStatic);
	}

	public static IImportDeclaration addImport(ICompilationUnit compilationUnit, String importElement, int flags) {
		try {
			return compilationUnit.createImport(importElement, null, flags, null);  // null = place at end of import list; null = no progress monitor
		} catch (JavaModelException ex) {
			throw new RuntimeException(ex);
		}
	}

	/**
	 * Build an AST for the specified member's compilation unit or class file.
	 */
	public static CompilationUnit createASTRoot(IMember member) {
		return (member.isBinary()) ?
			createASTRoot(member.getClassFile())  // the class file must have a source attachment
		:
			createASTRoot(member.getCompilationUnit());
	}
	
	public static CompilationUnit createASTRoot(IClassFile classFile) {
		ASTParser parser = ASTParser.newParser(AST.JLS3);
		parser.setSource(classFile);
		return (CompilationUnit) parser.createAST(null);
		
	}
	
	public static CompilationUnit createASTRoot(ICompilationUnit compilationUnit) {
		ASTParser parser = ASTParser.newParser(AST.JLS3);
		parser.setSource(compilationUnit);
		return (CompilationUnit) parser.createAST(null);
	}

	/**
	 * Resolve the specified signature in the scope of the specified jdt type.
	 * Return the fully-qualified type name or return null if it cannot be
	 * resolved unambiguously.
	 */
	public static String resolveSignature(String signature, IType type) {
		String elementSignature = Signature.getElementType(signature);
		if (signatureIsPrimitive(elementSignature)) {
			return Signature.toString(signature);  // no need to resolve primitives
		}
		String elementTypeName = Signature.toString(elementSignature);
		elementTypeName = resolve(elementTypeName, type);
		if (elementTypeName == null) {
			return null;  // unable to resolve type
		}
		int arrayCount = Signature.getArrayCount(signature);
		if (arrayCount == 0) {
			return elementTypeName;
		}
		StringBuffer sb = new StringBuffer(elementTypeName.length() + 2*arrayCount);
		sb.append(elementTypeName);
		for (int i = arrayCount; i-- > 0; ) {
			sb.append('[').append(']');
		}
		return sb.toString();
	}

	/**
	 * Resolve the specified type name in the scope of the specified jdt type.
	 * Return the fully-qualified type name or return null if it cannot be
	 * resolved unambiguously.
	 */
	public static String resolve(String typeName, IType type) {
		try {
			return resolve_(typeName, type);
		} catch (JavaModelException ex) {
			throw new RuntimeException(ex);
		}
	}

	private static String resolve_(String typeName, IType type) throws JavaModelException {
		String[][] resolvedTypes = type.resolveType(typeName);
		// if more than one resolved type is returned, the type name is ambiguous
		if (resolvedTypes == null) {
			return null;
		}
		if (resolvedTypes.length > 1) {
			return null;
		}
		return resolvedTypes[0][0] + "." + resolvedTypes[0][1];
	}

	public static boolean signatureIsPrimitive(String signature) {
		return Signature.getTypeSignatureKind(signature) == Signature.BASE_TYPE_SIGNATURE;
	}

	public static String resolveEnum(ICompilationUnit sourceUnit, Expression enumExpression) {
		return resolveEnum(sourceUnit, enumExpression.getStartPosition(), enumExpression.getStartPosition() + enumExpression.getLength() - 1);
	}

	public static String resolveEnum(ICompilationUnit sourceUnit, int enumSourceStart, int enumSourceEnd) {
		try {
			return resolveEnum_(sourceUnit, enumSourceStart, enumSourceEnd);
		} catch (JavaModelException ex) {
			throw new RuntimeException(ex);
		}
	}

	private static String resolveEnum_(ICompilationUnit sourceUnit, int enumSourceStart, int enumSourceEnd) throws JavaModelException {
		String[][] resolvedEnums = resolveField_((org.eclipse.jdt.internal.core.CompilationUnit) sourceUnit, enumSourceStart, enumSourceEnd);
		// if more than one resolved enum is returned, the enum name is ambiguous
		if (resolvedEnums == null) {
			return null;
		}
		if (resolvedEnums.length > 1) {
			return null;
		}
		return resolvedEnums[0][0] + "." + resolvedEnums[0][1] + "." + resolvedEnums[0][2];
	}

	// code lifted from SourceType.resolveType(String, WorkingCopyOwner)
	private static String[][] resolveField_(org.eclipse.jdt.internal.core.CompilationUnit sourceUnit, int selectionSourceStart, int selectionSourceEnd) throws JavaModelException {
		class TypeResolveRequestor implements ISelectionRequestor {
			String[][] answers = null;
			public void acceptType(char[] packageName, char[] tName, int modifiers, boolean isDeclaration, char[] uniqueKey, int start, int end) {
				// ignore
			}
			public void acceptError(CategorizedProblem error) {
				// ignore
			}
			public void acceptField(char[] declaringTypePackageName, char[] declaringTypeName, char[] fieldName, boolean isDeclaration, char[] uniqueKey, int start, int end) {
				String[] answer = new String[]  {new String(declaringTypePackageName), new String(declaringTypeName), new String(fieldName) };
				if (this.answers == null) {
					this.answers = new String[][]{ answer };
				} else {
					int len = this.answers.length;
					System.arraycopy(this.answers, 0, this.answers = new String[len+1][], 0, len);
					this.answers[len] = answer;
				}
			}
			public void acceptMethod(char[] declaringTypePackageName, char[] declaringTypeName, String enclosingDeclaringTypeSignature, char[] selector, char[][] parameterPackageNames, char[][] parameterTypeNames, String[] parameterSignatures, char[][] typeParameterNames, char[][][] typeParameterBoundNames, boolean isConstructor, boolean isDeclaration, char[] uniqueKey, int start, int end) {
				// ignore
			}
			public void acceptPackage(char[] packageName){
				// ignore
			}
			public void acceptTypeParameter(char[] declaringTypePackageName, char[] declaringTypeName, char[] typeParameterName, boolean isDeclaration, int start, int end) {
				// ignore
			}
			public void acceptMethodTypeParameter(char[] declaringTypePackageName, char[] declaringTypeName, char[] selector, int selectorStart, int selcetorEnd, char[] typeParameterName, boolean isDeclaration, int start, int end) {
				// ignore
			}
	
		}
		TypeResolveRequestor requestor = new TypeResolveRequestor();
		JavaProject project = (JavaProject) sourceUnit.getJavaProject();
		SearchableEnvironment environment = project.newSearchableNameEnvironment(DefaultWorkingCopyOwner.PRIMARY);
	
		SelectionEngine engine = new SelectionEngine(environment, requestor, project.getOptions(true));
			
		engine.select(sourceUnit, selectionSourceStart, selectionSourceEnd);
		return requestor.answers;
	}
}

Back to the top