Skip to content

Commit

Permalink
attempt to make JackpotTrees more robust.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbien committed Oct 28, 2021
1 parent 8eab67f commit bfd6830
Showing 1 changed file with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@
import com.sun.tools.javac.util.Name;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.implementation.FieldAccessor;
Expand Down Expand Up @@ -85,36 +86,45 @@ public static <T> T createInstance(Context ctx, Class<T> clazz, Name ident, JCId
baseClass2Impl.put(clazz, fake);
}

NEXT: for (Constructor<?> c : fake.getDeclaredConstructors()) {
if (c.getParameterCount() < requiredConstructor.length)
continue;
for (int e = 0; e < requiredConstructor.length; e++) {
if (!c.getParameterTypes()[e].equals(requiredConstructor[e])) {
continue NEXT;
}
}
java.util.List<Object> instances = new ArrayList<>();
instances.addAll(Arrays.asList(params));
Optional<Constructor<?>> shortestCompatible = Stream.of(fake.getDeclaredConstructors())
.filter((c) -> {
if (c.getParameterCount() < requiredConstructor.length)
return false;
for (int param = 0; param < requiredConstructor.length; param++)
if (!c.getParameterTypes()[param].equals(requiredConstructor[param]))
return false;
return true;
})
.min((c1, c2) -> c1.getParameterCount() - c2.getParameterCount());

if (shortestCompatible.isPresent()) {

Constructor<?> c = shortestCompatible.get();

java.util.List<Object> instances = new ArrayList<>(Arrays.asList(params));
for (int i = instances.size(); i < c.getParameterCount(); i++) {
if (c.getParameterTypes()[i].isPrimitive()) {
throw new IllegalStateException("incompatible constructor");
}
instances.add(null);
}

JCTree tree = (JCTree) c.newInstance(instances.toArray(new Object[0]));

Field identField = fake.getDeclaredField("ident");

identField.set(tree, ident);

Field jcIdentField = fake.getDeclaredField("jcIdent");

jcIdentField.set(tree, jcIdent);

return clazz.cast(tree);

} else {
throw new IllegalStateException("no compatible constructors found in: "+Arrays.asList(fake.getDeclaredConstructors()).toString());
}

throw new IllegalStateException(Arrays.asList(fake.getDeclaredConstructors()).toString());
} catch (IllegalAccessException | IllegalArgumentException | IllegalStateException | InstantiationException | NoSuchFieldException | NoSuchMethodException | SecurityException | InvocationTargetException ex) {
throw new IllegalStateException(ex);
} catch (ReflectiveOperationException | IllegalArgumentException | IllegalStateException | SecurityException ex) {
throw new IllegalStateException("can't instantiate "+Arrays.asList(requiredConstructor).toString()+" of "+clazz, ex);
}
}

Expand Down

0 comments on commit bfd6830

Please sign in to comment.