|
| 1 | +package io.tinyauth.elasticsearch.reflection; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Set; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Comparator; |
| 7 | +import java.lang.reflect.Method; |
| 8 | +import java.lang.reflect.Type; |
| 9 | +import java.lang.reflect.Field; |
| 10 | +import java.util.stream.Stream; |
| 11 | +import java.util.stream.Collectors; |
| 12 | +import java.lang.reflect.ParameterizedType; |
| 13 | + |
| 14 | +import org.elasticsearch.action.ActionRequest; |
| 15 | +import org.elasticsearch.action.ActionRequestBuilder; |
| 16 | +import org.elasticsearch.action.Action; |
| 17 | + |
| 18 | +import org.reflections.Reflections; |
| 19 | + |
| 20 | +import org.jtwig.JtwigModel; |
| 21 | +import org.jtwig.JtwigTemplate; |
| 22 | + |
| 23 | + |
| 24 | +public class App { |
| 25 | + private static Comparator<Class<?>> classComparator = Comparator.comparing(c -> c.getCanonicalName()); |
| 26 | + |
| 27 | + private static boolean hasMethod(Class<?> klass, String key, String returnType) { |
| 28 | + try { |
| 29 | + Method m = klass.getMethod(key); |
| 30 | + if (m == null) { |
| 31 | + return false; |
| 32 | + } |
| 33 | + |
| 34 | + if (!m.getGenericReturnType().getTypeName().equals(returnType)) { |
| 35 | + System.out.println("Method rejected due to return type: " + m.getGenericReturnType().getTypeName()); |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + return true; |
| 40 | + |
| 41 | + } catch (NoSuchMethodException e) { |
| 42 | + // System.out.println("NoSuchMethod exception thrown"); |
| 43 | + return false; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private static Class<?> getActionForRequest(String canonicalName) { |
| 48 | + if (canonicalName.endsWith("Request")) |
| 49 | + canonicalName = canonicalName.substring(0, canonicalName.length() - 7); |
| 50 | + |
| 51 | + if (canonicalName.endsWith("$")) |
| 52 | + canonicalName = canonicalName.substring(0, canonicalName.length() - 1); |
| 53 | + |
| 54 | + System.out.println(canonicalName); |
| 55 | + |
| 56 | + try { |
| 57 | + return Class.forName(canonicalName); |
| 58 | + } catch (ClassNotFoundException e) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public static void main(String[] args) { |
| 64 | + Reflections reflections = new Reflections("org.elasticsearch"); |
| 65 | + |
| 66 | + Set<Class<? extends ActionRequest>> requestTypes = reflections.getSubTypesOf(ActionRequest.class); |
| 67 | + requestTypes.stream().sorted(classComparator).forEach(t -> { |
| 68 | + System.out.println("import " + t.getCanonicalName() + ';'); |
| 69 | + }); |
| 70 | + |
| 71 | + System.out.println("\n\n"); |
| 72 | + |
| 73 | + Set<Class<? extends Action>> actionTypes = reflections.getSubTypesOf(Action.class); |
| 74 | + actionTypes.stream().sorted(classComparator).forEach(actionType -> { |
| 75 | + String canonicalName = actionType.getCanonicalName(); |
| 76 | + System.out.println(canonicalName); |
| 77 | + |
| 78 | + String permissionName; |
| 79 | + try { |
| 80 | + Field permissionField = actionType.getField("NAME"); |
| 81 | + permissionName = (String)permissionField.get(null); |
| 82 | + if (permissionName == null) { |
| 83 | + System.out.println("/* Skipped " + actionType + " as it NAME seems to be null */"); |
| 84 | + return; |
| 85 | + } |
| 86 | + } catch(NoSuchFieldException e) { |
| 87 | + System.out.println("/* Skipped " + actionType + " as it doesnt have a NAME */"); |
| 88 | + return; |
| 89 | + } catch (IllegalAccessException e) { |
| 90 | + System.out.println("/* Skipped " + actionType + " as code generator not allowed to access NAME */"); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + System.out.println(permissionName); |
| 95 | + |
| 96 | + Method newRequestBuilder = Stream.of(actionType.getMethods()) |
| 97 | + .filter(m -> !m.isBridge()) |
| 98 | + .filter(m -> m.getName() == "newRequestBuilder") |
| 99 | + // .reduce((a, b) -> throw new RuntimeError("Got multiple newRequestBuilder!")) |
| 100 | + .collect(Collectors.toList()).get(0); |
| 101 | + |
| 102 | + Class<? extends ActionRequestBuilder> builderClass = (Class<? extends ActionRequestBuilder>)newRequestBuilder.getReturnType(); |
| 103 | + System.out.println(builderClass); |
| 104 | + |
| 105 | + try { |
| 106 | + builderClass.getField("request"); |
| 107 | + } catch (NoSuchFieldException e) { |
| 108 | + System.out.println("/* RequestBuilder does not have a request field */"); |
| 109 | + } |
| 110 | + |
| 111 | + /*for (Field field : ) { |
| 112 | + System.out.format("Type: %s%n", field.getType()); |
| 113 | + System.out.format("GenericType: %s%n", field.getGenericType()); |
| 114 | + }*/ |
| 115 | + |
| 116 | + /*Class<T> persistentClass = (Class<T>) |
| 117 | + ((ParameterizedType)getClass().getGenericSuperclass()) |
| 118 | + .getActualTypeArguments()[0];*/ |
| 119 | + |
| 120 | + System.out.println("**"); |
| 121 | + Arrays.asList(actionType.getGenericInterfaces()).stream().forEach(t -> System.out.println(t)); |
| 122 | + // ParameterizedType parameterizedType = (ParameterizedType) clazz.getGenericInterfaces()[0]; |
| 123 | + |
| 124 | + System.out.println("**"); |
| 125 | + Arrays.asList(actionType.getTypeParameters()).stream().forEach(t -> System.out.println(t)); |
| 126 | + System.out.println("**"); |
| 127 | + |
| 128 | + /*Stream.of(builderClass.getMethods()) |
| 129 | + // .reduce((a, b) -> throw new RuntimeError("Got multiple newRequestBuilder!")) |
| 130 | + .forEach(m -> System.out.println(m));*/ |
| 131 | + |
| 132 | + /*if (hasMethod(t, "indices", "java.lang.String[]")) { |
| 133 | + JtwigTemplate template = JtwigTemplate.classpathTemplate("templates/indices.twig"); |
| 134 | + JtwigModel model = JtwigModel.newModel().with("requestClassName", t.getName()).with("permissionName", "SomePermissionAction"); |
| 135 | + template.render(model, System.out); |
| 136 | + return; |
| 137 | + }*/ |
| 138 | + |
| 139 | + System.out.println("\n\n// Not able to generate wrapper for '" + actionType + "'\n\n"); |
| 140 | + }); |
| 141 | + } |
| 142 | +} |
0 commit comments