From 1fe24ef22c640abb0570932f6b8e6358e943854c Mon Sep 17 00:00:00 2001 From: Tom Copeland Date: Sat, 8 Apr 2006 00:57:41 +0000 Subject: [PATCH] Modified JJTree grammar to use node suppression for ASTModifier nodes; this replaces a bunch of DiscardableNodeCleaner hackery. git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4327 51baf565-9d33-0410-a72c-fc3788e3496d --- pmd/etc/changelog.txt | 3 +- pmd/etc/grammar/Java.jjt | 2 +- .../net/sourceforge/pmd/ast/ASTModifiers.java | 44 +----- .../pmd/ast/DiscardableNodeCleaner.java | 16 -- .../net/sourceforge/pmd/ast/JavaParser.java | 147 ++++++++---------- .../pmd/ast/JavaParserTreeConstants.java | 4 +- .../pmd/ast/JavaParserVisitor.java | 1 - 7 files changed, 67 insertions(+), 150 deletions(-) diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 3f9e270ac4..86582fd2ce 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -3,7 +3,8 @@ Fixed bug 1465574 - UnusedPrivateMethod no longer reports false positives when a Fixed major bug in CPD; it was not picking up files other than .java or .jsp. Added RuleViolation.getBeginColumn()/getEndColumn() Added an IRuleViolation interface and modified various code classes (include Renderer implementations and Report) to use it. -Modified Java grammar to use conditional node descriptors for some expression nodes +Modified JJTree grammar to use conditional node descriptors for some expression nodes. +Modified JJTree grammar to use node suppression for ASTModifier nodes; this replaces a bunch of DiscardableNodeCleaner hackery. March 29, 2006 - 3.6: New rules: diff --git a/pmd/etc/grammar/Java.jjt b/pmd/etc/grammar/Java.jjt index 1104ae3b2b..7c29d3e7e1 100644 --- a/pmd/etc/grammar/Java.jjt +++ b/pmd/etc/grammar/Java.jjt @@ -1080,7 +1080,7 @@ void ImportDeclaration() : * syntax errors for simple modifier mistakes. It will also enable us to give * better error messages. */ -int Modifiers(): +int Modifiers() #void: { int modifiers = 0; } diff --git a/pmd/src/net/sourceforge/pmd/ast/ASTModifiers.java b/pmd/src/net/sourceforge/pmd/ast/ASTModifiers.java index 0c2b7bc262..6419d7a2db 100644 --- a/pmd/src/net/sourceforge/pmd/ast/ASTModifiers.java +++ b/pmd/src/net/sourceforge/pmd/ast/ASTModifiers.java @@ -3,6 +3,7 @@ package net.sourceforge.pmd.ast; public class ASTModifiers extends SimpleJavaNode { + public ASTModifiers(int id) { super(id); } @@ -11,49 +12,6 @@ public class ASTModifiers extends SimpleJavaNode { super(p, id); } - public void discardIfNecessary() { - SimpleNode parent = (SimpleNode) jjtGetParent(); - if (allChildrenAreAnnotations()) { - handleAnnotations(parent); - } else if (parent.jjtGetNumChildren() == 2) { - parent.children = new Node[]{parent.children[1]}; - } else if (parent.jjtGetNumChildren() == 3) { - // AnnotationTypeMemberDeclaration with default value, like this: - // String defaultValue() default ""; - parent.children = new Node[]{parent.children[1], parent.children[2]}; - } else if (parent.jjtGetNumChildren() == 4) { - // JDK 1.5 forloop syntax - parent.children = new Node[]{parent.children[1], parent.children[2], parent.children[3]}; - } else { - throw new RuntimeException("ASTModifiers.discardIfNecessary didn't see expected children"); - } - } - - private boolean allChildrenAreAnnotations() { - if (jjtGetNumChildren() == 0) { - return false; - } - for (int i = 0; i < jjtGetNumChildren(); i++) { - if (!(jjtGetChild(i) instanceof ASTAnnotation)) { - return false; - } - } - return true; - } - - private void handleAnnotations(SimpleNode parent) { - SimpleNode kid = (SimpleNode) this.jjtGetChild(0); - kid.jjtSetParent(parent); - for (int i = 0; i < jjtGetNumChildren(); i++) { - if (i == 0) { - parent.jjtReplaceChild(this, kid); - } else { - parent.jjtAddChild(jjtGetChild(i), i); - } - } - } - - /** * Accept the visitor. * */ diff --git a/pmd/src/net/sourceforge/pmd/ast/DiscardableNodeCleaner.java b/pmd/src/net/sourceforge/pmd/ast/DiscardableNodeCleaner.java index 2ca21acf97..65090372a0 100644 --- a/pmd/src/net/sourceforge/pmd/ast/DiscardableNodeCleaner.java +++ b/pmd/src/net/sourceforge/pmd/ast/DiscardableNodeCleaner.java @@ -6,25 +6,9 @@ import java.util.List; public class DiscardableNodeCleaner { private static final Class[] clazzes = new Class[]{ -/* - ASTEqualityExpression.class, - ASTAndExpression.class, - ASTInstanceOfExpression.class, - ASTUnaryExpression.class, - ASTShiftExpression.class, - ASTConditionalOrExpression.class, - ASTInclusiveOrExpression.class, - ASTExclusiveOrExpression.class, - ASTConditionalExpression.class, - ASTRelationalExpression.class, - ASTMultiplicativeExpression.class, - ASTAdditiveExpression.class, - ASTConditionalAndExpression.class, -*/ ASTUnaryExpression.class, ASTUnaryExpressionNotPlusMinus.class, ASTPostfixExpression.class, - ASTModifiers.class }; public void clean(ASTCompilationUnit root) { diff --git a/pmd/src/net/sourceforge/pmd/ast/JavaParser.java b/pmd/src/net/sourceforge/pmd/ast/JavaParser.java index 802028507e..e6f62316d8 100644 --- a/pmd/src/net/sourceforge/pmd/ast/JavaParser.java +++ b/pmd/src/net/sourceforge/pmd/ast/JavaParser.java @@ -282,94 +282,69 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java * better error messages. */ final public int Modifiers() throws ParseException { - /*@bgen(jjtree) Modifiers */ - ASTModifiers jjtn000 = new ASTModifiers(this, JJTMODIFIERS); - boolean jjtc000 = true; - jjtree.openNodeScope(jjtn000);int modifiers = 0; - try { - label_3: - while (true) { - if (jj_2_1(2)) { - ; - } else { - break label_3; - } - switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { - case PUBLIC: - jj_consume_token(PUBLIC); - modifiers |= AccessNode.PUBLIC; - break; - case STATIC: - jj_consume_token(STATIC); - modifiers |= AccessNode.STATIC; - break; - case PROTECTED: - jj_consume_token(PROTECTED); - modifiers |= AccessNode.PROTECTED; - break; - case PRIVATE: - jj_consume_token(PRIVATE); - modifiers |= AccessNode.PRIVATE; - break; - case FINAL: - jj_consume_token(FINAL); - modifiers |= AccessNode.FINAL; - break; - case ABSTRACT: - jj_consume_token(ABSTRACT); - modifiers |= AccessNode.ABSTRACT; - break; - case SYNCHRONIZED: - jj_consume_token(SYNCHRONIZED); - modifiers |= AccessNode.SYNCHRONIZED; - break; - case NATIVE: - jj_consume_token(NATIVE); - modifiers |= AccessNode.NATIVE; - break; - case TRANSIENT: - jj_consume_token(TRANSIENT); - modifiers |= AccessNode.TRANSIENT; - break; - case VOLATILE: - jj_consume_token(VOLATILE); - modifiers |= AccessNode.VOLATILE; - break; - case STRICTFP: - jj_consume_token(STRICTFP); - modifiers |= AccessNode.STRICTFP; - break; - case AT: - Annotation(); - break; - default: - jj_la1[7] = jj_gen; - jj_consume_token(-1); - throw new ParseException(); - } + int modifiers = 0; + label_3: + while (true) { + if (jj_2_1(2)) { + ; + } else { + break label_3; + } + switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { + case PUBLIC: + jj_consume_token(PUBLIC); + modifiers |= AccessNode.PUBLIC; + break; + case STATIC: + jj_consume_token(STATIC); + modifiers |= AccessNode.STATIC; + break; + case PROTECTED: + jj_consume_token(PROTECTED); + modifiers |= AccessNode.PROTECTED; + break; + case PRIVATE: + jj_consume_token(PRIVATE); + modifiers |= AccessNode.PRIVATE; + break; + case FINAL: + jj_consume_token(FINAL); + modifiers |= AccessNode.FINAL; + break; + case ABSTRACT: + jj_consume_token(ABSTRACT); + modifiers |= AccessNode.ABSTRACT; + break; + case SYNCHRONIZED: + jj_consume_token(SYNCHRONIZED); + modifiers |= AccessNode.SYNCHRONIZED; + break; + case NATIVE: + jj_consume_token(NATIVE); + modifiers |= AccessNode.NATIVE; + break; + case TRANSIENT: + jj_consume_token(TRANSIENT); + modifiers |= AccessNode.TRANSIENT; + break; + case VOLATILE: + jj_consume_token(VOLATILE); + modifiers |= AccessNode.VOLATILE; + break; + case STRICTFP: + jj_consume_token(STRICTFP); + modifiers |= AccessNode.STRICTFP; + break; + case AT: + Annotation(); + break; + default: + jj_la1[7] = jj_gen; + jj_consume_token(-1); + throw new ParseException(); } - jjtree.closeNodeScope(jjtn000, true); - jjtc000 = false; - {if (true) return modifiers;} - } catch (Throwable jjte000) { - if (jjtc000) { - jjtree.clearNodeScope(jjtn000); - jjtc000 = false; - } else { - jjtree.popNode(); - } - if (jjte000 instanceof RuntimeException) { - {if (true) throw (RuntimeException)jjte000;} - } - if (jjte000 instanceof ParseException) { - {if (true) throw (ParseException)jjte000;} - } - {if (true) throw (Error)jjte000;} - } finally { - if (jjtc000) { - jjtree.closeNodeScope(jjtn000, true); - } } + {if (true) return modifiers;} throw new RuntimeException("Missing return statement in function"); } diff --git a/pmd/src/net/sourceforge/pmd/ast/JavaParserTreeConstants.java b/pmd/src/net/sourceforge/pmd/ast/JavaParserTreeConstants.java index 6777d8c732..430a6da28e 100644 --- a/pmd/src/net/sourceforge/pmd/ast/JavaParserTreeConstants.java +++ b/pmd/src/net/sourceforge/pmd/ast/JavaParserTreeConstants.java @@ -7,7 +7,7 @@ public interface JavaParserTreeConstants public int JJTCOMPILATIONUNIT = 0; public int JJTPACKAGEDECLARATION = 1; public int JJTIMPORTDECLARATION = 2; - public int JJTMODIFIERS = 3; + public int JJTVOID = 3; public int JJTTYPEDECLARATION = 4; public int JJTCLASSORINTERFACEDECLARATION = 5; public int JJTEXTENDSLIST = 6; @@ -119,7 +119,7 @@ public interface JavaParserTreeConstants "CompilationUnit", "PackageDeclaration", "ImportDeclaration", - "Modifiers", + "void", "TypeDeclaration", "ClassOrInterfaceDeclaration", "ExtendsList", diff --git a/pmd/src/net/sourceforge/pmd/ast/JavaParserVisitor.java b/pmd/src/net/sourceforge/pmd/ast/JavaParserVisitor.java index 30b0e475bb..6a4fee8ce7 100644 --- a/pmd/src/net/sourceforge/pmd/ast/JavaParserVisitor.java +++ b/pmd/src/net/sourceforge/pmd/ast/JavaParserVisitor.java @@ -8,7 +8,6 @@ public interface JavaParserVisitor public Object visit(ASTCompilationUnit node, Object data); public Object visit(ASTPackageDeclaration node, Object data); public Object visit(ASTImportDeclaration node, Object data); - public Object visit(ASTModifiers node, Object data); public Object visit(ASTTypeDeclaration node, Object data); public Object visit(ASTClassOrInterfaceDeclaration node, Object data); public Object visit(ASTExtendsList node, Object data);