Merge pull request #10191 from smowton/smowton/admin/java-implicit-this-type-tests

Java: Add test regarding the type of an implicit `this` expression
This commit is contained in:
Anders Schack-Mulligen
2022-09-16 10:58:48 +02:00
committed by GitHub
19 changed files with 262 additions and 9 deletions

View File

@@ -0,0 +1,2 @@
| GenericTest.java:14:7:14:9 | f(...) | A $@ is called instead of a $@. | GenericTest.class:0:0:0:0 | f | method declared in a superclass | GenericTest.java:9:8:9:8 | f | method with the same signature in an enclosing class |
| Test.java:14:7:14:9 | f(...) | A $@ is called instead of a $@. | Test.java:3:8:3:8 | f | method declared in a superclass | Test.java:9:8:9:8 | f | method with the same signature in an enclosing class |

View File

@@ -0,0 +1 @@
Violations of Best Practice/Naming Conventions/AmbiguousOuterSuper.ql

View File

@@ -0,0 +1,19 @@
public class GenericTest<T> {
void f() { }
}
class Outer2 {
void f() { }
class Inner<T> extends GenericTest<T> {
public void test() {
f();
}
}
}

View File

@@ -0,0 +1,19 @@
public class Test {
void f() { }
}
class Outer {
void f() { }
class Inner extends Test {
public void test() {
f();
}
}
}

View File

@@ -0,0 +1,7 @@
| ExposesRep.java:11:19:11:28 | getStrings | getStrings exposes the internal representation stored in field strings. The value may be modified $@. | User.java:5:5:5:19 | User.java:5:5:5:19 | after this call to getStrings |
| ExposesRep.java:11:19:11:28 | getStrings | getStrings exposes the internal representation stored in field strings. The value may be modified $@. | User.java:13:12:13:26 | User.java:13:12:13:26 | after this call to getStrings |
| ExposesRep.java:11:19:11:28 | getStrings | getStrings exposes the internal representation stored in field strings. The value may be modified $@. | User.java:38:12:38:26 | User.java:38:12:38:26 | after this call to getStrings |
| ExposesRep.java:13:30:13:41 | getStringMap | getStringMap exposes the internal representation stored in field stringMap. The value may be modified $@. | User.java:9:5:9:21 | User.java:9:5:9:21 | after this call to getStringMap |
| ExposesRep.java:17:15:17:24 | setStrings | setStrings exposes the internal representation stored in field strings. The value may be modified $@. | User.java:22:5:22:6 | User.java:22:5:22:6 | through the variable ss |
| ExposesRep.java:21:15:21:26 | setStringMap | setStringMap exposes the internal representation stored in field stringMap. The value may be modified $@. | User.java:27:5:27:5 | User.java:27:5:27:5 | through the variable m |
| ExposesRep.java:29:14:29:21 | getArray | getArray exposes the internal representation stored in field array. The value may be modified $@. | User.java:31:5:31:18 | User.java:31:5:31:18 | after this call to getArray |

View File

@@ -0,0 +1 @@
Violations of Best Practice/Implementation Hiding/ExposeRepresentation.ql

View File

@@ -0,0 +1,30 @@
import java.util.Map;
public class ExposesRep {
private String[] strings;
private Map<String, String> stringMap;
public ExposesRep() {
strings = new String[1];
}
public String[] getStrings() { return strings; }
public Map<String, String> getStringMap() {
return stringMap;
}
public void setStrings(String[] ss) {
this.strings = ss;
}
public void setStringMap(Map<String, String> m) {
this.stringMap = m;
}
}
class GenericExposesRep<T> {
private T[] array;
public T[] getArray() { return array; }
}

View File

@@ -0,0 +1,45 @@
import java.util.Map;
public class User {
public static void test1(ExposesRep er) {
er.getStrings()[0] = "Hello world";
}
public static void test2(ExposesRep er) {
er.getStringMap().put("Hello", "world");
}
public String[] indirectGetStrings(ExposesRep er) {
return er.getStrings();
}
public void test3(ExposesRep er) {
indirectGetStrings(er)[0] = "Hello world";
}
public static void test4(ExposesRep er, String[] ss) {
er.setStrings(ss);
ss[0] = "Hello world";
}
public static void test5(ExposesRep er, Map<String, String> m) {
er.setStringMap(m);
m.put("Hello", "world");
}
public static void test6(GenericExposesRep<String> ger) {
ger.getArray()[0] = "Hello world";
}
}
class GenericUser<T> {
public String[] indirectGetStrings(ExposesRep er) {
return er.getStrings();
}
public static void test1(ExposesRep er, GenericUser<String> gu) {
gu.indirectGetStrings(er)[0] = "Hello world";
}
}

View File

@@ -1,2 +1,3 @@
| Test.java:16:5:16:17 | remove(...) | This call may fail when iterating over the collection created $@, since it does not support element removal. | Test.java:29:16:29:32 | asList(...) | here |
| Test.java:16:5:16:17 | remove(...) | This call may fail when iterating over the collection created $@, since it does not support element removal. | Test.java:33:16:33:43 | singletonList(...) | here |
| Test.java:44:3:44:23 | remove(...) | This call may fail when iterating over the collection created $@, since it does not support element removal. | Test.java:52:15:52:31 | asList(...) | here |

View File

@@ -36,4 +36,20 @@ class A {
public List<Integer> getL() {
return l;
}
}
class Parent<T> {
public void removeFirst(List<T> l) {
l.iterator().remove();
}
}
class Child extends Parent<String> {
public void test(String... ss) {
removeFirst(Arrays.asList(ss));
}
}