-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve checking of access modifiers for methods
- Loading branch information
Showing
4 changed files
with
101 additions
and
48 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
jadx-core/src/main/java/jadx/core/dex/nodes/utils/AccessCheckUtils.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,85 @@ | ||
package jadx.core.dex.nodes.utils; | ||
|
||
import jadx.core.dex.info.AccessInfo; | ||
import jadx.core.dex.nodes.ClassNode; | ||
import jadx.core.dex.nodes.FieldNode; | ||
import jadx.core.dex.nodes.ICodeNode; | ||
import jadx.core.dex.nodes.MethodNode; | ||
import jadx.core.dex.nodes.RootNode; | ||
import jadx.core.utils.exceptions.JadxRuntimeException; | ||
|
||
class AccessCheckUtils { | ||
|
||
private final RootNode root; | ||
|
||
AccessCheckUtils(RootNode rootNode) { | ||
this.root = rootNode; | ||
} | ||
|
||
boolean isAccessible(ICodeNode targetNode, ICodeNode callerNode) { | ||
ClassNode targetCls = getDeclaringClass(targetNode); | ||
ClassNode callerCls = getDeclaringClass(callerNode); | ||
|
||
if (targetCls.equals(callerCls)) { | ||
return true; | ||
} | ||
|
||
AccessInfo targetVisibility; | ||
if (targetNode == targetCls) { | ||
targetVisibility = targetNode.getAccessFlags().getVisibility(); | ||
} else { | ||
AccessInfo targetClsVisibility = targetCls.getAccessFlags().getVisibility(); | ||
AccessInfo targetNodeVisibility = targetNode.getAccessFlags().getVisibility(); | ||
targetVisibility = targetClsVisibility.isVisibilityWeakerThan(targetNodeVisibility) | ||
? targetClsVisibility | ||
: targetNodeVisibility; | ||
} | ||
|
||
if (targetVisibility.isPublic()) { | ||
return true; | ||
} | ||
|
||
if (targetVisibility.isProtected()) { | ||
return isProtectedAccessible(targetCls, callerCls); | ||
} | ||
|
||
if (targetVisibility.isPackagePrivate()) { | ||
return isPackagePrivateAccessible(targetCls, callerCls); | ||
} | ||
|
||
if (targetVisibility.isPrivate()) { | ||
return isPrivateAccessible(targetCls, callerCls); | ||
} | ||
|
||
throw new JadxRuntimeException(targetVisibility + " is not supported"); | ||
} | ||
|
||
private ClassNode getDeclaringClass(ICodeNode node) { | ||
if (node instanceof ClassNode) { | ||
return (ClassNode) node; | ||
} else if (node instanceof MethodNode) { | ||
return ((MethodNode) node).getParentClass(); | ||
} else if (node instanceof FieldNode) { | ||
return ((FieldNode) node).getParentClass(); | ||
} else { | ||
throw new JadxRuntimeException(node + " is not supported"); | ||
} | ||
} | ||
|
||
private boolean isProtectedAccessible(ClassNode cls, ClassNode callerCls) { | ||
return isPackagePrivateAccessible(cls, callerCls) || isSuperType(cls, callerCls); | ||
} | ||
|
||
private boolean isPackagePrivateAccessible(ClassNode cls, ClassNode callerCls) { | ||
return cls.getPackageNode().equals(callerCls.getPackageNode()); | ||
} | ||
|
||
private boolean isPrivateAccessible(ClassNode cls, ClassNode callerCls) { | ||
return cls.getTopParentClass().equals(callerCls.getTopParentClass()); | ||
} | ||
|
||
private boolean isSuperType(ClassNode cls, ClassNode superCls) { | ||
return root.getClsp().getSuperTypes(cls.getRawName()).stream() | ||
.anyMatch(x -> x.equals(superCls.getRawName())); | ||
} | ||
} |
44 changes: 3 additions & 41 deletions
44
jadx-core/src/main/java/jadx/core/dex/nodes/utils/ClassUtils.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 |
---|---|---|
@@ -1,57 +1,19 @@ | ||
package jadx.core.dex.nodes.utils; | ||
|
||
import jadx.core.dex.info.AccessInfo; | ||
import jadx.core.dex.nodes.ClassNode; | ||
import jadx.core.dex.nodes.RootNode; | ||
import jadx.core.utils.exceptions.JadxRuntimeException; | ||
|
||
public class ClassUtils { | ||
|
||
private final RootNode root; | ||
private final AccessCheckUtils accessCheckUtils; | ||
|
||
public ClassUtils(RootNode rootNode) { | ||
this.root = rootNode; | ||
this.accessCheckUtils = new AccessCheckUtils(root); | ||
} | ||
|
||
public boolean isAccessible(ClassNode cls, ClassNode callerCls) { | ||
if (cls.equals(callerCls)) { | ||
return true; | ||
} | ||
|
||
final AccessInfo accessFlags = cls.getAccessFlags(); | ||
if (accessFlags.isPublic()) { | ||
return true; | ||
} | ||
|
||
if (accessFlags.isProtected()) { | ||
return isProtectedAccessible(cls, callerCls); | ||
} | ||
|
||
if (accessFlags.isPackagePrivate()) { | ||
return isPackagePrivateAccessible(cls, callerCls); | ||
} | ||
|
||
if (accessFlags.isPrivate()) { | ||
return isPrivateAccessible(cls, callerCls); | ||
} | ||
|
||
throw new JadxRuntimeException(accessFlags + " is not supported"); | ||
} | ||
|
||
private boolean isProtectedAccessible(ClassNode cls, ClassNode callerCls) { | ||
return isPackagePrivateAccessible(cls, callerCls) || isSuperType(cls, callerCls); | ||
} | ||
|
||
private boolean isPackagePrivateAccessible(ClassNode cls, ClassNode callerCls) { | ||
return cls.getPackageNode().equals(callerCls.getPackageNode()); | ||
} | ||
|
||
private boolean isPrivateAccessible(ClassNode cls, ClassNode callerCls) { | ||
return cls.getTopParentClass().equals(callerCls.getTopParentClass()); | ||
} | ||
|
||
private boolean isSuperType(ClassNode cls, ClassNode superCls) { | ||
return root.getClsp().getSuperTypes(cls.getRawName()).stream() | ||
.anyMatch(x -> x.equals(superCls.getRawName())); | ||
return accessCheckUtils.isAccessible(cls, callerCls); | ||
} | ||
} |
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
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