Skip to content

Commit a6e1dd0

Browse files
committed
Restore logic for fields
1 parent f14c90f commit a6e1dd0

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/main/java/de/thetaphi/forbiddenapis/ClassScanner.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ public String visit(ClassMetadata c, String origName, boolean isInterfaceOfAnces
434434
}, true, false /* JVM spec says: interfaces after superclasses */);
435435
}
436436

437-
private String checkFieldAccess(String owner, final String field, final boolean callIsVirtual) {
437+
private String checkFieldAccess(String owner, final String field) {
438438
String violation = checkClassUse(owner, "class/interface", owner);
439439
if (violation != null) {
440440
return violation;
@@ -454,10 +454,9 @@ public String visit(ClassMetadata c, String origName, boolean isInterfaceOfAnces
454454
if (!c.fields.contains(field)) {
455455
return null;
456456
}
457-
// is we have a virtual call, look into superclasses, otherwise stop:
458-
final String notFoundRet = callIsVirtual ? null : AncestorVisitor.STOP;
457+
// we found the field: from now on we use STOP to exit, because fields are not virtual!
459458
if (previousInRuntime && c.isNonPortableRuntime) {
460-
return notFoundRet; // something inside the JVM is extending internal class/interface
459+
return STOP; // something inside the JVM is extending internal class/interface
461460
}
462461
String violation = forbiddenSignatures.checkField(c.className, field);
463462
if (violation != null) {
@@ -470,7 +469,8 @@ public String visit(ClassMetadata c, String origName, boolean isInterfaceOfAnces
470469
return violation;
471470
}
472471
}
473-
return notFoundRet;
472+
// we found the field and as those are not virtual, there is no need to go up in class hierarchy:
473+
return STOP;
474474
}
475475
}, true, true /* JVM spec says: superclasses after interfaces */);
476476
}
@@ -479,10 +479,9 @@ private String checkHandle(Handle handle, boolean checkLambdaHandle) {
479479
switch (handle.getTag()) {
480480
case Opcodes.H_GETFIELD:
481481
case Opcodes.H_PUTFIELD:
482-
return checkFieldAccess(handle.getOwner(), handle.getName(), true);
483482
case Opcodes.H_GETSTATIC:
484483
case Opcodes.H_PUTSTATIC:
485-
return checkFieldAccess(handle.getOwner(), handle.getName(), false);
484+
return checkFieldAccess(handle.getOwner(), handle.getName());
486485
case Opcodes.H_INVOKEVIRTUAL:
487486
case Opcodes.H_INVOKESTATIC:
488487
case Opcodes.H_INVOKESPECIAL:
@@ -560,8 +559,7 @@ public void visitMethodInsn(int opcode, String owner, String name, String desc,
560559

561560
@Override
562561
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
563-
final boolean callIsVirtual = (opcode == Opcodes.GETFIELD) || (opcode == Opcodes.PUTFIELD);
564-
reportMethodViolation(checkFieldAccess(owner, name, callIsVirtual), "method body");
562+
reportMethodViolation(checkFieldAccess(owner, name), "method body");
565563
}
566564

567565
@Override

0 commit comments

Comments
 (0)