1 11 package org.eclipse.jdt.internal.corext.refactoring.util; 12 13 import java.util.Iterator ; 14 import java.util.Map ; 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 73 public CompilationUnit parse(String 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 92 public CompilationUnit parse(String newCfSource, IClassFile originalCf, boolean resolveBindings, boolean statementsRecovery, IProgressMonitor pm) { 93 fParser.setResolveBindings(resolveBindings); 94 fParser.setStatementsRecovery(statementsRecovery); 95 fParser.setSource(newCfSource.toCharArray()); 96 String 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 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 getCompilerOptions(IJavaElement element) { 134 IJavaProject project= element.getJavaProject(); 135 Map options= project.getOptions(true); 136 for (Iterator iter= options.keySet().iterator(); iter.hasNext();) { 140 String key= (String )iter.next(); 141 String value= (String )options.get(key); 142 if ("error".equals(value) || "warning".equals(value)) { options.put(key, "ignore"); } 146 } 147 options.put(JavaCore.COMPILER_TASK_TAGS, ""); return options; 149 } 150 } 151 | Popular Tags |