Skip to content

Commit

Permalink
#641: Increase reflection 65535 constant cap by splitting reflection …
Browse files Browse the repository at this point in the history
…between two classes.

- Merges TheLogicMaster:split-gwt-reflection-cache, excluding an optional source-reshuffle.
  • Loading branch information
DaanVanYperen committed Jul 15, 2021
1 parent 1e22ddc commit 0f4bc89
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 27 deletions.
3 changes: 2 additions & 1 deletion CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Schosin (https://github.com/schosin)
TheLogicMaster (https://github.com/TheLogicMaster)
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class Type {

Class componentType;
Object[] enumConstants;
IReflectionCache source;

private Field[] allFields;
private Method[] allMethods;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<JType> types = new ArrayList<JType>();
List<JType> types = new ArrayList<JType>();
final List<SetterGetterStub> setterGetterStubs = new ArrayList<SetterGetterStub>();
final List<MethodStub> methodStubs = new ArrayList<MethodStub>();
final Map<String, String> parameterName2ParameterInstantiation = new HashMap<String, String>();
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 0f4bc89

Please sign in to comment.