Skip to content

Commit

Permalink
class should access their own fields and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
t-8ch committed Mar 22, 2019
1 parent 78544cc commit a8c6ef9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals=clean package
36 changes: 36 additions & 0 deletions access-modifier-checker/src/it/own-members/pom.xml
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>
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() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,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
Expand Down Expand Up @@ -388,19 +388,21 @@ 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.ASM5);
log.debug(String.format("New method visitor at %s#%s",
currentLocation.getClassName(), currentLocation.getMethodName()));
this.currentLocation = currentLocation;
this.skippedTypesFromParent = skippedTypes;
this.currentClass = currentClass;
}

@Override
Expand All @@ -420,6 +422,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 (currentClass.equals(owner)) {
return;
}

for (Restrictions r : getRestrictions(owner + '.' + name + desc, getSkippedTypes())) {
r.invoked(currentLocation, errorListener);
}
Expand All @@ -429,6 +436,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 (currentClass.equals(owner)) {
return;
}

Iterable<Restrictions> rs = getRestrictions(owner + '.' + name, getSkippedTypes());
switch (opcode) {
case Opcodes.GETSTATIC:
Expand All @@ -444,7 +455,6 @@ public void visitFieldInsn(int opcode, String owner, String name, String desc) {
}
break;
}
super.visitFieldInsn(opcode, owner, name, desc);
}

@Override
Expand Down

0 comments on commit a8c6ef9

Please sign in to comment.