Class
<?> clsMHandles
= Class.
forName("java.lang.invoke.MethodHandles");
Method mStaticLookup
= clsMHandles.
getMethod("lookup");
Object oStaticLookup
= mStaticLookup.
invoke(null);
Method mLookup = clsMHandles.getMethod("privateLookupIn", Class.class, Class.forName("java.lang.invoke.MethodHandles$Lookup"));
Object oLookup = mLookup.invoke(null, ClassLoader.class, oStaticLookup);
Method mFindStatic = oLookup.getClass().getMethod("findStaticVarHandle", Class.class, String.class, Class.class);
Object oVarHandle = mFindStatic.invoke(oLookup, ClassLoader.class, "usr_paths", String[].class);
//MethodHandle mh = MethodHandles.lookup().findVirtual(VarHandle.class, "get", MethodType.methodType(Object.class));
//mh.invoke(oVarHandle);
Method mFindVirtual = oStaticLookup.getClass().getMethod("findVirtual", Class.class, String.class, Class.forName("java.lang.invoke.MethodType"));
Class<?> clsMethodType = Class.forName("java.lang.invoke.MethodType");
Method mMethodType = clsMethodType.getMethod("methodType", Class.class);
Object oMethodHandleGet = mFindVirtual.invoke(oStaticLookup, Class.forName("java.lang.invoke.VarHandle"), "get", mMethodType.invoke(null, Object.class));
Method mMethodHandleGet = oMethodHandleGet.getClass().getMethod("invokeWithArguments", Object[].class);
String[] saPath = (String[])mMethodHandleGet.invoke(oMethodHandleGet, new Object[] {new Object[] {oVarHandle}});
//check if the path to add is already present
for (String path : saPath)
{
if (path.equals(pPath))
{
return;
}
}
//add the new path
final String[] saNewPaths = Arrays.copyOf(saPath, saPath.length + 1);
saNewPaths[saNewPaths.length - 1] = pPath;
//MethodHandle mh = MethodHandles.lookup().findVirtual(VarHandle.class, "set", MethodType.methodType(Void.class, Object[].class));
//mh.invoke(oVarHandle, new String[] {"GEHT"});
mMethodType = clsMethodType.getMethod("methodType", Class.class, Class.class);
Object oMethodHandleSet = mFindVirtual.invoke(oStaticLookup, Class.forName("java.lang.invoke.VarHandle"), "set", mMethodType.invoke(null, Void.class, Object[].class));
Method mMethodHandleSet = oMethodHandleSet.getClass().getMethod("invokeWithArguments", Object[].class);
mMethodHandleSet.invoke(oMethodHandleSet, new Object[] {new Object[] {oVarHandle, saNewPaths}});