View Javadoc

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