View Javadoc

1   package net.sourceforge.pmd.lang.java;
2   
3   import java.io.Writer;
4   
5   import net.sf.saxon.sxpath.IndependentContext;
6   import net.sourceforge.pmd.lang.AbstractLanguageVersionHandler;
7   import net.sourceforge.pmd.lang.DataFlowHandler;
8   import net.sourceforge.pmd.lang.Language;
9   import net.sourceforge.pmd.lang.VisitorStarter;
10  import net.sourceforge.pmd.lang.XPathHandler;
11  import net.sourceforge.pmd.lang.ast.Node;
12  import net.sourceforge.pmd.lang.ast.xpath.AbstractASTXPathHandler;
13  import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
14  import net.sourceforge.pmd.lang.java.ast.DumpFacade;
15  import net.sourceforge.pmd.lang.java.ast.JavaNode;
16  import net.sourceforge.pmd.lang.java.dfa.DataFlowFacade;
17  import net.sourceforge.pmd.lang.java.rule.JavaRuleViolationFactory;
18  import net.sourceforge.pmd.lang.java.symboltable.SymbolFacade;
19  import net.sourceforge.pmd.lang.java.typeresolution.TypeResolutionFacade;
20  import net.sourceforge.pmd.lang.java.xpath.GetCommentOnFunction;
21  import net.sourceforge.pmd.lang.java.xpath.JavaFunctions;
22  import net.sourceforge.pmd.lang.java.xpath.TypeOfFunction;
23  import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
24  
25  /**
26   * Implementation of LanguageVersionHandler for the Java AST. It uses anonymous classes
27   * as adapters of the visitors to the VisitorStarter interface.
28   *
29   * @author pieter_van_raemdonck - Application Engineers NV/SA - www.ae.be
30   */
31  public abstract class AbstractJavaHandler extends AbstractLanguageVersionHandler {
32  
33      @Override
34      public DataFlowHandler getDataFlowHandler() {
35  	return new JavaDataFlowHandler();
36      }
37  
38      @Override
39      public XPathHandler getXPathHandler() {
40  	return new AbstractASTXPathHandler() {
41  	    public void initialize() {
42  		TypeOfFunction.registerSelfInSimpleContext();
43  		GetCommentOnFunction.registerSelfInSimpleContext();
44  	    }
45  
46  	    public void initialize(IndependentContext context) {
47  		super.initialize(context, Language.JAVA, JavaFunctions.class);
48  	    }
49  	};
50      }
51  
52      public RuleViolationFactory getRuleViolationFactory() {
53  	return JavaRuleViolationFactory.INSTANCE;
54      }
55  
56      @Override
57      public VisitorStarter getDataFlowFacade() {
58  	return new VisitorStarter() {
59  	    public void start(Node rootNode) {
60  		new DataFlowFacade().initializeWith(getDataFlowHandler(), (ASTCompilationUnit) rootNode);
61  	    }
62  	};
63      }
64  
65      @Override
66      public VisitorStarter getSymbolFacade() {
67  	return new VisitorStarter() {
68  	    public void start(Node rootNode) {
69  		new SymbolFacade().initializeWith((ASTCompilationUnit) rootNode);
70  	    }
71  	};
72      }
73  
74      @Override
75      public VisitorStarter getTypeResolutionFacade(final ClassLoader classLoader) {
76  	return new VisitorStarter() {
77  	    public void start(Node rootNode) {
78  		new TypeResolutionFacade().initializeWith(classLoader, (ASTCompilationUnit) rootNode);
79  	    }
80  	};
81      }
82  
83      @Override
84      public VisitorStarter getDumpFacade(final Writer writer, final String prefix, final boolean recurse) {
85  	return new VisitorStarter() {
86  	    public void start(Node rootNode) {
87  		new DumpFacade().initializeWith(writer, prefix, recurse, (JavaNode) rootNode);
88  	    }
89  	};
90      }
91  }