Skip to content

Commit

Permalink
fix: JVM error when instantiating certain types
Browse files Browse the repository at this point in the history
  • Loading branch information
dmulloy2 committed Jun 14, 2024
1 parent e726f6e commit a006b70
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.function.Supplier;

import com.comphenix.protocol.reflect.accessors.Accessors;
import com.comphenix.protocol.reflect.accessors.ConstructorAccessor;
import com.comphenix.protocol.reflect.accessors.MethodAccessor;

public final class InstanceCreator implements Supplier<Object> {
private static Map<Class<?>, Object> BANNED_PARAMETERS = new WeakHashMap<>();

This comment has been minimized.

Copy link
@Ingrim4

Ingrim4 Jun 15, 2024

Collaborator

Why is this a weak hash map wouldn't a hash set suffice here?


static {
try {
BANNED_PARAMETERS.put(ByteBuffer.class, true);
BANNED_PARAMETERS.put(FloatBuffer.class, true);
} catch (Throwable ignored) {
}
}

private ConstructorAccessor constructor = null;
private MethodAccessor factoryMethod = null;
private Class<?>[] paramTypes = null;
Expand Down Expand Up @@ -45,6 +59,15 @@ private Object[] createParams(Class<?>[] paramTypes) {
return params;
}

private boolean containsBannedParameter(Class<?>[] paramTypes) {
for (Class<?> paramType : paramTypes) {
if (BANNED_PARAMETERS.containsKey(paramType)) {
return true;
}
}
return false;
}

@Override
public Object get() {
Object[] params = paramTypes != null ? createParams(paramTypes) : null;
Expand All @@ -70,6 +93,10 @@ public Object get() {
continue;
}

if (containsBannedParameter(paramTypes)) {
continue;
}

Object[] testParams = createParams(paramTypes);

try {
Expand Down Expand Up @@ -103,6 +130,10 @@ public Object get() {
continue;
}

if (containsBannedParameter(paramTypes)) {
continue;
}

Object[] testParams = createParams(paramTypes);

try {
Expand Down

0 comments on commit a006b70

Please sign in to comment.