From a8c6ef9717e8867ce0c17c6fbb5bd9f0a85411b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= Date: Fri, 22 Mar 2019 13:39:28 +0100 Subject: [PATCH] class should access their own fields and methods --- .../src/it/own-members/invoker.properties | 1 + .../src/it/own-members/pom.xml | 36 +++++++++++++++++++ .../own-members/src/main/java/SomeClass.java | 18 ++++++++++ .../java/org/kohsuke/accmod/impl/Checker.java | 16 +++++++-- 4 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 access-modifier-checker/src/it/own-members/invoker.properties create mode 100644 access-modifier-checker/src/it/own-members/pom.xml create mode 100644 access-modifier-checker/src/it/own-members/src/main/java/SomeClass.java diff --git a/access-modifier-checker/src/it/own-members/invoker.properties b/access-modifier-checker/src/it/own-members/invoker.properties new file mode 100644 index 0000000..fff78ee --- /dev/null +++ b/access-modifier-checker/src/it/own-members/invoker.properties @@ -0,0 +1 @@ +invoker.goals=clean package diff --git a/access-modifier-checker/src/it/own-members/pom.xml b/access-modifier-checker/src/it/own-members/pom.xml new file mode 100644 index 0000000..ad7ddae --- /dev/null +++ b/access-modifier-checker/src/it/own-members/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + test + this-instance-impl + 1.0-SNAPSHOT + jar + + UTF-8 + 1.8 + 1.8 + + + + org.kohsuke + access-modifier-annotation + @project.version@ + + + + + + org.kohsuke + access-modifier-checker + @project.version@ + + + + enforce + + + + + + + diff --git a/access-modifier-checker/src/it/own-members/src/main/java/SomeClass.java b/access-modifier-checker/src/it/own-members/src/main/java/SomeClass.java new file mode 100644 index 0000000..98165d9 --- /dev/null +++ b/access-modifier-checker/src/it/own-members/src/main/java/SomeClass.java @@ -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() {} +} diff --git a/access-modifier-checker/src/main/java/org/kohsuke/accmod/impl/Checker.java b/access-modifier-checker/src/main/java/org/kohsuke/accmod/impl/Checker.java index 42160c8..13d4cdf 100644 --- a/access-modifier-checker/src/main/java/org/kohsuke/accmod/impl/Checker.java +++ b/access-modifier-checker/src/main/java/org/kohsuke/accmod/impl/Checker.java @@ -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 @@ -388,6 +388,7 @@ private class RestrictedMethodVisitor extends MethodVisitor { private final Set skippedTypesFromParent; private Location currentLocation; private RestrictedAnnotationVisitor annotationVisitor = new RestrictedAnnotationVisitor(); + private final String currentClass; private Set getSkippedTypes() { Set allSkippedTypes = new HashSet<>(skippedTypesFromParent); @@ -395,12 +396,13 @@ private Set getSkippedTypes() { return allSkippedTypes; } - public RestrictedMethodVisitor(Location currentLocation, Set skippedTypes) { + public RestrictedMethodVisitor(Location currentLocation, String currentClass, Set 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 @@ -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); } @@ -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 rs = getRestrictions(owner + '.' + name, getSkippedTypes()); switch (opcode) { case Opcodes.GETSTATIC: @@ -444,7 +455,6 @@ public void visitFieldInsn(int opcode, String owner, String name, String desc) { } break; } - super.visitFieldInsn(opcode, owner, name, desc); } @Override