-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
79 additions
and
10 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
api/src/main/java/io/github/karlatemp/unsafeaccessor/ProtectedObject.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,17 @@ | ||
package io.github.karlatemp.unsafeaccessor; | ||
|
||
class ProtectedObject { | ||
boolean trusted; | ||
|
||
void checkTrusted() { | ||
if (!trusted) throw new SecurityException(); | ||
} | ||
|
||
public ProtectedObject(boolean t) { | ||
this.trusted = t; | ||
} | ||
|
||
public ProtectedObject() { | ||
this(true); | ||
} | ||
} |
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
61 changes: 61 additions & 0 deletions
61
api/src/main/java/io/github/karlatemp/unsafeaccessor/UnsafeAccess.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,61 @@ | ||
package io.github.karlatemp.unsafeaccessor; | ||
|
||
import java.lang.invoke.MethodHandles; | ||
import java.lang.reflect.AccessibleObject; | ||
|
||
/** | ||
* Direct unsafe access | ||
* <p> | ||
* A bridge object to access unsafe. | ||
* <p> | ||
* Unlike other methods, using this bridge will not trigger any permission checks. | ||
* The permission check will only happen in {@link #getInstance()} | ||
* | ||
* @since 1.4.0 | ||
*/ | ||
public final class UnsafeAccess extends ProtectedObject { | ||
static final UnsafeAccess INSTANCE = new UnsafeAccess(); | ||
|
||
public static UnsafeAccess getInstance() { | ||
SecurityCheck.getInstance(); | ||
return INSTANCE; | ||
} | ||
|
||
public SecurityCheck getSecuritySettings() { | ||
checkTrusted(); | ||
return SecurityCheck.INSTANCE; | ||
} | ||
|
||
public Unsafe getUnsafe() { | ||
checkTrusted(); | ||
return Unsafe.getUnsafe0(); | ||
} | ||
|
||
/** | ||
* Use {@link #getTrustedIn(Class)} | ||
* | ||
* @return MethodHandles.Lookup.IMPL_LOOKUP | ||
*/ | ||
@Deprecated | ||
public MethodHandles.Lookup getTrusted() { | ||
checkTrusted(); | ||
return Root.RootLookupHolder.ROOT; | ||
} | ||
|
||
public MethodHandles.Lookup getTrustedIn(Class<?> target) { | ||
checkTrusted(); | ||
return Root.RootLookupHolder.trustedIn(target); | ||
} | ||
|
||
public <T extends AccessibleObject> T openAccess(T object) { | ||
checkTrusted(); | ||
Root.OpenAccess.openAccess0(object, true); | ||
return object; | ||
} | ||
|
||
public <T extends AccessibleObject> T setAccessible(T object, boolean accessible) { | ||
checkTrusted(); | ||
Root.OpenAccess.openAccess0(object, accessible); | ||
return object; | ||
} | ||
} |