01: /*******************************************************************************
02: * Copyright (c) 2000, 2005 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring.typeconstraints;
11:
12: import org.eclipse.jdt.core.ICompilationUnit;
13: import org.eclipse.jdt.core.WorkingCopyOwner;
14: import org.eclipse.jdt.core.dom.AST;
15: import org.eclipse.jdt.core.dom.ASTNode;
16: import org.eclipse.jdt.core.dom.ASTParser;
17: import org.eclipse.jdt.core.dom.CompilationUnit;
18:
19: import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
20:
21: public class ASTCreator {
22:
23: public static final String CU_PROPERTY = "org.eclipse.jdt.ui.refactoring.cu"; //$NON-NLS-1$
24:
25: private ASTCreator() {
26: //private
27: }
28:
29: public static CompilationUnit createAST(ICompilationUnit cu,
30: WorkingCopyOwner workingCopyOwner) {
31: CompilationUnit cuNode = getCuNode(workingCopyOwner, cu);
32: cuNode.setProperty(CU_PROPERTY, cu);
33: return cuNode;
34: }
35:
36: private static CompilationUnit getCuNode(
37: WorkingCopyOwner workingCopyOwner, ICompilationUnit cu) {
38: ASTParser p = ASTParser.newParser(AST.JLS3);
39: p.setSource(cu);
40: p.setResolveBindings(true);
41: p.setWorkingCopyOwner(workingCopyOwner);
42: p.setCompilerOptions(RefactoringASTParser
43: .getCompilerOptions(cu));
44: return (CompilationUnit) p.createAST(null);
45: }
46:
47: public static ICompilationUnit getCu(ASTNode node) {
48: Object property = node.getRoot().getProperty(CU_PROPERTY);
49: if (property instanceof ICompilationUnit)
50: return (ICompilationUnit) property;
51: return null;
52: }
53: }
|