Cleanup, find new failing test case
This commit is contained in:
1 parent
a867c86ffb
commit
0c4cdd8973
3 files changed
+31
-30
No files matched your search
@@ -278,9 +278,6 @@ final class PolyResolution {
|
||||
|| e instanceof InvocationNode;
|
||||
}
|
||||
|
||||
// Some symbol is not resolved
|
||||
// go backwards from the context to get it.
|
||||
|
||||
/**
|
||||
* Fallback for some standalone expressions, that may use some context
|
||||
* to set their type. This must not trigger any type inference process
|
||||
@@ -288,6 +285,9 @@ final class PolyResolution {
|
||||
* context, that context must not be called.
|
||||
*/
|
||||
JTypeMirror getContextTypeForStandaloneFallback(ASTExpression e) {
|
||||
// Some symbol is not resolved
|
||||
// go backwards from the context to get it.
|
||||
|
||||
// The case mentioned by the doc is removed. We could be smarter
|
||||
// with how we retry failed invocation resolution, see history
|
||||
// of this comment
|
||||
@@ -556,7 +556,7 @@ final class PolyResolution {
|
||||
// means the "node" is a "stop recursion because no context" result in contextOf
|
||||
return false;
|
||||
} else if (isPreJava8()) {
|
||||
// in java < 8, context doesn't go flow through ternaries
|
||||
// in java < 8, context doesn't flow through ternaries
|
||||
return false;
|
||||
} else if (!internalUse
|
||||
&& node instanceof ASTConditionalExpression
|
||||
|
||||
+12
-16
@@ -4,7 +4,6 @@
|
||||
|
||||
package net.sourceforge.pmd.lang.java.rule.codestyle;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@@ -125,8 +124,7 @@ public class UseDiamondOperatorRule extends AbstractJavaRulechainRule {
|
||||
// this may not mutate the AST
|
||||
JavaExprMirrors factory = JavaExprMirrors.forObservation(infer);
|
||||
CtorInvocationMirror baseMirror = (CtorInvocationMirror) factory.getTopLevelInvocationMirror(ctorCall);
|
||||
JTypeMirror newType = ctorCall.getTypeNode().getTypeMirror();
|
||||
return new SpyInvocMirror(baseMirror, (JClassType) newType);
|
||||
return new SpyInvocMirror(baseMirror);
|
||||
}
|
||||
|
||||
|
||||
@@ -169,24 +167,17 @@ public class UseDiamondOperatorRule extends AbstractJavaRulechainRule {
|
||||
private static final class SpyInvocMirror implements CtorInvocationMirror {
|
||||
|
||||
private final CtorInvocationMirror base;
|
||||
private final JClassType modifiedNewType;
|
||||
|
||||
SpyInvocMirror(CtorInvocationMirror base, JClassType baseNewType) {
|
||||
SpyInvocMirror(CtorInvocationMirror base) {
|
||||
this.base = base;
|
||||
// see doc of CtorInvocationMirror#getNewType
|
||||
this.modifiedNewType = baseNewType.getGenericTypeDeclaration();
|
||||
}
|
||||
|
||||
// overridden methods
|
||||
|
||||
@Override
|
||||
public void setInferredType(JTypeMirror mirror) {
|
||||
// do nothing, we shouldn't affect the AST from here
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull JTypeMirror getNewType() {
|
||||
return modifiedNewType;
|
||||
// see doc of CtorInvocationMirror#getNewType
|
||||
return ((JClassType) base.getNewType()).getGenericTypeDeclaration();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -194,17 +185,22 @@ public class UseDiamondOperatorRule extends AbstractJavaRulechainRule {
|
||||
return true; // pretend it is
|
||||
}
|
||||
|
||||
// delegated methods
|
||||
|
||||
@Override
|
||||
public List<JTypeMirror> getExplicitTypeArguments() {
|
||||
return Collections.emptyList(); // pretend they're not there
|
||||
return base.getExplicitTypeArguments();
|
||||
}
|
||||
|
||||
@Override
|
||||
public JavaNode getExplicitTargLoc(int i) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
return base.getExplicitTargLoc(i);
|
||||
}
|
||||
|
||||
// delegated methods
|
||||
@Override
|
||||
public void setInferredType(JTypeMirror mirror) {
|
||||
base.setInferredType(mirror);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCtDecl(MethodCtDecl methodType) {
|
||||
|
||||
+15
-10
@@ -292,9 +292,6 @@ public class UseDiamondOperatorFalseNegative {
|
||||
<description>Java 7 invocation context</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Generic<T> {
|
||||
<E> Generic<E> method(Generic<E> e) { return e; }
|
||||
public Generic<T> test() {
|
||||
@@ -308,14 +305,26 @@ public class Generic<T> {
|
||||
<description>Java 8+ invocation context</description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Generic<T> {
|
||||
<E> Generic<E> method(Generic<E> e) { return e; }
|
||||
public Generic<T> test() {
|
||||
return method(new Generic<T>());
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description>Java 8+ invocation context, no target</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Generic<T> {
|
||||
<E> Generic<E> method(Generic<E> e) { return e; }
|
||||
public Generic<T> test() {
|
||||
// todo need to create spy,
|
||||
// ask someone (PolyResolution?) to create enclosing mirrors,
|
||||
// then hand that to Infer, then unwrap
|
||||
var m = method(new Generic<T>());
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
@@ -327,8 +336,6 @@ public class Generic<T> {
|
||||
</expected-messages>
|
||||
<code><![CDATA[
|
||||
package p;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Generic<T> {
|
||||
<E> Generic<E> method(Generic<E> e) { return e; }
|
||||
@@ -347,8 +354,6 @@ public class Generic<T> {
|
||||
</expected-messages>
|
||||
<code><![CDATA[
|
||||
package p;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Generic<T> {
|
||||
static class Inner<K> {}
|
||||
|
||||
Reference in new issue
Block a user