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