-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
99 additions
and
48 deletions.
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
85 changes: 85 additions & 0 deletions
85
jadx-core/src/main/java/jadx/core/dex/nodes/utils/CodeNodeUtils.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 CodeNodeUtils { | ||
|
||
private final RootNode root; | ||
|
||
CodeNodeUtils(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())); | ||
} | ||
} |
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