Skip to main content
summaryrefslogtreecommitdiffstats
blob: cf1b284da5c97af7c00cf08dfbd36c9c8b7e5672 (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
/*******************************************************************************
 * 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.IClassFile;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
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.core.dom.IBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jpt.core.internal.JptCorePlugin;

public class JDTTools {

	// TODO get rid of the "lightweight" methods after reworking how
	// ValidationMessages determine line numbers
	/**
	 * Build an AST for the specified member's compilation unit or
	 * (source-attached) class file. Build the AST without its bindings
	 * resolved.
	 */
	public static CompilationUnit buildLightweightASTRoot(IMember member) {
		return buildASTRoot(member, false);
	}

	/**
	 * Build an AST for the specified member's compilation unit or
	 * (source-attached) class file. Build the AST with its bindings
	 * resolved (and the resultant performance hit).
	 */
	public static CompilationUnit buildASTRoot(IMember member) {
		return buildASTRoot(member, true);
	}

	/**
	 * Build an AST for the specified member's compilation unit or
	 * (source-attached) class file.
	 */
	private static CompilationUnit buildASTRoot(IMember member, boolean resolveBindings) {
		return (member.isBinary()) ?
			buildASTRoot(member.getClassFile(), resolveBindings)  // the class file must have a source attachment
		:
			buildASTRoot(member.getCompilationUnit(), resolveBindings);
	}
	
	public static CompilationUnit buildASTRoot(IClassFile classFile) {
		return buildASTRoot(classFile, true);
	}
	
	private static CompilationUnit buildASTRoot(IClassFile classFile, boolean resolveBindings) {
		ASTParser parser = ASTParser.newParser(AST.JLS3);
		parser.setSource(classFile);
		parser.setResolveBindings(resolveBindings);
		return (CompilationUnit) parser.createAST(null);
	}
	
	public static CompilationUnit buildASTRoot(ICompilationUnit compilationUnit) {
		return buildASTRoot(compilationUnit, true);
	}
	
	private static CompilationUnit buildASTRoot(ICompilationUnit compilationUnit, boolean resolveBindings) {
		ASTParser parser = ASTParser.newParser(AST.JLS3);
		parser.setSource(compilationUnit);
		parser.setResolveBindings(resolveBindings);
		return (CompilationUnit) parser.createAST(null);
	}
	
	public static IType findType(String packageName, String qualifiedTypeName, IJavaProject javaProject) {
		try {
			return javaProject.findType(packageName, qualifiedTypeName.replace('$', '.'));
		} catch (JavaModelException ex) {
			JptCorePlugin.log(ex);
			return null;
		}
	}

	public static String resolveEnum(Expression expression) {
		if (expression == null) {
			return null;
		}
		switch (expression.getNodeType()) {
			case ASTNode.QUALIFIED_NAME:
			case ASTNode.SIMPLE_NAME:
				return resolveEnum((Name) expression);
			default:
				return null;
		}
	}

	public static String resolveEnum(Name enumExpression) {
		IBinding binding = enumExpression.resolveBinding();
		if (binding == null) {
			return null;  // TODO figure why this is null sometimes
		}
		if (binding.getKind() != IBinding.VARIABLE) {
			return null;
		}
		IVariableBinding variableBinding = (IVariableBinding) binding;
		return variableBinding.getType().getQualifiedName() + "." + variableBinding.getName();
	}

}

Back to the top