Skip to content

Commit

Permalink
fix(java): References within InvocationHandler, Issue #1364 - optimiz…
Browse files Browse the repository at this point in the history
…ations from discussion
  • Loading branch information
cn-at-osmit committed Feb 8, 2024
1 parent 34edf49 commit bc5b56a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.fury.serializer;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.apache.fury.Fury;
import org.apache.fury.memory.MemoryBuffer;
Expand All @@ -33,16 +34,15 @@
public class JdkProxySerializer extends Serializer {

// Make offset compatible with graalvm native image.
private static final long PROXY_HANDLER_FIELD_OFFSET;

static {
try {
// Make offset compatible with graalvm native image.
PROXY_HANDLER_FIELD_OFFSET = Platform.objectFieldOffset(Proxy.class.getDeclaredField("h"));
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
}
private static final long PROXY_HANDLER_FIELD_OFFSET =
Platform.objectFieldOffset(Proxy.class, InvocationHandler.class);
private static final InvocationHandler STUB_HANDLER =
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
throw new IllegalStateException("Deserialization stub handler still active");
}
};

public JdkProxySerializer(Fury fury, Class cls) {
super(fury, cls);
Expand All @@ -63,8 +63,7 @@ public Object read(MemoryBuffer buffer) {
final int refId = resolver.lastPreservedRefId();
final Class<?>[] interfaces = (Class<?>[]) fury.readRef(buffer);
Preconditions.checkNotNull(interfaces);
final Class<?> proxyClass = Proxy.getProxyClass(fury.getClassLoader(), interfaces);
Object proxy = Platform.newInstance(proxyClass);
Object proxy = Proxy.newProxyInstance(fury.getClassLoader(), interfaces, STUB_HANDLER);
resolver.setReadObject(refId, proxy);
InvocationHandler invocationHandler = (InvocationHandler) fury.readRef(buffer);
Preconditions.checkNotNull(invocationHandler);
Expand Down
33 changes: 33 additions & 0 deletions java/fury-core/src/main/java/org/apache/fury/util/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,39 @@ public static long objectFieldOffset(Field f) {
return UNSAFE.objectFieldOffset(f);
}

/**
* Gets the offset of the only field in the class matching the required type
*
* @param clazz the class in which the field should be declared
* @param fieldType the required type of the field
* @return
* @throws IllegalStateException if there are multiple fields of the required type
* @throws IllegalArgumentException if there are no fields of the required type
*/
public static long objectFieldOffset(Class<?> clazz, Class<?> fieldType) {
Field f = null;
for (Field fi : clazz.getDeclaredFields()) {
if (fieldType.equals(fi.getType())) {
if (f != null)
throw new IllegalStateException(
"Found multiple field s matching "
+ fieldType
+ " in "
+ clazz
+ ": "
+ f
+ " and "
+ fi);

f = fi;
}
}
if (f == null)
throw new IllegalArgumentException(
"Found no field matching " + fieldType.getName() + " in " + clazz + "!");
return objectFieldOffset(f);
}

public static int getInt(Object object, long offset) {
return UNSAFE.getInt(object, offset);
}
Expand Down

0 comments on commit bc5b56a

Please sign in to comment.