-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For static method calls and field accesses do not look into superclas…
…ses (#238)
- Loading branch information
1 parent
2b3e1e2
commit 666f2f1
Showing
7 changed files
with
139 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* (C) Copyright Uwe Schindler (Generics Policeman) and others. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* The binary class file is packaged together with the source distribution. | ||
*/ | ||
|
||
import java.util.BitSet; | ||
|
||
public class Java7StaticVsVirtual { | ||
public static final long[] data = new long[] {1, 2, 3, 4}; | ||
|
||
public static void main(String[] args) { | ||
X.valueOf(data).toString(); // Line 26 -- the only violation for static BitSet#valueOf(**) | ||
Y.valueOf(data).toString(); // Line 27 -- should pass | ||
Z.valueOf(data).toString(); // Line 28 -- should pass | ||
Integer.toString(Y.a); // Line 29 -- violation (static field access) | ||
Integer.toString(Z.a); // Line 30 -- should pass (hidden) | ||
new X().get(0); // Line 31 -- violation (virtual methods detected regardles how they are called) | ||
new Y().get(0); // Line 32 -- violation (virtual methods detected regardles how they are called) | ||
new Z().get(0); // Line 33 -- violation (virtual methods detected regardles how they are called) | ||
Integer.toString(new Y().b); // Line 34 -- violation | ||
Integer.toString(new Z().b); // Line 35 -- should pass (hidden) | ||
} | ||
|
||
public static class X extends BitSet { } | ||
|
||
public static class Y extends X { | ||
public static int a; | ||
public int b; | ||
|
||
public static BitSet valueOf(long[] longs) { | ||
return new BitSet(); | ||
} | ||
|
||
@Override | ||
public boolean get(int bit) { | ||
return false; | ||
} | ||
} | ||
|
||
public static class Z extends Y { | ||
public static int a; // hides field in superclass | ||
public int b; // hides field in superclass | ||
|
||
public String goStatic() { | ||
return valueOf(data).toString(); // Line 59 -- should pass | ||
} | ||
public boolean goVirtual() { | ||
return get(0); // Line 62 -- violation (virtual methods detected regardles how they are called) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
* (C) Copyright Uwe Schindler (Generics Policeman) and others. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
--> | ||
<project xmlns:au="antlib:org.apache.ant.antunit"> | ||
|
||
<target name="testStatic"> | ||
<au:expectfailure expectedMessage="Check for forbidden API calls failed, see log"> | ||
<forbiddenapis> | ||
<fileset file="Java7StaticVsVirtual*.class"/> | ||
java.util.BitSet#valueOf(**) @ Forbidden static method | ||
</forbiddenapis> | ||
</au:expectfailure> | ||
<au:assertLogContains level="error" text="Forbidden method invocation: java.util.BitSet#valueOf(**) [Forbidden static method]"/> | ||
<au:assertLogContains level="error" text="(Java7StaticVsVirtual.java:26)"/> | ||
<au:assertLogContains level="error" text=" 1 error(s)"/> | ||
</target> | ||
|
||
<target name="testVirtual"> | ||
<au:expectfailure expectedMessage="Check for forbidden API calls failed, see log"> | ||
<forbiddenapis> | ||
<fileset file="Java7StaticVsVirtual*.class"/> | ||
java.util.BitSet#get(int) @ Forbidden virtual method | ||
</forbiddenapis> | ||
</au:expectfailure> | ||
<au:assertLogContains level="error" text="Forbidden method invocation: java.util.BitSet#get(int) [Forbidden virtual method]"/> | ||
<au:assertLogContains level="error" text="(Java7StaticVsVirtual.java:31)"/> | ||
<au:assertLogContains level="error" text="(Java7StaticVsVirtual.java:32)"/> | ||
<au:assertLogContains level="error" text="(Java7StaticVsVirtual.java:33)"/> | ||
<au:assertLogContains level="error" text="(Java7StaticVsVirtual.java:62)"/> | ||
<au:assertLogContains level="error" text=" 4 error(s)"/> | ||
</target> | ||
|
||
<target name="testFields"> | ||
<au:expectfailure expectedMessage="Check for forbidden API calls failed, see log"> | ||
<forbiddenapis classpath="."> | ||
<fileset file="Java7StaticVsVirtual*.class"/> | ||
Java7StaticVsVirtual$Y#a @ Forbidden static field | ||
Java7StaticVsVirtual$Y#b @ Forbidden instance field | ||
</forbiddenapis> | ||
</au:expectfailure> | ||
<au:assertLogContains level="error" text="Forbidden field access: Java7StaticVsVirtual$Y#a [Forbidden static field]"/> | ||
<au:assertLogContains level="error" text="(Java7StaticVsVirtual.java:29)"/> | ||
<au:assertLogContains level="error" text="Forbidden field access: Java7StaticVsVirtual$Y#b [Forbidden instance field]"/> | ||
<au:assertLogContains level="error" text="(Java7StaticVsVirtual.java:34)"/> | ||
<au:assertLogContains level="error" text=" 2 error(s)"/> | ||
</target> | ||
|
||
</project> |