11package io .tinyauth .elasticsearch .reflection ;
22
3+ import java .util .ArrayList ;
34import java .util .Arrays ;
45import java .util .Set ;
56import java .util .List ;
67import java .util .Comparator ;
78import java .lang .reflect .Method ;
89import java .lang .reflect .Type ;
910import java .lang .reflect .Field ;
11+ import java .lang .reflect .Constructor ;
1012import java .util .stream .Stream ;
1113import java .util .stream .Collectors ;
1214import java .lang .reflect .ParameterizedType ;
2325
2426public class App {
2527 private static Comparator <Class <?>> classComparator = Comparator .comparing (c -> c .getCanonicalName ());
26-
28+
2729 private static boolean hasMethod (Class <?> klass , String key , String returnType ) {
2830 try {
2931 Method m = klass .getMethod (key );
@@ -44,99 +46,171 @@ private static boolean hasMethod(Class<?> klass, String key, String returnType)
4446 }
4547 }
4648
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 );
49+ private static class Skip extends Exception {
50+ public Skip (Class <?> actionType , String reason ) {
51+ super ("Skipped " + actionType + " as " + reason );
52+ }
53+ }
5554
55+ private static String getPermissionForAction (Class <? extends Action > actionType ) throws Skip {
56+ String permissionName ;
5657 try {
57- return Class .forName (canonicalName );
58- } catch (ClassNotFoundException e ) {
59- return null ;
58+ Field permissionField = actionType .getField ("NAME" );
59+ permissionName = (String )permissionField .get (null );
60+ if (permissionName == null ) {
61+ throw new Skip (actionType , "it's NAME seems to be null" );
62+ }
63+ } catch (NoSuchFieldException e ) {
64+ throw new Skip (actionType , "it doesn't have a NAME" );
65+ } catch (IllegalAccessException e ) {
66+ throw new Skip (actionType , "code generator not allowed to access NAME" );
6067 }
68+ return permissionName ;
6169 }
6270
63- public static void main (String [] args ) {
71+ private static Class <? extends ActionRequest > getActionRequestForAction (Class <? extends Action > actionType ) throws Skip {
72+ ParameterizedType actionTypeGeneric = (ParameterizedType )actionType .getGenericSuperclass ();
73+ List <Class <?>> actionRequestTypes = Stream .of (actionTypeGeneric .getActualTypeArguments ())
74+ .filter (t -> t instanceof Class <?>)
75+ .map (r -> (Class <?>)r )
76+ .filter (r -> ActionRequest .class .isAssignableFrom (r ))
77+ .collect (Collectors .toList ());
78+
79+ if (actionRequestTypes .size () == 0 ) {
80+ throw new Skip (actionType , "could not find ActionRequest in actual type arguments for Action" );
81+ } else if (actionRequestTypes .size () > 1 ) {
82+ throw new Skip (actionType , "found multiple ActionRequest in actual type arguments for Action" );
83+ }
84+
85+ return (Class <? extends ActionRequest >)actionRequestTypes .get (0 );
86+ }
87+
88+ public static String singleResource (String functionName , String resourceType ) {
89+ JtwigTemplate template = JtwigTemplate .classpathTemplate ("templates/singleResource.twig" );
90+ JtwigModel model = JtwigModel .newModel ()
91+ .with ("functionName" , functionName )
92+ .with ("resourceType" , resourceType );
93+ return template .render (model );
94+ }
95+
96+ public static String listResource (String functionName , String resourceType ) {
97+ JtwigTemplate template = JtwigTemplate .classpathTemplate ("templates/listResource.twig" );
98+ JtwigModel model = JtwigModel .newModel ()
99+ .with ("functionName" , functionName )
100+ .with ("resourceType" , resourceType );
101+ return template .render (model );
102+ }
103+
104+ public static void main (String [] args ) {
64105 Reflections reflections = new Reflections ("org.elasticsearch" );
65106
66- Set <Class <? extends ActionRequest >> requestTypes = reflections .getSubTypesOf (ActionRequest .class );
67- requestTypes .stream ().sorted (classComparator ).forEach (t -> {
107+ Set <Class <? extends ActionRequest >> allRequestTypes = reflections .getSubTypesOf (ActionRequest .class );
108+ allRequestTypes .stream ().sorted (classComparator ).forEach (t -> {
68109 System .out .println ("import " + t .getCanonicalName () + ';' );
69110 });
70-
111+
71112 System .out .println ("\n \n " );
72113
73114 Set <Class <? extends Action >> actionTypes = reflections .getSubTypesOf (Action .class );
74115 actionTypes .stream ().sorted (classComparator ).forEach (actionType -> {
75- String canonicalName = actionType .getCanonicalName ();
76- System .out .println (canonicalName );
77-
78- String permissionName ;
79116 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 */" );
117+ String permissionName = getPermissionForAction (actionType );
118+ Class <? extends ActionRequest > actionRequestType = getActionRequestForAction (actionType );
119+ List <String > extractions = new ArrayList <String >();
120+
121+ if (hasMethod (actionRequestType , "indices" , "java.lang.String[]" )) {
122+ JtwigTemplate template = JtwigTemplate .classpathTemplate ("templates/indices.twig" );
123+ JtwigModel model = JtwigModel .newModel ()
124+ .with ("requestClassName" , actionRequestType .getSimpleName ())
125+ .with ("permissionName" , permissionName );
126+ extractions .add (template .render (model ));
127+ }
128+
129+ if (hasMethod (actionRequestType , "nodeIds" , "java.lang.String[]" )) {
130+ JtwigTemplate template = JtwigTemplate .classpathTemplate ("templates/nodeIds.twig" );
131+ JtwigModel model = JtwigModel .newModel ()
132+ .with ("requestClassName" , actionRequestType .getSimpleName ())
133+ .with ("permissionName" , permissionName );
134+ extractions .add (template .render (model ));
84135 return ;
85136 }
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 );
104137
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");
138+ if (hasMethod (actionRequestType , "getNodes" , "java.lang.String[]" )) {
139+ JtwigTemplate template = JtwigTemplate .classpathTemplate ("templates/getNodes.twig" );
140+ JtwigModel model = JtwigModel .newModel ()
141+ .with ("requestClassName" , actionRequestType .getSimpleName ())
142+ .with ("permissionName" , permissionName );
143+ extractions .add (template .render (model ));
144+ }
145+
146+ if (hasMethod (actionRequestType , "getRequests" , "java.lang.String[]" )) {
147+ JtwigTemplate template = JtwigTemplate .classpathTemplate ("templates/getRequests.twig" );
148+ JtwigModel model = JtwigModel .newModel ()
149+ .with ("requestClassName" , actionRequestType .getSimpleName ())
150+ .with ("permissionName" , permissionName );
151+ extractions .add (template .render (model ));
152+ }
153+
154+ if (hasMethod (actionRequestType , "requests" , "java.lang.String[]" )) {
155+ JtwigTemplate template = JtwigTemplate .classpathTemplate ("templates/requests.twig" );
156+ JtwigModel model = JtwigModel .newModel ()
157+ .with ("requestClassName" , actionRequestType .getSimpleName ())
158+ .with ("permissionName" , permissionName );
159+ extractions .add (template .render (model ));
160+ }
161+
162+ if (actionRequestType .getSimpleName ().contains ("Repositor" )) {
163+ if (hasMethod (actionRequestType , "name" , "java.lang.String" )) {
164+ JtwigTemplate template = JtwigTemplate .classpathTemplate ("templates/Repository.name.twig" );
165+ JtwigModel model = JtwigModel .newModel ()
166+ .with ("requestClassName" , actionRequestType .getSimpleName ())
167+ .with ("permissionName" , permissionName );
168+ extractions .add (template .render (model ));
169+ }
170+ }
171+
172+ if (hasMethod (actionRequestType , "repositories" , "java.lang.String[]" )) {
173+ JtwigTemplate template = JtwigTemplate .classpathTemplate ("templates/Repository.repositories.twig" );
174+ JtwigModel model = JtwigModel .newModel ()
175+ .with ("requestClassName" , actionRequestType .getSimpleName ())
176+ .with ("permissionName" , permissionName );
177+ extractions .add (template .render (model ));
178+ }
179+
180+ if (actionRequestType .getSimpleName ().contains ("StoredScript" )) {
181+ if (hasMethod (actionRequestType , "id" , "java.lang.String" )) {
182+ extractions .add (singleResource ("id" , "stored-script" ));
183+ }
184+ }
185+
186+ if (hasMethod (actionRequestType , "snapshot" , "java.lang.String" )) {
187+ extractions .add (singleResource ("snapshot" , "snapshot" ));
188+ }
189+
190+ if (hasMethod (actionRequestType , "snapshots" , "java.lang.String[]" )) {
191+ extractions .add (singleResource ("snapshots" , "snapshot" ));
192+ }
193+
194+ if (extractions .size () == 0 ) {
195+ JtwigTemplate template = JtwigTemplate .classpathTemplate ("templates/simple.twig" );
196+ JtwigModel model = JtwigModel .newModel ()
197+ .with ("requestClassName" , actionRequestType .getSimpleName ())
198+ .with ("permissionName" , permissionName );
199+ extractions .add (template .render (model ));
200+ }
201+
202+ JtwigTemplate template = JtwigTemplate .classpathTemplate ("templates/body.twig" );
203+ JtwigModel model = JtwigModel .newModel ()
204+ .with ("requestClassName" , actionRequestType .getSimpleName ())
205+ .with ("permissionName" , permissionName )
206+ .with ("body" , String .join ("\n " , extractions ));
135207 template .render (model , System .out );
136- return;
137- }*/
138-
139- System .out .println ("\n \n // Not able to generate wrapper for '" + actionType + "'\n \n " );
208+
209+ } catch (Skip s ) {
210+ System .out .println ("/* " + s .getMessage () + " */" );
211+ }
212+
213+ System .out .println ("" );
140214 });
141215 }
142216}
0 commit comments