Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4ca46e1e5a517015b209a0d7a350b87872b282a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*******************************************************************************
 * Copyright (c) 2007, 2009 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.jpa.gen.internal.util;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jface.text.Document;
import org.eclipse.text.edits.TextEdit;


/**
 * Performs the grunt work needed for modifying a compilation 
 * unit and performs the modified compilation unit save.
 * 
 * The typical usage is as follows:
 * <ol><li>Create an instance.
 * 
 * <li>Modify the compilation unit using AST operations performed 
 * 	on the node returned by {@link #getCompilationUnitNode()}.
 *  Alternatively you can call <code>setJavaSource</code> to change the entire source code. 
 * 
 * <li>Call the {@link #save()} method.
 * 
 */
public class CompilationUnitModifier
{
	private IJavaProject mProject;
	private ICompilationUnit mCompilationUnit;
	private CompilationUnit mCompilationUnitNode;
	private Document mDocument;
	
	public CompilationUnitModifier(IJavaProject project, String className) throws Exception {
		super();
		
		mProject = project;
		
		IType type = project.findType(className);
		if (type == null) {
			throw new Exception("The class " + className + " does not exist.");
		}
		mCompilationUnit = type.getCompilationUnit();
		if (mCompilationUnit == null) {
			throw new Exception("The source code for " + className + " does not exist.");
		}
	}
	public CompilationUnitModifier(IJavaProject project, ICompilationUnit cu) throws Exception {
		super();
		
		mProject = project;
		mCompilationUnit = cu;
	}
	public CompilationUnitModifier(IJavaProject project, ICompilationUnit cu, CompilationUnit cuNode) throws Exception {
		super();
		
		mProject = project;
		mCompilationUnit = cu;
		mCompilationUnitNode = cuNode;
		
		getCompilationUnitNode(); //to create mDocument (the caller in this case does not have to call getCompilationUnitNode)
	}
	public ICompilationUnit getCompilationUnit() {
		return mCompilationUnit;
	}
	/**
	 * Returns the compilation unit node that should be used for 
	 * tyhe modification AST operations.
	 */
	public CompilationUnit getCompilationUnitNode() {
		if (mCompilationUnitNode == null) {
			ASTParser c = ASTParser.newParser(AST.JLS3);
			c.setSource(mCompilationUnit);
			c.setResolveBindings(true);
			mCompilationUnitNode = (CompilationUnit)c.createAST(null);
		}
		if (mDocument == null) {
			try {
				mDocument = new Document(mCompilationUnit.getBuffer().getContents());
			} catch (JavaModelException e) {
				e.printStackTrace();
			}
			
			mCompilationUnitNode.recordModifications();
		}
		
		return mCompilationUnitNode;
	}
	/**
	 * Changes the entire Java source code of the compilation unit.
	 */
	public void setJavaSource(String newSource) {
		try {
			mCompilationUnit.getBuffer().setContents(newSource);
		} catch (JavaModelException e) {
			e.printStackTrace();
		}
	}
	/**
	 * Saves the compilation unit modifications.
	 */
	public void save() throws Exception {
		if (mCompilationUnitNode != null) {
			assert(mDocument != null); //see getCompilationUnitNode
			
			//computation of the text edits
			TextEdit edits = mCompilationUnitNode.rewrite(mDocument, mProject.getOptions(true));
			//computation of the new source code
			edits.apply(mDocument);
			String newSource = mDocument.get();
			// update of the compilation unit
			mCompilationUnit.getBuffer().setContents(newSource);
		}
		
		if (mCompilationUnit.isWorkingCopy()) {
			mCompilationUnit.commitWorkingCopy(true/*force*/, null/*monitor*/);
		} else {
			mCompilationUnit.save(null/*monitor*/, true/*force*/);
		}
	}
}

Back to the top