diff --git a/pmd/src/net/sourceforge/pmd/lang/java/AbstractJavaHandler.java b/pmd/src/net/sourceforge/pmd/lang/java/AbstractJavaHandler.java index e226b98978..15a67011bf 100644 --- a/pmd/src/net/sourceforge/pmd/lang/java/AbstractJavaHandler.java +++ b/pmd/src/net/sourceforge/pmd/lang/java/AbstractJavaHandler.java @@ -17,6 +17,7 @@ import net.sourceforge.pmd.lang.java.dfa.DataFlowFacade; import net.sourceforge.pmd.lang.java.rule.JavaRuleViolationFactory; import net.sourceforge.pmd.lang.java.symboltable.SymbolFacade; import net.sourceforge.pmd.lang.java.typeresolution.TypeResolutionFacade; +import net.sourceforge.pmd.lang.java.xpath.GetCommentOnFunction; import net.sourceforge.pmd.lang.java.xpath.JavaFunctions; import net.sourceforge.pmd.lang.java.xpath.TypeOfFunction; import net.sourceforge.pmd.lang.rule.RuleViolationFactory; @@ -39,6 +40,7 @@ public abstract class AbstractJavaHandler extends AbstractLanguageVersionHandler return new AbstractASTXPathHandler() { public void initialize() { TypeOfFunction.registerSelfInSimpleContext(); + GetCommentOnFunction.registerSelfInSimpleContext(); } public void initialize(IndependentContext context) { diff --git a/pmd/src/net/sourceforge/pmd/lang/java/xpath/GetCommentOnFunction.java b/pmd/src/net/sourceforge/pmd/lang/java/xpath/GetCommentOnFunction.java new file mode 100644 index 0000000000..9586c7e1e2 --- /dev/null +++ b/pmd/src/net/sourceforge/pmd/lang/java/xpath/GetCommentOnFunction.java @@ -0,0 +1,45 @@ +package net.sourceforge.pmd.lang.java.xpath; + +import java.util.List; + +import net.sourceforge.pmd.lang.ast.AbstractNode; +import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; +import net.sourceforge.pmd.lang.java.ast.Comment; + +import org.jaxen.Context; +import org.jaxen.Function; +import org.jaxen.FunctionCallException; +import org.jaxen.SimpleFunctionContext; +import org.jaxen.XPathFunctionContext; + +/** + * + * @author Andy Throgmorton + */ +public class GetCommentOnFunction implements Function { + + public static void registerSelfInSimpleContext() { + // see http://jaxen.org/extensions.html + ((SimpleFunctionContext) XPathFunctionContext.getInstance()).registerFunction(null, "getCommentOn", new GetCommentOnFunction()); + } + + public Object call(Context context, List args) throws FunctionCallException { + if (!args.isEmpty()) { + return Boolean.FALSE; + } + Node n = (Node) context.getNodeSet().get(0); + if (n instanceof AbstractNode) { + int codeBeginLine = ((AbstractNode) n).getBeginLine(); + int codeEndLine = ((AbstractNode) n).getEndLine(); + + List commentList = ((AbstractNode)n).getFirstParentOfType(ASTCompilationUnit.class).getComments(); + for (Comment comment : commentList) { + if (comment.getBeginLine() == codeBeginLine || comment.getEndLine() == codeEndLine) { + return comment.getImage(); + } + } + } + return Boolean.FALSE; + } +} \ No newline at end of file diff --git a/pmd/src/net/sourceforge/pmd/lang/java/xpath/JavaFunctions.java b/pmd/src/net/sourceforge/pmd/lang/java/xpath/JavaFunctions.java index 5218522590..ad76b2880d 100644 --- a/pmd/src/net/sourceforge/pmd/lang/java/xpath/JavaFunctions.java +++ b/pmd/src/net/sourceforge/pmd/lang/java/xpath/JavaFunctions.java @@ -8,12 +8,13 @@ import net.sourceforge.pmd.lang.ast.xpath.saxon.ElementNode; * Exposes all Java Language specific functions for Saxon use. */ public class JavaFunctions { + public static boolean typeof(XPathContext context, String nodeTypeName, String fullTypeName) { - return typeof(context, nodeTypeName, fullTypeName, null); + return typeof(context, nodeTypeName, fullTypeName, null); } public static boolean typeof(XPathContext context, String nodeTypeName, String fullTypeName, String shortTypeName) { - return TypeOfFunction.typeof((Node) ((ElementNode) context.getContextItem()).getUnderlyingNode(), nodeTypeName, - fullTypeName, shortTypeName); + return TypeOfFunction.typeof((Node) ((ElementNode) context.getContextItem()).getUnderlyingNode(), nodeTypeName, + fullTypeName, shortTypeName); } } diff --git a/pmd/src/net/sourceforge/pmd/lang/xpath/Initializer.java b/pmd/src/net/sourceforge/pmd/lang/xpath/Initializer.java index e8ff5364ef..e6de203273 100644 --- a/pmd/src/net/sourceforge/pmd/lang/xpath/Initializer.java +++ b/pmd/src/net/sourceforge/pmd/lang/xpath/Initializer.java @@ -7,6 +7,8 @@ import net.sf.saxon.sxpath.IndependentContext; import net.sourceforge.pmd.lang.Language; import net.sourceforge.pmd.lang.LanguageVersion; import net.sourceforge.pmd.lang.LanguageVersionHandler; +import net.sourceforge.pmd.lang.java.xpath.GetCommentOnFunction; +import net.sourceforge.pmd.lang.java.xpath.TypeOfFunction; /** * This class serves as the means to perform XPath related static initialization. @@ -27,34 +29,36 @@ public class Initializer { * Perform all initialization. */ public static void initialize(IndependentContext context) { - context.declareNamespace("pmd", "java:" + PMDFunctions.class.getName()); - for (Language language : Language.values()) { - for (LanguageVersion languageVersion : language.getVersions()) { - LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler(); - if (languageVersionHandler != null) { - languageVersionHandler.getXPathHandler().initialize(context); + context.declareNamespace("pmd", "java:" + PMDFunctions.class.getName()); + for (Language language : Language.values()) { + for (LanguageVersion languageVersion : language.getVersions()) { + LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler(); + if (languageVersionHandler != null) { + languageVersionHandler.getXPathHandler().initialize(context); + } + } } - } - } } static { - initializeGlobal(); - initializeLanguages(); + initializeGlobal(); + initializeLanguages(); } private static void initializeGlobal() { - MatchesFunction.registerSelfInSimpleContext(); + GetCommentOnFunction.registerSelfInSimpleContext(); + MatchesFunction.registerSelfInSimpleContext(); + TypeOfFunction.registerSelfInSimpleContext(); } private static void initializeLanguages() { - for (Language language : Language.values()) { - for (LanguageVersion languageVersion : language.getVersions()) { - LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler(); - if (languageVersionHandler != null) { - languageVersionHandler.getXPathHandler().initialize(); + for (Language language : Language.values()) { + for (LanguageVersion languageVersion : language.getVersions()) { + LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler(); + if (languageVersionHandler != null) { + languageVersionHandler.getXPathHandler().initialize(); + } + } } - } - } } }