Skip to content

Commit 09b722d

Browse files
committed
Reflectioning thoughts
1 parent 8735c50 commit 09b722d

File tree

7 files changed

+220
-0
lines changed

7 files changed

+220
-0
lines changed

reflectioning/Dockerfile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
FROM openjdk:8-jdk AS builder
2+
3+
ENV GRADLE_VERSION 3.3
4+
ENV GRADLE_SHA c58650c278d8cf0696cab65108ae3c8d95eea9c1938e0eb8b997095d5ca9a292
5+
6+
RUN cd /usr/lib \
7+
&& curl -fl https://downloads.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip -o gradle-bin.zip \
8+
&& echo "$GRADLE_SHA gradle-bin.zip" | sha256sum -c - \
9+
&& unzip "gradle-bin.zip" \
10+
&& ln -s "/usr/lib/gradle-${GRADLE_VERSION}/bin/gradle" /usr/bin/gradle \
11+
&& rm "gradle-bin.zip"
12+
13+
# Set Appropriate Environmental Variables
14+
ENV GRADLE_HOME /usr/lib/gradle
15+
ENV PATH $PATH:$GRADLE_HOME/bin
16+
17+
COPY build.gradle /src/build.gradle
18+
WORKDIR /src
19+
RUN gradle downloadDependencies
20+
21+
COPY . .
22+
RUN gradle assemble
23+
24+
CMD ["gradle", "run"]

reflectioning/build.gradle

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
apply plugin: 'java'
2+
apply plugin: 'application'
3+
4+
repositories {
5+
jcenter()
6+
}
7+
8+
dependencies {
9+
compile 'org.elasticsearch:elasticsearch:5.5.2'
10+
compile 'org.reflections:reflections:0.9.11'
11+
compile "org.jtwig:jtwig-core:5.86.1.RELEASE"
12+
testCompile 'junit:junit:4.12'
13+
}
14+
15+
mainClassName = 'io.tinyauth.elasticsearch.reflection.App'
16+
17+
task downloadDependencies {
18+
description "Pre-downloads *most* dependencies"
19+
doLast {
20+
configurations.getAsMap().each { name, config ->
21+
println "Retrieving dependencies for $name"
22+
try {
23+
config.files
24+
} catch (e) {
25+
project.logger.info e.message // some cannot be resolved, silentlyish skip them
26+
}
27+
}
28+
}
29+
}

reflectioning/docker-compose.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: '2'
2+
3+
services:
4+
reflect:
5+
build: .
6+
volumes:
7+
- .:/data

reflectioning/settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = 'reflectioning'
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
this.methods.put({{ requestClassName }}.class, (permissions, request) -> {
3+
{{ requestClassName }} req = ({{ requestClassName }})request;
4+
Set<String> permission = permissions.get("{{ permissionName }}");
5+
Stream.of(req.indices()).map(idx -> formatArn("index", idx)).forEach(permission::add);
6+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import io.tinyauth.elasticsearch.reflection.App;
2+
3+
import org.junit.Test;
4+
import static org.junit.Assert.*;
5+
6+
7+
public class AppTest {
8+
@Test public void testAppInstanceable() {
9+
App classUnderTest = new App();
10+
}
11+
}

0 commit comments

Comments
 (0)