01: /**
02: *
03: */package core;
04:
05: import org.eclipse.jdt.core.ICompilationUnit;
06: import org.eclipse.jdt.core.dom.AST;
07: import org.eclipse.jdt.core.dom.ASTVisitor;
08: import org.eclipse.jdt.core.dom.CompilationUnit;
09: import org.eclipse.jdt.core.dom.FieldDeclaration;
10: import org.eclipse.jdt.core.dom.Type;
11: import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
12:
13: /**
14: * This is class is used by the ActionUtil for initialize
15: * the rename dialog.
16: *
17: * @author sh
18: *
19: */
20: public class CompilationUnitParser extends AbstractAST {
21: private String selection = null;
22: private FieldDeclaration field = null;
23:
24: /**
25: * This method parses all field declarations for the given name in
26: * the given compilation unit.
27: *
28: * @param unit the compilation unit to parse
29: * @param name the field name searching for
30: * @return field a field declaration instance of the found field type
31: */
32: public FieldDeclaration parseFieldDeclarations(
33: ICompilationUnit unit, String name) {
34: CompilationUnit u = parse(unit);
35: this .selection = name;
36: new FieldScanner().process(u);
37: return field;
38: }
39:
40: private class FieldScanner extends ASTVisitor {
41:
42: /**
43: * Looks for field declarations.
44: * For an occurence matching the selection
45: * a new field declaration object will be created.
46: *
47: * @param node the node to visit
48: */
49: @Override
50: public boolean visit(FieldDeclaration node) {
51: Type type = node.getType();
52: if (type.toString().equals(selection)) {
53: AST ast = node.getAST();
54: VariableDeclarationFragment vdf = ast
55: .newVariableDeclarationFragment();
56: FieldDeclaration fd = ast.newFieldDeclaration(vdf);
57: fd.setType(ast.newSimpleType(ast.newName(selection)));
58: field = fd;
59: }
60: return super .visit(node);
61: }
62:
63: /**
64: * Starts the process.
65: *
66: * @param unit the AST root node. Bindings have to been resolved.
67: */
68: public void process(CompilationUnit unit) {
69: unit.accept(this);
70: }
71: }
72: }
|