Skip to content

Commit

Permalink
Optimise primitive clazz checks
Browse files Browse the repository at this point in the history
  • Loading branch information
franz1981 committed Dec 11, 2023
1 parent af6d3d7 commit 7ddf961
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -31,7 +30,6 @@ public abstract class Serialisers {
public static final Annotation[] NO_ANNOTATION = new Annotation[0];
public static final ReaderInterceptor[] NO_READER_INTERCEPTOR = new ReaderInterceptor[0];
public static final WriterInterceptor[] NO_WRITER_INTERCEPTOR = new WriterInterceptor[0];
protected static final Map<Class<?>, Class<?>> primitivesToWrappers = new HashMap<>();
// FIXME: spec says we should use generic type, but not sure how to pass that type from Jandex to reflection
protected final QuarkusMultivaluedMap<Class<?>, ResourceWriter> writers = new QuarkusMultivaluedHashMap<>();
protected final QuarkusMultivaluedMap<Class<?>, ResourceReader> readers = new QuarkusMultivaluedHashMap<>();
Expand All @@ -46,9 +44,7 @@ public List<MessageBodyReader<?>> findReaders(ConfigurationImpl configuration, C
List<MediaType> desired = MediaTypeHelper.getUngroupedMediaTypes(mediaType);
List<MessageBodyReader<?>> ret = new ArrayList<>();
Deque<Class<?>> toProcess = new LinkedList<>();
Class<?> klass = entityType;
if (primitivesToWrappers.containsKey(klass))
klass = primitivesToWrappers.get(klass);
Class<?> klass = lookupPrimitiveWrapper(entityType);
QuarkusMultivaluedMap<Class<?>, ResourceReader> readers;
if (configuration != null && !configuration.getResourceReaders().isEmpty()) {
readers = new QuarkusMultivaluedHashMap<>();
Expand Down Expand Up @@ -124,9 +120,7 @@ public List<MessageBodyWriter<?>> findBuildTimeWriters(Class<?> entityType, Runt
if (Response.class.isAssignableFrom(entityType)) {
return Collections.emptyList();
}
Class<?> klass = entityType;
if (primitivesToWrappers.containsKey(klass))
klass = primitivesToWrappers.get(klass);
Class<?> klass = primitiveWrapperOf(entityType);
//first we check to make sure that the return type is build time selectable
//this fails when there are eligible writers for a sub type of the entity type
//e.g. if the entity type is Object and there are mappers for String then we
Expand Down Expand Up @@ -241,14 +235,17 @@ public List<MessageBodyWriter<?>> findWriters(ConfigurationImpl configuration, C
return findWriters(configuration, entityType, resolvedMediaType, null);
}

protected Class<?> lookupPrimitiveWrapper(Class<?> entityType) {
return entityType;
}

public List<MessageBodyWriter<?>> findWriters(ConfigurationImpl configuration, Class<?> entityType,
MediaType resolvedMediaType, RuntimeType runtimeType) {
// FIXME: invocation is very different between client and server, where the server doesn't treat GenericEntity specially
// it's probably missing from there, while the client handles it upstack
List<MediaType> mt = Collections.singletonList(resolvedMediaType);
Class<?> klass = entityType;
if (primitivesToWrappers.containsKey(klass))
klass = primitivesToWrappers.get(klass);
klass = primitiveWrapperOf(entityType);
QuarkusMultivaluedMap<Class<?>, ResourceWriter> writers;
if (configuration != null && !configuration.getResourceWriters().isEmpty()) {
writers = new QuarkusMultivaluedHashMap<>();
Expand All @@ -263,6 +260,10 @@ public List<MessageBodyWriter<?>> findWriters(ConfigurationImpl configuration, C
return toMessageBodyWriters(findResourceWriters(writers, klass, mt, runtimeType));
}

private Class<?> primitiveWrapperOf(Class<?> entityType) {
return entityType;
}

public static class Builtin {
public final Class<?> entityClass;
public final String mediaType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,6 @@ public void accept(ResteasyReactiveRequestContext context) {
private static final String LENGTH_LOWER = "length";
private static final String CONTENT_TYPE = CONTENT + "-" + TYPE; // use this instead of the Vert.x constant because the TCK expects upper case

static {
primitivesToWrappers.put(boolean.class, Boolean.class);
primitivesToWrappers.put(char.class, Character.class);
primitivesToWrappers.put(byte.class, Byte.class);
primitivesToWrappers.put(short.class, Short.class);
primitivesToWrappers.put(int.class, Integer.class);
primitivesToWrappers.put(long.class, Long.class);
primitivesToWrappers.put(float.class, Float.class);
primitivesToWrappers.put(double.class, Double.class);
}

public final static List<Serialisers.BuiltinReader> BUILTIN_READERS = List.of(
new Serialisers.BuiltinReader(String.class, ServerStringMessageBodyHandler.class,
MediaType.WILDCARD),
Expand Down Expand Up @@ -189,6 +178,32 @@ public List<ResourceWriter> apply(Class<?> aClass) {
}
};

@Override
protected final Class<?> lookupPrimitiveWrapper(final Class<?> entityType) {
if (!entityType.isPrimitive()) {
return entityType;
}
if (entityType == boolean.class) {
return Boolean.class;
} else if (entityType == char.class) {
return Character.class;
} else if (entityType == byte.class) {
return Byte.class;
} else if (entityType == short.class) {
return Short.class;
} else if (entityType == int.class) {
return Integer.class;
} else if (entityType == long.class) {
return Long.class;
} else if (entityType == float.class) {
return Float.class;
} else if (entityType == double.class) {
return Double.class;
}
// this shouldn't really happen, but better be safe than sorry
return entityType;
}

public static boolean invokeWriter(ResteasyReactiveRequestContext context, Object entity, MessageBodyWriter writer,
ServerSerialisers serialisers)
throws IOException {
Expand Down

0 comments on commit 7ddf961

Please sign in to comment.