-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Classes should be able to access their own fields and methods #17
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
invoker.goals=clean package |
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,36 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>test</groupId> | ||
<artifactId>this-instance-impl</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.kohsuke</groupId> | ||
<artifactId>access-modifier-annotation</artifactId> | ||
<version>@project.version@</version> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.kohsuke</groupId> | ||
<artifactId>access-modifier-checker</artifactId> | ||
<version>@project.version@</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>enforce</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
11 changes: 11 additions & 0 deletions
11
access-modifier-checker/src/it/own-members/src/main/java/Outer.java
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,11 @@ | ||
import org.kohsuke.accmod.Restricted; | ||
import org.kohsuke.accmod.restrictions.DoNotUse; | ||
|
||
class Outer { | ||
@Restricted(DoNotUse.class) | ||
static class Middle { | ||
static class Inner { | ||
static {new Middle();} | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
access-modifier-checker/src/it/own-members/src/main/java/SomeClass.java
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,18 @@ | ||
import org.kohsuke.accmod.Restricted; | ||
import org.kohsuke.accmod.restrictions.DoNotUse; | ||
|
||
@Restricted(DoNotUse.class) | ||
public class SomeClass { | ||
private int foo; | ||
|
||
public SomeClass() { | ||
foo = 12; | ||
} | ||
|
||
public int getFoo() { | ||
doSomething(); | ||
return foo; | ||
} | ||
|
||
private void doSomething() {} | ||
} |
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 |
---|---|---|
|
@@ -344,7 +344,7 @@ public MethodVisitor visitMethod(int access, String name, String desc, String si | |
return null; | ||
} | ||
|
||
return new RestrictedMethodVisitor(currentLocation, annotationVisitor.getSkippedTypes()); | ||
return new RestrictedMethodVisitor(currentLocation, className, annotationVisitor.getSkippedTypes()); | ||
} | ||
|
||
@Override | ||
|
@@ -388,24 +388,38 @@ public String getProperty(String key) { | |
}; | ||
} | ||
|
||
private static String topLevelClass(String a) { | ||
int i = a.indexOf('$'); | ||
if (i == -1) { | ||
return a; | ||
} | ||
return a.substring(0, i); | ||
} | ||
|
||
private static boolean sameClassFile(String currentClass, String owner) { | ||
return topLevelClass(currentClass).equals(topLevelClass(owner)); | ||
} | ||
|
||
private class RestrictedMethodVisitor extends MethodVisitor { | ||
|
||
private final Set<Type> skippedTypesFromParent; | ||
private Location currentLocation; | ||
private RestrictedAnnotationVisitor annotationVisitor = new RestrictedAnnotationVisitor(); | ||
private final String currentClass; | ||
|
||
private Set<Type> getSkippedTypes() { | ||
Set<Type> allSkippedTypes = new HashSet<>(skippedTypesFromParent); | ||
allSkippedTypes.addAll(annotationVisitor.getSkippedTypes()); | ||
return allSkippedTypes; | ||
} | ||
|
||
public RestrictedMethodVisitor(Location currentLocation, Set<Type> skippedTypes) { | ||
public RestrictedMethodVisitor(Location currentLocation, String currentClass, Set<Type> skippedTypes) { | ||
super(Opcodes.ASM9); | ||
log.debug(String.format("New method visitor at %s#%s", | ||
currentLocation.getClassName(), currentLocation.getMethodName())); | ||
this.currentLocation = currentLocation; | ||
this.skippedTypesFromParent = skippedTypes; | ||
this.currentClass = currentClass; | ||
} | ||
|
||
@Override | ||
|
@@ -416,6 +430,10 @@ public void visitLineNumber(int _line, Label start) { | |
public void visitTypeInsn(int opcode, String type) { | ||
switch (opcode) { | ||
case Opcodes.NEW: | ||
if (sameClassFile(currentClass, type)) { | ||
return; | ||
} | ||
|
||
for (Restrictions r : getRestrictions(type, getSkippedTypes())) { | ||
r.instantiated(currentLocation, errorListener); | ||
} | ||
|
@@ -425,6 +443,11 @@ public void visitTypeInsn(int opcode, String type) { | |
@Override | ||
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) { | ||
log.debug(String.format("Visiting method %s#%s", owner, name)); | ||
|
||
if (sameClassFile(currentClass, owner)) { | ||
return; | ||
} | ||
|
||
for (Restrictions r : getRestrictions(owner + '.' + name + desc, getSkippedTypes())) { | ||
r.invoked(currentLocation, errorListener); | ||
} | ||
|
@@ -434,6 +457,10 @@ public void visitMethodInsn(int opcode, String owner, String name, String desc, | |
public void visitFieldInsn(int opcode, String owner, String name, String desc) { | ||
log.debug(String.format("Visiting field '%s %s' in type %s", desc, name, owner)); | ||
|
||
if (sameClassFile(currentClass, owner)) { | ||
return; | ||
} | ||
|
||
Iterable<Restrictions> rs = getRestrictions(owner + '.' + name, getSkippedTypes()); | ||
switch (opcode) { | ||
case Opcodes.GETSTATIC: | ||
|
@@ -449,7 +476,6 @@ public void visitFieldInsn(int opcode, String owner, String name, String desc) { | |
} | ||
break; | ||
} | ||
super.visitFieldInsn(opcode, owner, name, desc); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
@Override | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the way this is not technically correct since
$
is a legal identifier character, but in practice it is so rarely used that I think this is fine.