01: /**
02: *
03: */package core;
04:
05: import java.util.Iterator;
06:
07: import org.eclipse.jdt.core.ICompilationUnit;
08: import org.eclipse.jdt.core.dom.ASTVisitor;
09: import org.eclipse.jdt.core.dom.CompilationUnit;
10: import org.eclipse.jdt.core.dom.FieldDeclaration;
11: import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
12:
13: import diagram.section.EnvEntry;
14:
15: /**
16: * @author sh
17: *
18: */
19: public class FieldDeclarationParser extends AbstractAST {
20: private EnvEntry envEntry = null;
21: private FieldDeclaration field = null;
22:
23: /**
24: * This method parses all field declarations for the given EnvEntry in
25: * the given compilation unit.
26: *
27: * @param unit the compilation unit to parse
28: * @param envEntry the environment entry searching for
29: * @return field a field declaration instance of the found field type
30: */
31: public FieldDeclaration parseFieldDeclarations(
32: ICompilationUnit unit, EnvEntry envEntry) {
33: CompilationUnit u = parse(unit);
34: this .envEntry = envEntry;
35: new FieldScanner().process(u);
36: return field;
37: }
38:
39: private class FieldScanner extends ASTVisitor {
40:
41: /**
42: * Looks for field declarations.
43: *
44: * @param node the node to visit
45: */
46: @Override
47: public boolean visit(FieldDeclaration node) {
48: for (Iterator iterator = node.fragments().iterator(); iterator
49: .hasNext();) {
50: Object obj = (Object) iterator.next();
51: if (obj instanceof VariableDeclarationFragment) {
52: VariableDeclarationFragment var = (VariableDeclarationFragment) obj;
53: String name = var.getName().toString();
54: String type = node.getType().toString();
55: if (name.equals(envEntry.getName())
56: && type.equals(envEntry.getType())) {
57: field = node;
58: }
59: }
60: }
61: return super .visit(node);
62: }
63:
64: /**
65: * Starts the process.
66: *
67: * @param unit the AST root node. Bindings have to been resolved.
68: */
69: public void process(CompilationUnit unit) {
70: unit.accept(this);
71: }
72: }
73: }
|