Implement for java

This commit is contained in:
Clément Fournier
2020-03-17 19:49:06 +01:00
parent 87ba035aa6
commit 8fb9d2e74d
3 changed files with 109 additions and 6 deletions

View File

@ -12,8 +12,6 @@ import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.ast.xpath.Attribute;
import net.sourceforge.pmd.lang.symboltable.ScopedNode;
import com.sun.istack.internal.Nullable;
/**
* Gathers some services to customise how language implementations bind
* to the designer.
@ -41,7 +39,6 @@ public interface DesignerBindings {
* <p>Order of the collection is unimportant, it's sorted using
* {@link AdditionalInfo#getAlphaSortKey()}.
*/
@Nullable
Collection<AdditionalInfo> getAdditionalInfo(Node node);
@ -59,6 +56,10 @@ public interface DesignerBindings {
this.display = display;
}
public AdditionalInfo(String display) {
this(display, display);
}
/**
* Returns the string used to sort the additional info.
* For example, returning {@code "A"} ensures this is displayed
@ -96,7 +97,7 @@ public interface DesignerBindings {
*
* <p>This method is meant to break the designer's dependency on {@link Node#getImage()}.
*/
@Nullable
// @Nullable
Attribute getMainAttribute(Node node);
@ -112,7 +113,7 @@ public interface DesignerBindings {
* in the treeview and other relevant places. Returns null if no icon
* is applicable.
*/
@Nullable
// @Nullable
TreeIconId getIcon(Node node);
@ -125,7 +126,7 @@ public interface DesignerBindings {
METHOD,
CONSTRUCTOR,
FIELD,
LOCAL_VAR
VARIABLE
}

View File

@ -21,6 +21,7 @@ import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.ast.MethodLikeNode;
import net.sourceforge.pmd.lang.java.dfa.DataFlowFacade;
import net.sourceforge.pmd.lang.java.dfa.JavaDFAGraphRule;
import net.sourceforge.pmd.lang.java.internal.JavaDesignerBindings;
import net.sourceforge.pmd.lang.java.multifile.MultifileVisitorFacade;
import net.sourceforge.pmd.lang.java.qname.QualifiedNameResolver;
import net.sourceforge.pmd.lang.java.rule.JavaRuleViolationFactory;
@ -34,6 +35,7 @@ import net.sourceforge.pmd.lang.java.xpath.TypeIsFunction;
import net.sourceforge.pmd.lang.java.xpath.TypeOfFunction;
import net.sourceforge.pmd.lang.metrics.LanguageMetricsProvider;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
import net.sourceforge.pmd.util.designerbindings.DesignerBindings;
import net.sf.saxon.sxpath.IndependentContext;
@ -151,6 +153,10 @@ public abstract class AbstractJavaHandler extends AbstractLanguageVersionHandler
};
}
@Override
public DesignerBindings getDesignerBindings() {
return JavaDesignerBindings.INSTANCE;
}
@Override
public DFAGraphRule getDFAGraphRule() {

View File

@ -0,0 +1,96 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.internal;
import java.util.Collection;
import java.util.Collections;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.ast.xpath.Attribute;
import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTRecordConstructorDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.ast.JavaParserVisitor;
import net.sourceforge.pmd.lang.java.ast.JavaParserVisitorReducedAdapter;
import net.sourceforge.pmd.lang.java.ast.TypeNode;
import net.sourceforge.pmd.util.designerbindings.DesignerBindings.DefaultDesignerBindings;
public final class JavaDesignerBindings extends DefaultDesignerBindings {
public static final JavaDesignerBindings INSTANCE = new JavaDesignerBindings();
private JavaDesignerBindings() {
}
@Override
public Attribute getMainAttribute(Node node) {
if (node instanceof JavaNode) {
Attribute attr = (Attribute) ((JavaNode) node).jjtAccept(MainAttrVisitor.INSTANCE, null);
if (attr != null) {
return attr;
}
}
return super.getMainAttribute(node);
}
@Override
public TreeIconId getIcon(Node node) {
if (node instanceof ASTFieldDeclaration) {
return TreeIconId.FIELD;
} else if (node instanceof ASTAnyTypeDeclaration) {
return TreeIconId.CLASS;
} else if (node instanceof ASTMethodDeclaration) {
return TreeIconId.METHOD;
} else if (node instanceof ASTConstructorDeclaration
|| node instanceof ASTRecordConstructorDeclaration) {
return TreeIconId.CONSTRUCTOR;
} else if (node instanceof ASTVariableDeclaratorId) {
return TreeIconId.VARIABLE;
}
return super.getIcon(node);
}
@Override
public Collection<AdditionalInfo> getAdditionalInfo(Node node) {
if (node instanceof TypeNode) {
Class<?> type = ((TypeNode) node).getType();
if (type != null) {
return Collections.singletonList(new AdditionalInfo("Type: " + type));
}
}
return super.getAdditionalInfo(node);
}
private static final class MainAttrVisitor extends JavaParserVisitorReducedAdapter {
private static final JavaParserVisitor INSTANCE = new MainAttrVisitor();
private MainAttrVisitor() {
}
@Override
public Object visit(JavaNode node, Object data) {
return null; // don't recurse
}
@Override
public Object visit(ASTAnyTypeDeclaration node, Object data) {
return new Attribute(node, "SimpleName", node.getSimpleName());
}
@Override
public Object visit(ASTMethodDeclaration node, Object data) {
return new Attribute(node, "Name", node.getName());
}
}
}