From 0f4bc89b3c4eab8ad99b1d6d7f37b4d778ae6178 Mon Sep 17 00:00:00 2001 From: DaanVanYperen Date: Thu, 15 Jul 2021 17:48:15 +0200 Subject: [PATCH] #641: Increase reflection 65535 constant cap by splitting reflection between two classes. - Merges TheLogicMaster:split-gwt-reflection-cache, excluding an optional source-reshuffle. --- CONTRIBUTORS | 3 +- .../utils/reflect/ClassReflection.java | 2 +- .../gwtref/client/IReflectionCache2.java | 22 +++++++ .../gwtref/client/ReflectionCache.java | 61 +++++++++++++------ .../java/com/artemis/gwtref/client/Type.java | 1 + .../gwtref/gen/ReflectionCacheGenerator.java | 7 ++- .../gen/ReflectionCacheSourceCreator.java | 16 ++++- 7 files changed, 85 insertions(+), 27 deletions(-) create mode 100644 artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/gwtref/client/IReflectionCache2.java diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 28f8d2ad8..1c8d15418 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -11,4 +11,5 @@ CodePoKE (https://github.com/codepoke) Mykola Zekter (https://github.com/nikoliazekter) Nicolaichuk (https://github.com/nicolaichuk) Felix Bridault (https://github.com/GMWolf) -Schosin (https://github.com/schosin) \ No newline at end of file +Schosin (https://github.com/schosin) +TheLogicMaster (https://github.com/TheLogicMaster) \ No newline at end of file diff --git a/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/backends/gwt/emu/com/artemis/utils/reflect/ClassReflection.java b/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/backends/gwt/emu/com/artemis/utils/reflect/ClassReflection.java index d2970e8ed..14b4a2561 100644 --- a/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/backends/gwt/emu/com/artemis/utils/reflect/ClassReflection.java +++ b/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/backends/gwt/emu/com/artemis/utils/reflect/ClassReflection.java @@ -31,7 +31,7 @@ public final class ClassReflection { static public Class forName (String name) throws ReflectionException { try { return ReflectionCache.forName(name).getClassOfType(); - } catch (ClassNotFoundException e) { + } catch (RuntimeException e) { throw new ReflectionException("Class not found: " + name); } } diff --git a/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/gwtref/client/IReflectionCache2.java b/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/gwtref/client/IReflectionCache2.java new file mode 100644 index 000000000..9a1ae93ac --- /dev/null +++ b/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/gwtref/client/IReflectionCache2.java @@ -0,0 +1,22 @@ +/******************************************************************************* + * Copyright 2011 See AUTHORS file. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ + +package com.artemis.gwtref.client; + +import java.util.Collection; + +public interface IReflectionCache2 extends IReflectionCache { +} diff --git a/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/gwtref/client/ReflectionCache.java b/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/gwtref/client/ReflectionCache.java index 644650bdb..c2dda0493 100644 --- a/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/gwtref/client/ReflectionCache.java +++ b/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/gwtref/client/ReflectionCache.java @@ -22,21 +22,43 @@ public class ReflectionCache { - private static IReflectionCache instance = GWT.create(IReflectionCache.class); + private static final IReflectionCache instance1 = GWT.create(IReflectionCache.class); + private static final IReflectionCache instance2 = GWT.create(IReflectionCache2.class); - public static Type forName (String name) throws ClassNotFoundException { - Type type = instance.forName(convert(name)); - if (type == null) { - throw new RuntimeException("Couldn't find Type for class '" + name + "'"); + public static Type forName (String name) { + Type type = instance1.forName(convert(name)); + if (type != null) { + if (type.source == null) { + type.source = instance1; + } + } else { + type = instance2.forName(convert(name)); + if (type == null) { + throw new RuntimeException("Couldn't find Type for class '" + name + "'"); + } + if (type.source == null) { + type.source = instance2; + } } return type; } public static Type getType (Class clazz) { - if (clazz == null) return null; - Type type = instance.forName(convert(clazz.getName())); - if (type == null) { - throw new RuntimeException("(artemis-odb) Couldn't find Type for class '" + clazz.getName() + "'"); + if (clazz == null) + return null; + Type type = instance1.forName(convert(clazz.getName())); + if (type != null) { + if (type.source == null) { + type.source = instance1; + } + } else { + type = instance2.forName(convert(clazz.getName())); + if (type == null) { + throw new RuntimeException("Couldn't find Type for class '" + clazz.getName() + "'"); + } + if (type.source == null) { + type.source = instance2; + } } return type; } @@ -80,30 +102,31 @@ private static String convert (String className) { } public static Object newArray (Class componentType, int size) { - return instance.newArray(getType(componentType), size); + Type type = getType(componentType); + return type.source.newArray(type, size); } public static Object getFieldValue (Field field, Object obj) throws IllegalAccessException { - return instance.get(field, obj); + return field.getEnclosingType().source.get(field, obj); } public static void setFieldValue (Field field, Object obj, Object value) throws IllegalAccessException { - instance.set(field, obj, value); - } - - public static Object invoke (Method method, Object obj, Object[] params) { - return instance.invoke(method, obj, params); + field.getEnclosingType().source.set(field, obj, value); } public static int getArrayLength (Type type, Object obj) { - return instance.getArrayLength(type, obj); + return type.source.getArrayLength(type, obj); } public static Object getArrayElement (Type type, Object obj, int i) { - return instance.getArrayElement(type, obj, i); + return type.source.getArrayElement(type, obj, i); } public static void setArrayElement (Type type, Object obj, int i, Object value) { - instance.setArrayElement(type, obj, i, value); + type.source.setArrayElement(type, obj, i, value); + } + + public static Object invoke (Method method, Object obj, Object[] params) { + return method.enclosingType.getType().source.invoke(method, obj, params); } } \ No newline at end of file diff --git a/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/gwtref/client/Type.java b/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/gwtref/client/Type.java index cbc581668..4af596270 100644 --- a/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/gwtref/client/Type.java +++ b/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/gwtref/client/Type.java @@ -56,6 +56,7 @@ public class Type { Class componentType; Object[] enumConstants; + IReflectionCache source; private Field[] allFields; private Method[] allMethods; diff --git a/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/gwtref/gen/ReflectionCacheGenerator.java b/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/gwtref/gen/ReflectionCacheGenerator.java index 633c68940..9fe2c7be9 100644 --- a/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/gwtref/gen/ReflectionCacheGenerator.java +++ b/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/gwtref/gen/ReflectionCacheGenerator.java @@ -40,7 +40,8 @@ public String generate (TreeLogger logger, GeneratorContext context, String type throw new UnableToCompleteException(); } - ReflectionCacheSourceCreator source = new ReflectionCacheSourceCreator(logger, context, type); - return source.create(); - } + final int partToInclude = typeName.contains("IReflectionCache2") ? 1 : 0; + ReflectionCacheSourceCreator source = new ReflectionCacheSourceCreator(logger, context, type, partToInclude); + return source.create(); + } } \ No newline at end of file diff --git a/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/gwtref/gen/ReflectionCacheSourceCreator.java b/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/gwtref/gen/ReflectionCacheSourceCreator.java index 2be41d040..7482c7fae 100644 --- a/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/gwtref/gen/ReflectionCacheSourceCreator.java +++ b/artemis-backend-gwt/artemis-gwt/src/main/java/com/artemis/gwtref/gen/ReflectionCacheSourceCreator.java @@ -45,9 +45,10 @@ public class ReflectionCacheSourceCreator { final JClassType type; final String simpleName; final String packageName; + final int part; SourceWriter sw; final StringBuilder source = new StringBuilder(); - final List types = new ArrayList(); + List types = new ArrayList(); final List setterGetterStubs = new ArrayList(); final List methodStubs = new ArrayList(); final Map parameterName2ParameterInstantiation = new HashMap(); @@ -84,12 +85,13 @@ class MethodStub { boolean unused; } - public ReflectionCacheSourceCreator (TreeLogger logger, GeneratorContext context, JClassType type) { + public ReflectionCacheSourceCreator(TreeLogger logger, GeneratorContext context, JClassType type, int part) { this.logger = logger; this.context = context; this.type = type; this.packageName = type.getPackage().getName(); this.simpleName = type.getSimpleSourceName() + "Generated"; + this.part = part; logger.log(Type.INFO, type.getQualifiedSourceName()); } @@ -177,6 +179,14 @@ public int compare (JType o1, JType o2) { } }); + // Split types between two classes as a workaround for the java 65535 const limit. + if (part == 1) { + types = types.subList(types.size() / 2, types.size()); + } else { + types = types.subList(0, types.size() / 2); + } + + // generate Type lookup generator methods. for (JType t : types) { p(createTypeGenerator(t)); @@ -459,7 +469,7 @@ private String generateSetterGetterStub (SetterGetterStub stub) { return sb.toString(); } - private boolean isVisible (JType type) { + private static boolean isVisible (JType type) { if (type == null) return false; if (type instanceof JClassType) {