KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > util > RefactoringASTParser


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.refactoring.util;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.runtime.IProgressMonitor;
17
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;
28
29 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
30
31 import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider;
32
33 public class RefactoringASTParser {
34
35     private ASTParser fParser;
36     
37     public RefactoringASTParser(int level) {
38         fParser= ASTParser.newParser(level);
39     }
40     
41     public CompilationUnit parse(ITypeRoot typeRoot, boolean resolveBindings) {
42         return parse(typeRoot, resolveBindings, null);
43     }
44
45     public CompilationUnit parse(ITypeRoot typeRoot, boolean resolveBindings, IProgressMonitor pm) {
46         return parse(typeRoot, null, resolveBindings, pm);
47     }
48
49     public CompilationUnit parse(ITypeRoot typeRoot, WorkingCopyOwner owner, boolean resolveBindings, IProgressMonitor pm) {
50         return parse(typeRoot, owner, resolveBindings, false, false, pm);
51     }
52
53     public CompilationUnit parse(ITypeRoot typeRoot, WorkingCopyOwner owner, boolean resolveBindings, boolean statementsRecovery, boolean bindingsRecovery, IProgressMonitor pm) {
54         fParser.setResolveBindings(resolveBindings);
55         fParser.setStatementsRecovery(statementsRecovery);
56         fParser.setBindingsRecovery(bindingsRecovery);
57         fParser.setSource(typeRoot);
58         if (owner != null)
59             fParser.setWorkingCopyOwner(owner);
60         fParser.setCompilerOptions(getCompilerOptions(typeRoot));
61         CompilationUnit result= (CompilationUnit) fParser.createAST(pm);
62         return result;
63     }
64
65     /**
66      * @param newCuSource the source
67      * @param originalCu the compilation unit to get the name and project from
68      * @param resolveBindings whether bindings are to be resolved
69      * @param statementsRecovery whether statements recovery should be enabled
70      * @param pm an {@link IProgressMonitor}, or <code>null</code>
71      * @return the parsed CompilationUnit
72      */

73     public CompilationUnit parse(String JavaDoc newCuSource, ICompilationUnit originalCu, boolean resolveBindings, boolean statementsRecovery, IProgressMonitor pm) {
74         fParser.setResolveBindings(resolveBindings);
75         fParser.setStatementsRecovery(statementsRecovery);
76         fParser.setSource(newCuSource.toCharArray());
77         fParser.setUnitName(originalCu.getElementName());
78         fParser.setProject(originalCu.getJavaProject());
79         fParser.setCompilerOptions(getCompilerOptions(originalCu));
80         CompilationUnit newCUNode= (CompilationUnit) fParser.createAST(pm);
81         return newCUNode;
82     }
83     
84     /**
85      * @param newCfSource the source
86      * @param originalCf the class file to get the name and project from
87      * @param resolveBindings whether bindings are to be resolved
88      * @param statementsRecovery whether statements recovery should be enabled
89      * @param pm an {@link IProgressMonitor}, or <code>null</code>
90      * @return the parsed CompilationUnit
91      */

92     public CompilationUnit parse(String JavaDoc newCfSource, IClassFile originalCf, boolean resolveBindings, boolean statementsRecovery, IProgressMonitor pm) {
93         fParser.setResolveBindings(resolveBindings);
94         fParser.setStatementsRecovery(statementsRecovery);
95         fParser.setSource(newCfSource.toCharArray());
96         String JavaDoc cfName= originalCf.getElementName();
97         fParser.setUnitName(cfName.substring(0, cfName.length() - 6) + JavaModelUtil.DEFAULT_CU_SUFFIX);
98         fParser.setProject(originalCf.getJavaProject());
99         fParser.setCompilerOptions(getCompilerOptions(originalCf));
100         CompilationUnit newCUNode= (CompilationUnit) fParser.createAST(pm);
101         return newCUNode;
102     }
103     
104     /**
105      * Tries to get the shared AST from the ASTProvider.
106      * If the shared AST is not available, parses the type root with a
107      * RefactoringASTParser that uses settings similar to the ASTProvider.
108      *
109      * @param typeRoot the type root
110      * @param resolveBindings TODO
111      * @param pm an {@link IProgressMonitor}, or <code>null</code>
112      * @return the parsed CompilationUnit
113      */

114     public static CompilationUnit parseWithASTProvider(ITypeRoot typeRoot, boolean resolveBindings, IProgressMonitor pm) {
115         CompilationUnit cuNode= ASTProvider.getASTProvider().getAST(typeRoot, ASTProvider.WAIT_ACTIVE_ONLY, pm);
116         if (cuNode != null) {
117             return cuNode;
118         } else {
119             return new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(typeRoot, null, resolveBindings, ASTProvider.SHARED_AST_STATEMENT_RECOVERY, ASTProvider.SHARED_BINDING_RECOVERY, pm);
120         }
121     }
122
123     public static ICompilationUnit getCompilationUnit(ASTNode node) {
124         ASTNode root= node.getRoot();
125         if (root instanceof CompilationUnit) {
126             IJavaElement cu= ((CompilationUnit) root).getJavaElement();
127             if (cu instanceof ICompilationUnit)
128                 return (ICompilationUnit) cu;
129         }
130         return null;
131     }
132     
133     public static Map JavaDoc getCompilerOptions(IJavaElement element) {
134         IJavaProject project= element.getJavaProject();
135         Map JavaDoc options= project.getOptions(true);
136         // turn all errors and warnings into ignore. The customizable set of compiler
137
// options only contains additional Eclipse options. The standard JDK compiler
138
// options can't be changed anyway.
139
for (Iterator JavaDoc iter= options.keySet().iterator(); iter.hasNext();) {
140             String JavaDoc key= (String JavaDoc)iter.next();
141             String JavaDoc value= (String JavaDoc)options.get(key);
142             if ("error".equals(value) || "warning".equals(value)) { //$NON-NLS-1$//$NON-NLS-2$
143
// System.out.println("Ignoring - " + key);
144
options.put(key, "ignore"); //$NON-NLS-1$
145
}
146         }
147         options.put(JavaCore.COMPILER_TASK_TAGS, ""); //$NON-NLS-1$
148
return options;
149     }
150 }
151
Popular Tags