-
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 classes (PR #2251)
- Loading branch information
Showing
3 changed files
with
96 additions
and
29 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
57 changes: 57 additions & 0 deletions
57
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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; | ||
|
||
public ClassUtils(RootNode rootNode) { | ||
this.root = rootNode; | ||
} | ||
|
||
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())); | ||
} | ||
} |
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