Affects PMD Version:
6.0.0
Rule:
Description:
A valid '<>' (diamond operator) in java 1.8 is misrecognized as anonymous inner class and a ParseException occurs.
Code Sample demonstrating the issue:
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.LayerUI;
import javax.swing.plaf.basic.BasicComboBoxEditor;
public class PMDExceptionTest {
private Component makeUI() {
String[] model = {"123456", "7890"};
JComboBox<String> comboBox = new JComboBox<>(model);
comboBox.setEditable(true);
comboBox.setEditor(new BasicComboBoxEditor() {
private Component editorComponent;
@Override public Component getEditorComponent() {
if (editorComponent == null) {
JTextField tc = (JTextField) super.getEditorComponent();
editorComponent = new JLayer<>(tc, new ValidationLayerUI<>());
}
return editorComponent;
}
});
JPanel p = new JPanel();
p.add(comboBox);
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new PMDExceptionTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
class ValidationLayerUI<V extends JTextField> extends LayerUI<V> {
@Override public void paint(Graphics g, JComponent c) {
super.paint(g, c);
// paint an icon and so on but omit it
}
}
$ "%JAVA_HOME%/bin/javac" -version
javac 1.8.0_162
$ "%JAVA_HOME%/bin/javac" src/PMDExceptionTest.java
$ dir src
'PMDExceptionTest$1.class' PMDExceptionTest.class PMDExceptionTest.java ValidationLayerUI.class
$ "%PMD_HOME%/bin/pmd.bat" -debug -d ./src -R category/java/bestpractices.xml -language java -version 1.8
Loaded rule ...
...
Processing C:\temp\src\PMDExceptionTest.java
Error while processing file: C:\temp\src\PMDExceptionTest.java
net.sourceforge.pmd.PMDException: Error while parsing C:\temp\src\PMDExceptionTest.java
at net.sourceforge.pmd.SourceCodeProcessor.processSourceCode(SourceCodeProcessor.java:98)
at net.sourceforge.pmd.SourceCodeProcessor.processSourceCode(SourceCodeProcessor.java:50)
at net.sourceforge.pmd.processor.PmdRunnable.call(PmdRunnable.java:75)
at net.sourceforge.pmd.processor.PmdRunnable.call(PmdRunnable.java:23)
at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: net.sourceforge.pmd.lang.java.ast.ParseException: Line 21, Column 5: Cannot use '<>' with anonymous inner classes when running in JDK inferior to 9 mode!
at net.sourceforge.pmd.lang.java.ast.JavaParser.throwParseException(JavaParser.java:23)
at net.sourceforge.pmd.lang.java.ast.JavaParser.checkForBadAnonymousDiamondUsage(JavaParser.java:139)
at net.sourceforge.pmd.lang.java.ast.JavaParser.AllocationExpression(JavaParser.java:4519)
at net.sourceforge.pmd.lang.java.ast.JavaParser.PrimaryPrefix(JavaParser.java:3922)
Running PMD through: [CLI]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4