001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring.util;
011:
012: import java.util.Iterator;
013: import java.util.Map;
014:
015: import org.eclipse.core.runtime.IProgressMonitor;
016:
017: import org.eclipse.jdt.core.IClassFile;
018: import org.eclipse.jdt.core.ICompilationUnit;
019: import org.eclipse.jdt.core.IJavaElement;
020: import org.eclipse.jdt.core.IJavaProject;
021: import org.eclipse.jdt.core.ITypeRoot;
022: import org.eclipse.jdt.core.JavaCore;
023: import org.eclipse.jdt.core.WorkingCopyOwner;
024: import org.eclipse.jdt.core.dom.ASTNode;
025: import org.eclipse.jdt.core.dom.ASTParser;
026: import org.eclipse.jdt.core.dom.CompilationUnit;
027:
028: import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
029:
030: import org.eclipse.jdt.ui.SharedASTProvider;
031:
032: import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider;
033:
034: public class RefactoringASTParser {
035:
036: private ASTParser fParser;
037:
038: public RefactoringASTParser(int level) {
039: fParser = ASTParser.newParser(level);
040: }
041:
042: public CompilationUnit parse(ITypeRoot typeRoot,
043: boolean resolveBindings) {
044: return parse(typeRoot, resolveBindings, null);
045: }
046:
047: public CompilationUnit parse(ITypeRoot typeRoot,
048: boolean resolveBindings, IProgressMonitor pm) {
049: return parse(typeRoot, null, resolveBindings, pm);
050: }
051:
052: public CompilationUnit parse(ITypeRoot typeRoot,
053: WorkingCopyOwner owner, boolean resolveBindings,
054: IProgressMonitor pm) {
055: return parse(typeRoot, owner, resolveBindings, false, false, pm);
056: }
057:
058: public CompilationUnit parse(ITypeRoot typeRoot,
059: WorkingCopyOwner owner, boolean resolveBindings,
060: boolean statementsRecovery, boolean bindingsRecovery,
061: IProgressMonitor pm) {
062: fParser.setResolveBindings(resolveBindings);
063: fParser.setStatementsRecovery(statementsRecovery);
064: fParser.setBindingsRecovery(bindingsRecovery);
065: fParser.setSource(typeRoot);
066: if (owner != null)
067: fParser.setWorkingCopyOwner(owner);
068: fParser.setCompilerOptions(getCompilerOptions(typeRoot));
069: CompilationUnit result = (CompilationUnit) fParser
070: .createAST(pm);
071: return result;
072: }
073:
074: /**
075: * @param newCuSource the source
076: * @param originalCu the compilation unit to get the name and project from
077: * @param resolveBindings whether bindings are to be resolved
078: * @param statementsRecovery whether statements recovery should be enabled
079: * @param pm an {@link IProgressMonitor}, or <code>null</code>
080: * @return the parsed CompilationUnit
081: */
082: public CompilationUnit parse(String newCuSource,
083: ICompilationUnit originalCu, boolean resolveBindings,
084: boolean statementsRecovery, IProgressMonitor pm) {
085: fParser.setResolveBindings(resolveBindings);
086: fParser.setStatementsRecovery(statementsRecovery);
087: fParser.setSource(newCuSource.toCharArray());
088: fParser.setUnitName(originalCu.getElementName());
089: fParser.setProject(originalCu.getJavaProject());
090: fParser.setCompilerOptions(getCompilerOptions(originalCu));
091: CompilationUnit newCUNode = (CompilationUnit) fParser
092: .createAST(pm);
093: return newCUNode;
094: }
095:
096: /**
097: * @param newCfSource the source
098: * @param originalCf the class file to get the name and project from
099: * @param resolveBindings whether bindings are to be resolved
100: * @param statementsRecovery whether statements recovery should be enabled
101: * @param pm an {@link IProgressMonitor}, or <code>null</code>
102: * @return the parsed CompilationUnit
103: */
104: public CompilationUnit parse(String newCfSource,
105: IClassFile originalCf, boolean resolveBindings,
106: boolean statementsRecovery, IProgressMonitor pm) {
107: fParser.setResolveBindings(resolveBindings);
108: fParser.setStatementsRecovery(statementsRecovery);
109: fParser.setSource(newCfSource.toCharArray());
110: String cfName = originalCf.getElementName();
111: fParser.setUnitName(cfName.substring(0, cfName.length() - 6)
112: + JavaModelUtil.DEFAULT_CU_SUFFIX);
113: fParser.setProject(originalCf.getJavaProject());
114: fParser.setCompilerOptions(getCompilerOptions(originalCf));
115: CompilationUnit newCUNode = (CompilationUnit) fParser
116: .createAST(pm);
117: return newCUNode;
118: }
119:
120: /**
121: * Tries to get the shared AST from the ASTProvider.
122: * If the shared AST is not available, parses the type root with a
123: * RefactoringASTParser that uses settings similar to the ASTProvider.
124: *
125: * @param typeRoot the type root
126: * @param resolveBindings TODO
127: * @param pm an {@link IProgressMonitor}, or <code>null</code>
128: * @return the parsed CompilationUnit
129: */
130: public static CompilationUnit parseWithASTProvider(
131: ITypeRoot typeRoot, boolean resolveBindings,
132: IProgressMonitor pm) {
133: CompilationUnit cuNode = SharedASTProvider.getAST(typeRoot,
134: SharedASTProvider.WAIT_ACTIVE_ONLY, pm);
135: if (cuNode != null) {
136: return cuNode;
137: } else {
138: return new RefactoringASTParser(
139: ASTProvider.SHARED_AST_LEVEL).parse(typeRoot, null,
140: resolveBindings,
141: ASTProvider.SHARED_AST_STATEMENT_RECOVERY,
142: ASTProvider.SHARED_BINDING_RECOVERY, pm);
143: }
144: }
145:
146: public static ICompilationUnit getCompilationUnit(ASTNode node) {
147: ASTNode root = node.getRoot();
148: if (root instanceof CompilationUnit) {
149: IJavaElement cu = ((CompilationUnit) root).getJavaElement();
150: if (cu instanceof ICompilationUnit)
151: return (ICompilationUnit) cu;
152: }
153: return null;
154: }
155:
156: public static Map getCompilerOptions(IJavaElement element) {
157: IJavaProject project = element.getJavaProject();
158: Map options = project.getOptions(true);
159: // turn all errors and warnings into ignore. The customizable set of compiler
160: // options only contains additional Eclipse options. The standard JDK compiler
161: // options can't be changed anyway.
162: for (Iterator iter = options.keySet().iterator(); iter
163: .hasNext();) {
164: String key = (String) iter.next();
165: String value = (String) options.get(key);
166: if ("error".equals(value) || "warning".equals(value)) { //$NON-NLS-1$//$NON-NLS-2$
167: // System.out.println("Ignoring - " + key);
168: options.put(key, "ignore"); //$NON-NLS-1$
169: }
170: }
171: options.put(JavaCore.COMPILER_TASK_TAGS, ""); //$NON-NLS-1$
172: return options;
173: }
174: }
|