Merge branch 'pr-2685' into master

[java] Fix NoClassDefFoundErrors #2685
This commit is contained in:
Andreas Dangel
2020-08-21 10:42:57 +02:00
6 changed files with 62 additions and 26 deletions

View File

@ -8,9 +8,12 @@ package net.sourceforge.pmd.lang.java.ast.internal;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
@ -19,11 +22,13 @@ import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
* Helper class to analyze {@link ASTImportDeclaration}s.
*/
public class ImportWrapper {
private ASTImportDeclaration node;
private String name;
private String fullname;
private boolean isStaticDemand;
private Set<String> allDemands = new HashSet<>();
private static final Logger LOG = Logger.getLogger(ImportWrapper.class.getName());
private final ASTImportDeclaration node;
private final String name;
private final String fullname;
private final boolean isStaticDemand;
private final Set<String> allStaticDemands;
public ImportWrapper(String fullname, String name) {
this(fullname, name, null);
@ -38,27 +43,38 @@ public class ImportWrapper {
this.name = name;
this.node = node;
this.isStaticDemand = isStaticDemand;
this.allStaticDemands = collectStaticFieldsAndMethods(node);
if (node != null && node.getType() != null) {
}
/**
* @param node
*/
private Set<String> collectStaticFieldsAndMethods(ASTImportDeclaration node) {
if (!this.isStaticDemand || node == null || node.getType() == null) {
return Collections.emptySet();
}
try {
Set<String> names = new HashSet<>();
Class<?> type = node.getType();
for (Method m : type.getMethods()) {
allDemands.add(m.getName());
}
for (Field f : type.getFields()) {
allDemands.add(f.getName());
}
// also consider static fields, that are not public
// consider static fields, public and non-public
for (Field f : type.getDeclaredFields()) {
if (Modifier.isStatic(f.getModifiers())) {
allDemands.add(f.getName());
names.add(f.getName());
}
}
// and methods, too
for (Method m : type.getDeclaredMethods()) {
if (Modifier.isStatic(m.getModifiers())) {
allDemands.add(m.getName());
names.add(m.getName());
}
}
return names;
} catch (LinkageError e) {
// This is an incomplete classpath, report the missing class
LOG.log(Level.FINE, "Possible incomplete auxclasspath: Error while processing imports", e);
return Collections.emptySet();
}
}
@ -86,7 +102,7 @@ public class ImportWrapper {
public boolean matches(ImportWrapper i) {
if (isStaticDemand) {
if (allDemands.contains(i.fullname)) {
if (allStaticDemands.contains(i.fullname)) {
return true;
}
}

View File

@ -15,6 +15,7 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression;
@ -131,7 +132,8 @@ public class MissingOverrideRule extends AbstractJavaRule {
return new MethodLookup(result, overridden);
} catch (final LinkageError e) {
// we may have an incomplete auxclasspath
// This is an incomplete classpath, report the missing class
LOG.log(Level.FINE, "Possible incomplete auxclasspath: Error while processing methods", e);
return null;
}
}

View File

@ -8,6 +8,8 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
@ -15,6 +17,7 @@ import net.sourceforge.pmd.lang.java.ast.internal.ImportWrapper;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
public class DuplicateImportsRule extends AbstractJavaRule {
private static final Logger LOG = Logger.getLogger(DuplicateImportsRule.class.getName());
private Set<ImportWrapper> singleTypeImports;
private Set<ImportWrapper> importOnDemandImports;
@ -68,11 +71,16 @@ public class DuplicateImportsRule extends AbstractJavaRule {
} else {
Class<?> importClass = node.getClassTypeResolver().loadClassOrNull(thisImportOnDemand.getName());
if (importClass != null) {
for (Method m : importClass.getMethods()) {
if (Modifier.isStatic(m.getModifiers()) && m.getName().equals(singleTypeName)) {
// static method in another imported class
return true;
try {
for (Method m : importClass.getMethods()) {
if (Modifier.isStatic(m.getModifiers()) && m.getName().equals(singleTypeName)) {
// static method in another imported class
return true;
}
}
} catch (LinkageError e) {
// This is an incomplete classpath, report the missing class
LOG.log(Level.FINE, "Possible incomplete auxclasspath: Error while processing methods", e);
}
}
}

View File

@ -11,6 +11,8 @@ import java.util.List;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang3.StringUtils;
@ -32,6 +34,7 @@ import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
import net.sourceforge.pmd.lang.symboltable.Scope;
public class UnnecessaryFullyQualifiedNameRule extends AbstractJavaRule {
private static final Logger LOG = Logger.getLogger(UnnecessaryFullyQualifiedNameRule.class.getName());
private List<ASTImportDeclaration> imports = new ArrayList<>();
private String currentPackage;
@ -335,10 +338,15 @@ public class UnnecessaryFullyQualifiedNameRule extends AbstractJavaRule {
// We need type resolution to make sure there is a
// conflicting method
if (importDeclaration.getType() != null) {
for (final Method m : importDeclaration.getType().getMethods()) {
if (m.getName().equals(methodCalled)) {
return true;
try {
for (final Method m : importDeclaration.getType().getMethods()) {
if (m.getName().equals(methodCalled)) {
return true;
}
}
} catch (LinkageError e) {
// This is an incomplete classpath, report the missing class
LOG.log(Level.FINE, "Possible incomplete auxclasspath: Error while processing methods", e);
}
}
} else if (importDeclaration.getImportedName().endsWith(methodCalled)) {

View File

@ -475,8 +475,9 @@ public final class MethodTypeResolution {
result.add(getTypeDefOfMethod(context, method, typeArguments));
}
}
} catch (final LinkageError ignored) {
// TODO : This is an incomplete classpath, report the missing class
} catch (final LinkageError e) {
// This is an incomplete classpath, report the missing class
LOG.log(Level.FINE, "Possible incomplete auxclasspath: Error while processing methods", e);
}
// search it's supertype