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.util;
13 import java.util.Iterator;
16 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.jdt.core.IClassFile;
19 import org.eclipse.jdt.core.ICompilationUnit;
20 import org.eclipse.jdt.core.IJavaElement;
21 import org.eclipse.jdt.core.IJavaProject;
22 import org.eclipse.jdt.core.ITypeRoot;
23 import org.eclipse.jdt.core.JavaCore;
24 import org.eclipse.jdt.core.WorkingCopyOwner;
25 import org.eclipse.jdt.core.dom.ASTNode;
26 import org.eclipse.jdt.core.dom.ASTParser;
27 import org.eclipse.jdt.core.dom.CompilationUnit;
29 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
31 import org.eclipse.jdt.ui.SharedASTProvider;
33 import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider;
35 public class RefactoringASTParser {
37 private ASTParser fParser;
39 public RefactoringASTParser(int level) {
40 fParser= ASTParser.newParser(level);
43 public CompilationUnit parse(ITypeRoot typeRoot, boolean resolveBindings) {
44 return parse(typeRoot, resolveBindings, null);
47 public CompilationUnit parse(ITypeRoot typeRoot, boolean resolveBindings, IProgressMonitor pm) {
48 return parse(typeRoot, null, resolveBindings, pm);
51 public CompilationUnit parse(ITypeRoot typeRoot, WorkingCopyOwner owner, boolean resolveBindings, IProgressMonitor pm) {
52 return parse(typeRoot, owner, resolveBindings, false, false, pm);
55 public CompilationUnit parse(ITypeRoot typeRoot, WorkingCopyOwner owner, boolean resolveBindings, boolean statementsRecovery, boolean bindingsRecovery, IProgressMonitor pm) {
56 fParser.setResolveBindings(resolveBindings);
57 fParser.setStatementsRecovery(statementsRecovery);
58 fParser.setBindingsRecovery(bindingsRecovery);
59 fParser.setSource(typeRoot);
61 fParser.setWorkingCopyOwner(owner);
62 fParser.setCompilerOptions(getCompilerOptions(typeRoot));
63 CompilationUnit result= (CompilationUnit) fParser.createAST(pm);
68 * @param newCuSource the source
69 * @param originalCu the compilation unit to get the name and project from
70 * @param resolveBindings whether bindings are to be resolved
71 * @param recovery whether statements and binding recovery should be enabled
72 * @param pm an {@link IProgressMonitor}, or <code>null</code>
73 * @return the parsed CompilationUnit
75 public CompilationUnit parse(String newCuSource, ICompilationUnit originalCu, boolean resolveBindings, boolean recovery, IProgressMonitor pm) {
76 fParser.setResolveBindings(resolveBindings);
77 fParser.setStatementsRecovery(recovery);
78 fParser.setBindingsRecovery(recovery);
79 fParser.setSource(newCuSource.toCharArray());
80 fParser.setUnitName(originalCu.getElementName());
81 fParser.setProject(originalCu.getJavaProject());
82 fParser.setCompilerOptions(getCompilerOptions(originalCu));
83 CompilationUnit newCUNode= (CompilationUnit) fParser.createAST(pm);
88 * @param newCfSource the source
89 * @param originalCf the class file to get the name and project from
90 * @param resolveBindings whether bindings are to be resolved
91 * @param recovery whether statements and binding recovery should be enabled
92 * @param pm an {@link IProgressMonitor}, or <code>null</code>
93 * @return the parsed CompilationUnit
95 public CompilationUnit parse(String newCfSource, IClassFile originalCf, boolean resolveBindings, boolean recovery, IProgressMonitor pm) {
96 fParser.setResolveBindings(resolveBindings);
97 fParser.setStatementsRecovery(recovery);
98 fParser.setBindingsRecovery(recovery);
99 fParser.setSource(newCfSource.toCharArray());
100 String cfName= originalCf.getElementName();
101 fParser.setUnitName(cfName.substring(0, cfName.length() - 6) + JavaModelUtil.DEFAULT_CU_SUFFIX);
102 fParser.setProject(originalCf.getJavaProject());
103 fParser.setCompilerOptions(getCompilerOptions(originalCf));
104 CompilationUnit newCUNode= (CompilationUnit) fParser.createAST(pm);
109 * Tries to get the shared AST from the ASTProvider.
110 * If the shared AST is not available, parses the type root with a
111 * RefactoringASTParser that uses settings similar to the ASTProvider.
113 * @param typeRoot the type root
114 * @param resolveBindings whether bindings are to be resolved if a new AST needs to be created
115 * @param pm an {@link IProgressMonitor}, or <code>null</code>
116 * @return the parsed CompilationUnit
118 public static CompilationUnit parseWithASTProvider(ITypeRoot typeRoot, boolean resolveBindings, IProgressMonitor pm) {
119 CompilationUnit cuNode= SharedASTProvider.getAST(typeRoot, SharedASTProvider.WAIT_ACTIVE_ONLY, pm);
120 if (cuNode != null) {
123 return new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(typeRoot, null, resolveBindings, ASTProvider.SHARED_AST_STATEMENT_RECOVERY, ASTProvider.SHARED_BINDING_RECOVERY, pm);
127 public static ICompilationUnit getCompilationUnit(ASTNode node) {
128 ASTNode root= node.getRoot();
129 if (root instanceof CompilationUnit) {
130 IJavaElement cu= ((CompilationUnit) root).getJavaElement();
131 if (cu instanceof ICompilationUnit)
132 return (ICompilationUnit) cu;
138 * Returns the compiler options used for creating the refactoring AST.
140 * Turns all errors and warnings into ignore and disables task tags. The customizable set of
141 * compiler options only contains additional Eclipse options. The standard JDK compiler options
142 * can't be changed anyway.
144 * @param element an element (not the Java model)
145 * @return compiler options
147 public static Map<String, String> getCompilerOptions(IJavaElement element) {
148 IJavaProject project= element.getJavaProject();
149 Map<String, String> options= project.getOptions(true);
150 for (Iterator<String> iter= options.keySet().iterator(); iter.hasNext();) {
151 String key= iter.next();
152 String value= options.get(key);
153 if (JavaCore.ERROR.equals(value) || JavaCore.WARNING.equals(value)) {
154 // System.out.println("Ignoring - " + key);
155 options.put(key, JavaCore.IGNORE);
158 options.put(JavaCore.COMPILER_PB_MAX_PER_UNIT, "0"); //$NON-NLS-1$
159 options.put(JavaCore.COMPILER_TASK_TAGS, ""); //$NON-NLS-1$