Skip to content

Commit a2b2f7d

Browse files
bbakermanyrashk
authored andcommitted
Remove Lombok dependency
1 parent 660b549 commit a2b2f7d

14 files changed

+324
-185
lines changed

build.gradle

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ dependencies {
7373
compile 'javax.validation:validation-api:1.1.0.Final'
7474
compile 'com.graphql-java:graphql-java:2.2.0'
7575

76-
// Remove some boilerplate
77-
compile 'org.projectlombok:lombok:1.16.6'
78-
7976
// OSGi
8077
compileOnly 'org.osgi:org.osgi.core:6.0.0'
8178
compileOnly 'org.osgi:org.osgi.service.cm:1.5.0'

src/main/java/graphql/annotations/BatchedMethodDataFetcher.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import graphql.execution.batched.Batched;
1818
import graphql.schema.DataFetchingEnvironment;
19-
import lombok.SneakyThrows;
2019

2120
import java.lang.reflect.Method;
2221
import java.lang.reflect.Modifier;
@@ -36,7 +35,6 @@ public BatchedMethodDataFetcher(Method method, TypeFunction typeFunction) {
3635
}
3736
}
3837

39-
@SneakyThrows
4038
@Batched
4139
@Override
4240
public Object get(DataFetchingEnvironment environment) {

src/main/java/graphql/annotations/DefaultTypeFunction.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import graphql.schema.GraphQLList;
2020
import graphql.schema.GraphQLType;
2121
import graphql.schema.GraphQLTypeReference;
22-
import lombok.SneakyThrows;
2322
import org.osgi.service.component.annotations.*;
2423

2524
import java.lang.reflect.AnnotatedParameterizedType;
@@ -166,7 +165,6 @@ public GraphQLType apply(Class<?> aClass, AnnotatedType annotatedType) {
166165
private class OptionalFunction implements TypeFunction {
167166

168167
@Override
169-
@SneakyThrows
170168
public GraphQLType apply(Class<?> aClass, AnnotatedType annotatedType) {
171169
if (!(annotatedType instanceof AnnotatedParameterizedType)) {
172170
throw new IllegalArgumentException("Optional type parameter should be specified");
@@ -230,7 +228,6 @@ private class ObjectFunction implements TypeFunction {
230228
private final Map<String, GraphQLType> types = new ConcurrentHashMap<>();
231229

232230
@Override
233-
@SneakyThrows
234231
public GraphQLType apply(Class<?> aClass, AnnotatedType annotatedType) {
235232
GraphQLName name = aClass.getAnnotation(GraphQLName.class);
236233
String typeName = name == null ? aClass.getSimpleName() : name.value();

src/main/java/graphql/annotations/GraphQLAnnotations.java

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import java.lang.reflect.AnnotatedType;
4040
import java.lang.reflect.Constructor;
4141
import java.lang.reflect.Field;
42-
import java.lang.reflect.InvocationTargetException;
4342
import java.lang.reflect.Method;
4443
import java.lang.reflect.Modifier;
4544
import java.lang.reflect.Parameter;
@@ -53,6 +52,8 @@
5352
import java.util.stream.Collectors;
5453

5554
import static graphql.Scalars.GraphQLBoolean;
55+
import static graphql.annotations.ReflectionKit.constructNewInstance;
56+
import static graphql.annotations.ReflectionKit.newInstance;
5657
import static graphql.schema.GraphQLArgument.newArgument;
5758
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
5859
import static graphql.schema.GraphQLInputObjectField.newInputObjectField;
@@ -316,7 +317,7 @@ protected GraphQLFieldDefinition getField(Field field) throws GraphQLAnnotations
316317

317318
if (actualDataFetcher == null) {
318319

319-
StringBuffer fluentBuffer = new StringBuffer(field.getName());
320+
StringBuilder fluentBuffer = new StringBuilder(field.getName());
320321
fluentBuffer.setCharAt(0, Character.toLowerCase(fluentBuffer.charAt(0)));
321322
String fluentGetter = fluentBuffer.toString();
322323

@@ -428,25 +429,22 @@ protected GraphQLFieldDefinition getField(Method method) throws GraphQLAnnotatio
428429
TypeFunction finalTypeFunction = typeFunction;
429430
List<GraphQLArgument> args = Arrays.asList(method.getParameters()).stream().
430431
filter(p -> !DataFetchingEnvironment.class.isAssignableFrom(p.getType())).
431-
map(new Function<Parameter, GraphQLArgument>() {
432-
@Override
433-
public GraphQLArgument apply(Parameter parameter) {
434-
Class<?> t = parameter.getType();
435-
graphql.schema.GraphQLType graphQLType = finalTypeFunction.apply(t, parameter.getAnnotatedType());
436-
if (graphQLType instanceof GraphQLObjectType) {
437-
GraphQLInputObjectType inputObject = getInputObject((GraphQLObjectType) graphQLType);
438-
graphQLType = inputObject;
439-
}
440-
return getArgument(parameter, graphQLType);
432+
map(parameter -> {
433+
Class<?> t = parameter.getType();
434+
graphql.schema.GraphQLType graphQLType = finalTypeFunction.apply(t, parameter.getAnnotatedType());
435+
if (graphQLType instanceof GraphQLObjectType) {
436+
GraphQLInputObjectType inputObject = getInputObject((GraphQLObjectType) graphQLType);
437+
graphQLType = inputObject;
441438
}
439+
return getArgument(parameter, graphQLType);
442440
}).collect(Collectors.toList());
443441

444442
GraphQLFieldDefinition relay = null;
445443
if (method.isAnnotationPresent(GraphQLRelayMutation.class)) {
446444
if (!(outputType instanceof GraphQLObjectType || outputType instanceof GraphQLInterfaceType)) {
447445
throw new RuntimeException("outputType should be an object or an interface");
448446
}
449-
StringBuffer titleBuffer = new StringBuffer(method.getName());
447+
StringBuilder titleBuffer = new StringBuilder(method.getName());
450448
titleBuffer.setCharAt(0, Character.toUpperCase(titleBuffer.charAt(0)));
451449
String title = titleBuffer.toString();
452450
List<GraphQLFieldDefinition> fieldDefinitions = outputType instanceof GraphQLObjectType ?
@@ -613,21 +611,4 @@ public GraphQLObjectType getType(Object object) {
613611
}
614612
}
615613
}
616-
617-
private <T> T newInstance(Class<T> clazz) throws GraphQLAnnotationsException {
618-
try {
619-
return clazz.newInstance();
620-
} catch (InstantiationException | IllegalAccessException e) {
621-
throw new GraphQLAnnotationsException("Unable to instantiate class : " + clazz, e);
622-
}
623-
}
624-
625-
private static <T> T constructNewInstance(Constructor<T> constructor, Object... args) throws GraphQLAnnotationsException {
626-
try {
627-
return constructor.newInstance(args);
628-
629-
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
630-
throw new GraphQLAnnotationsException("Unable to instantiate via constructor : " + constructor, e);
631-
}
632-
}
633614
}

src/main/java/graphql/annotations/GraphQLObjectTypeWrapper.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
package graphql.annotations;
1616

1717
import graphql.schema.GraphQLObjectType;
18-
import lombok.Getter;
1918

2019
public class GraphQLObjectTypeWrapper extends GraphQLObjectType implements GraphQLObjectBackedByClass {
2120

22-
@Getter
2321
private final Class<?> objectClass;
2422

2523
public GraphQLObjectTypeWrapper(Class<?> objectClass, GraphQLObjectType objectType) {
@@ -28,6 +26,11 @@ public GraphQLObjectTypeWrapper(Class<?> objectClass, GraphQLObjectType objectTy
2826
this.objectClass = objectClass;
2927
}
3028

29+
@Override
30+
public Class<?> getObjectClass() {
31+
return objectClass;
32+
}
33+
3134
@Override
3235
public boolean equals(Object obj) {
3336
return obj instanceof GraphQLObjectType &&

src/main/java/graphql/annotations/MethodDataFetcher.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,10 +17,20 @@
1717
import graphql.schema.DataFetcher;
1818
import graphql.schema.DataFetchingEnvironment;
1919
import graphql.schema.GraphQLObjectType;
20-
import lombok.SneakyThrows;
2120

22-
import java.lang.reflect.*;
23-
import java.util.*;
21+
import java.lang.reflect.Constructor;
22+
import java.lang.reflect.InvocationTargetException;
23+
import java.lang.reflect.Method;
24+
import java.lang.reflect.Modifier;
25+
import java.lang.reflect.Parameter;
26+
import java.util.ArrayList;
27+
import java.util.HashMap;
28+
import java.util.Iterator;
29+
import java.util.List;
30+
31+
import static graphql.annotations.ReflectionKit.constructNewInstance;
32+
import static graphql.annotations.ReflectionKit.constructor;
33+
import static graphql.annotations.ReflectionKit.newInstance;
2434

2535
class MethodDataFetcher implements DataFetcher {
2636
private final Method method;
@@ -35,7 +45,6 @@ public MethodDataFetcher(Method method, TypeFunction typeFunction) {
3545
this.typeFunction = typeFunction;
3646
}
3747

38-
@SneakyThrows
3948
@Override
4049
public Object get(DataFetchingEnvironment environment) {
4150
try {
@@ -49,17 +58,16 @@ public Object get(DataFetchingEnvironment environment) {
4958
return null;
5059
}
5160
} else {
52-
obj = method.getDeclaringClass().newInstance();
61+
obj = newInstance(method.getDeclaringClass());
5362
}
5463
return method.invoke(obj, invocationArgs(environment));
5564
} catch (IllegalAccessException | InvocationTargetException e) {
5665
throw new RuntimeException(e);
5766
}
5867
}
5968

60-
@SneakyThrows
6169
private Object[] invocationArgs(DataFetchingEnvironment environment) {
62-
List result = new ArrayList();
70+
List<Object> result = new ArrayList<>();
6371
Iterator envArgs = environment.getArguments().values().iterator();
6472
for (Parameter p : method.getParameters()) {
6573
Class<?> paramType = p.getType();
@@ -69,8 +77,8 @@ private Object[] invocationArgs(DataFetchingEnvironment environment) {
6977
}
7078
graphql.schema.GraphQLType graphQLType = typeFunction.apply(paramType, p.getAnnotatedType());
7179
if (graphQLType instanceof GraphQLObjectType) {
72-
Constructor<?> constructor = paramType.getConstructor(HashMap.class);
73-
result.add(constructor.newInstance(envArgs.next()));
80+
Constructor<?> constructor = constructor(paramType, HashMap.class);
81+
result.add(constructNewInstance(constructor, envArgs.next()));
7482

7583
} else {
7684
result.add(envArgs.next());
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright 2016 Yurii Rashkovskii
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
*/
15+
package graphql.annotations;
16+
17+
import java.lang.reflect.Constructor;
18+
import java.lang.reflect.InvocationTargetException;
19+
20+
/**
21+
* A package level helper in calling reflective methods and turning them into
22+
* GraphQLAnnotationsException runtime exceptions
23+
*/
24+
class ReflectionKit {
25+
static <T> T newInstance(Class<T> clazz) throws GraphQLAnnotationsException {
26+
try {
27+
return clazz.newInstance();
28+
} catch (InstantiationException | IllegalAccessException e) {
29+
throw new GraphQLAnnotationsException("Unable to instantiate class : " + clazz, e);
30+
}
31+
}
32+
33+
static <T> T constructNewInstance(Constructor<T> constructor, Object... args) throws GraphQLAnnotationsException {
34+
try {
35+
return constructor.newInstance(args);
36+
37+
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
38+
throw new GraphQLAnnotationsException("Unable to instantiate via constructor : " + constructor, e);
39+
}
40+
}
41+
42+
static <T> Constructor<T> constructor(Class<T> type, Class<?>... parameterTypes) {
43+
try {
44+
return type.getConstructor(parameterTypes);
45+
} catch (NoSuchMethodException e) {
46+
throw new GraphQLAnnotationsException("Unable to find constructor", e);
47+
}
48+
}
49+
50+
}

src/test/java/graphql/annotations/DefaultTypeFunctionTest.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616

1717
import graphql.schema.*;
1818
import graphql.schema.GraphQLType;
19-
import lombok.SneakyThrows;
2019
import org.testng.annotations.Test;
2120

22-
import java.lang.reflect.AnnotatedParameterizedType;
2321
import java.util.Arrays;
2422
import java.util.LinkedList;
2523
import java.util.List;
@@ -89,15 +87,15 @@ public void long_() {
8987

9088

9189
@SuppressWarnings("unused")
92-
public List<List<@GraphQLNonNull String>> listMethod() { return null;};
90+
public List<List<@GraphQLNonNull String>> listMethod() { return null;}
9391

9492
@SuppressWarnings("unused")
95-
public Stream<List<@GraphQLNonNull String>> streamMethod() { return null;};
93+
public Stream<List<@GraphQLNonNull String>> streamMethod() { return null;}
9694

9795
@Test
9896
public void list() throws NoSuchMethodException {
9997
DefaultTypeFunction instance = new DefaultTypeFunction();
100-
graphql.schema.GraphQLType type = instance.apply(getClass().getMethod("listMethod").getReturnType(), (AnnotatedParameterizedType) getClass().getMethod("listMethod").getAnnotatedReturnType());
98+
graphql.schema.GraphQLType type = instance.apply(getClass().getMethod("listMethod").getReturnType(), getClass().getMethod("listMethod").getAnnotatedReturnType());
10199
assertTrue(type instanceof GraphQLList);
102100
GraphQLList subtype = (GraphQLList) ((GraphQLList) type).getWrappedType();
103101
assertTrue(subtype.getWrappedType() instanceof graphql.schema.GraphQLNonNull);
@@ -115,7 +113,7 @@ public void unparametrizedList() {
115113
@Test
116114
public void stream() throws NoSuchMethodException {
117115
DefaultTypeFunction instance = new DefaultTypeFunction();
118-
graphql.schema.GraphQLType type = instance.apply(getClass().getMethod("streamMethod").getReturnType(), (AnnotatedParameterizedType) getClass().getMethod("listMethod").getAnnotatedReturnType());
116+
graphql.schema.GraphQLType type = instance.apply(getClass().getMethod("streamMethod").getReturnType(), getClass().getMethod("listMethod").getAnnotatedReturnType());
119117
assertTrue(type instanceof GraphQLList);
120118
GraphQLList subtype = (GraphQLList) ((GraphQLList) type).getWrappedType();
121119
assertTrue(subtype.getWrappedType() instanceof graphql.schema.GraphQLNonNull);
@@ -124,12 +122,12 @@ public void stream() throws NoSuchMethodException {
124122
}
125123

126124
@SuppressWarnings("unused")
127-
public Optional<List<@GraphQLNonNull String>> optionalMethod() { return Optional.empty();};
125+
public Optional<List<@GraphQLNonNull String>> optionalMethod() { return Optional.empty();}
128126

129127
@Test
130128
public void optional() throws NoSuchMethodException {
131129
DefaultTypeFunction instance = new DefaultTypeFunction();
132-
graphql.schema.GraphQLType type = instance.apply(getClass().getMethod("optionalMethod").getReturnType(), (AnnotatedParameterizedType) getClass().getMethod("listMethod").getAnnotatedReturnType());
130+
graphql.schema.GraphQLType type = instance.apply(getClass().getMethod("optionalMethod").getReturnType(), getClass().getMethod("listMethod").getAnnotatedReturnType());
133131
assertTrue(type instanceof GraphQLList);
134132
GraphQLType subtype = ((GraphQLList) type).getWrappedType();
135133
assertTrue(subtype instanceof graphql.schema.GraphQLNonNull);
@@ -157,8 +155,8 @@ public static class Class2 {
157155
public Class2 class2;
158156
}
159157

160-
@Test @SneakyThrows
161-
public void recursiveTypes() {
158+
@Test
159+
public void recursiveTypes() throws Exception {
162160
DefaultTypeFunction instance = (DefaultTypeFunction) GraphQLAnnotations.getInstance().defaultTypeFunction;
163161
GraphQLType type = instance.apply(Class1.class, Class2.class.getField("class1").getAnnotatedType());
164162
GraphQLFieldDefinition class1class2 = ((GraphQLObjectType) type).getFieldDefinition("class2");

src/test/java/graphql/annotations/GraphQLBatchedTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import graphql.execution.batched.BatchedExecutionStrategy;
2121
import graphql.schema.GraphQLObjectType;
2222
import graphql.schema.GraphQLSchema;
23-
import lombok.SneakyThrows;
2423
import org.testng.annotations.Test;
2524

2625
import java.util.Arrays;
@@ -32,6 +31,7 @@
3231
import static org.testng.Assert.assertEquals;
3332
import static org.testng.Assert.assertTrue;
3433

34+
@SuppressWarnings("unchecked")
3535
public class GraphQLBatchedTest {
3636
private static class SimpleBatchedField {
3737
@GraphQLField
@@ -49,8 +49,7 @@ public List<SimpleBatchedField> fields() {
4949
}
5050

5151
@Test
52-
@SneakyThrows
53-
public void batchedDataFetcher() {
52+
public void batchedDataFetcher() throws Throwable {
5453
GraphQLObjectType nestedObject = GraphQLAnnotations.object(SimpleBatchedField.class);
5554
assertEquals(nestedObject.getFieldDefinition("a").getType(), GraphQLString);
5655

@@ -77,7 +76,7 @@ public List<String> a() {
7776
}
7877
}
7978

80-
@Test(expectedExceptions = IllegalArgumentException.class) @SneakyThrows
79+
@Test(expectedExceptions = IllegalArgumentException.class)
8180
public void noStaticField() {
8281
GraphQLObjectType object = GraphQLAnnotations.object(NoStaticBatchedField.class);
8382
}
@@ -90,7 +89,7 @@ public String a() {
9089
}
9190
}
9291

93-
@Test(expectedExceptions = IllegalArgumentException.class) @SneakyThrows
92+
@Test(expectedExceptions = IllegalArgumentException.class)
9493
public void noListField() {
9594
GraphQLObjectType object = GraphQLAnnotations.object(NoStaticBatchedField.class);
9695
}
@@ -103,7 +102,7 @@ public List a() {
103102
}
104103
}
105104

106-
@Test(expectedExceptions = IllegalArgumentException.class) @SneakyThrows
105+
@Test(expectedExceptions = IllegalArgumentException.class)
107106
public void noParameterizedReturnField() {
108107
GraphQLObjectType object = GraphQLAnnotations.object(NoStaticBatchedField.class);
109108
}

0 commit comments

Comments
 (0)