diff --git a/graphql-java-client-processor/src/main/java/com/jacobmountain/graphql/client/GraphQLClientProcessor.java b/graphql-java-client-processor/src/main/java/com/jacobmountain/graphql/client/GraphQLClientProcessor.java index 5c08a53..ceb5da6 100644 --- a/graphql-java-client-processor/src/main/java/com/jacobmountain/graphql/client/GraphQLClientProcessor.java +++ b/graphql-java-client-processor/src/main/java/com/jacobmountain/graphql/client/GraphQLClientProcessor.java @@ -115,16 +115,16 @@ String getPackage() { Schema getSchema() { String value = getAnnotation().schema(); + File file = getSchemaFile(); try { if (StringUtils.hasLength(value)) { - File file = getSchemaFile(); log.info("Reading schema {}", file); return new Schema(getSchemaFile()); } } catch (Exception e) { e.printStackTrace(); } - throw new SchemaNotFoundException(); + throw new SchemaNotFoundException(file.getPath()); } File getSchemaFile() { diff --git a/graphql-java-client-processor/src/main/java/com/jacobmountain/graphql/client/PojoBuilder.java b/graphql-java-client-processor/src/main/java/com/jacobmountain/graphql/client/PojoBuilder.java index b2419f5..36ad39a 100644 --- a/graphql-java-client-processor/src/main/java/com/jacobmountain/graphql/client/PojoBuilder.java +++ b/graphql-java-client-processor/src/main/java/com/jacobmountain/graphql/client/PojoBuilder.java @@ -1,11 +1,13 @@ package com.jacobmountain.graphql.client; +import com.fasterxml.jackson.annotation.JsonProperty; import com.jacobmountain.graphql.client.utils.AnnotationUtils; import com.jacobmountain.graphql.client.utils.StringUtils; import com.squareup.javapoet.*; import graphql.language.EnumValueDefinition; import lombok.extern.slf4j.Slf4j; +import javax.lang.model.SourceVersion; import javax.lang.model.element.Modifier; import java.util.ArrayList; import java.util.List; @@ -84,16 +86,28 @@ private PojoBuilder enumeration(String name) { } public PojoBuilder withField(TypeName clazz, String name) { + boolean keyword = SourceVersion.isKeyword(name); + String finalName = name; + if (keyword) { + finalName = "_" + name; + } if (clazz instanceof ClassName) { log.info("\t" + name + ": " + ((ClassName) clazz).simpleName()); } else { log.info("\t" + name + ": " + clazz); } - fields.add(name); + fields.add(finalName); if (!isInterface()) { - builder.addField(clazz, name, Modifier.PRIVATE); + builder.addField( + FieldSpec.builder(clazz, finalName, Modifier.PRIVATE) + .addAnnotation( + AnnotationSpec.builder(JsonProperty.class) + .addMember("value", "\"$L\"", name) + .build() + ).build() + ); } - withAccessors(clazz, name); + withAccessors(clazz, finalName); return this; } @@ -163,18 +177,33 @@ private void generateToString() { this.builder.addMethod(toString); } + private String createGetterName(String variable) { + return StringUtils.camelCase("get", + variable.replaceFirst("_", "") + ); + } + private void withGetter(TypeName clazz, String name) { + String methodName = createGetterName(name); builder.addMethod( - accessorBuilder(StringUtils.camelCase("get", name), "return this.$L", name) + accessorBuilder(methodName, "return this.$L", name) .returns(clazz) .build() ); } + private String createSetterName(String variable) { + return StringUtils.camelCase( + "set", + variable.replaceFirst("_", "") + ); + } + private void withSetter(TypeName clazz, String name) { + String methodName = createSetterName(name); builder.addMethod( - accessorBuilder(StringUtils.camelCase("set", name), "this.$L = $L", name, name) - .addParameter(clazz, name) + accessorBuilder(methodName, "this.$L = $L", name, "set") + .addParameter(clazz, "set") .returns(void.class) .build() ); @@ -184,11 +213,11 @@ private void generateEquals() { if (type == Type.Enum) { return; } - String variable = StringUtils.camelCase(name); + String variable = StringUtils.camelCase("other", name); CodeBlock.Builder equals = CodeBlock.builder(); for (int i = 0; i < fields.size(); i++) { String field = fields.get(i); - equals.add(CodeBlock.of("$T.equals(this.$L, $L.$L())", Objects.class, field, variable, StringUtils.camelCase("get", field))); + equals.add(CodeBlock.of("$T.equals(this.$L, $L.$L())", Objects.class, field, variable, createGetterName(field))); if (i + 1 != fields.size()) { equals.add(" &&\n\t"); } diff --git a/graphql-java-client-processor/src/main/java/com/jacobmountain/graphql/client/exceptions/SchemaNotFoundException.java b/graphql-java-client-processor/src/main/java/com/jacobmountain/graphql/client/exceptions/SchemaNotFoundException.java index 8ace260..3520080 100644 --- a/graphql-java-client-processor/src/main/java/com/jacobmountain/graphql/client/exceptions/SchemaNotFoundException.java +++ b/graphql-java-client-processor/src/main/java/com/jacobmountain/graphql/client/exceptions/SchemaNotFoundException.java @@ -2,8 +2,8 @@ public class SchemaNotFoundException extends RuntimeException { - public SchemaNotFoundException() { - super("Failed to find the graphql schema file"); + public SchemaNotFoundException(String schema) { + super("Failed to find the graphql schema file: " + schema); } } diff --git a/graphql-java-client-processor/src/test/groovy/com/jacobmountain/POJOBuilderSpec.groovy b/graphql-java-client-processor/src/test/groovy/com/jacobmountain/POJOBuilderSpec.groovy index b3bccac..4e87e99 100644 --- a/graphql-java-client-processor/src/test/groovy/com/jacobmountain/POJOBuilderSpec.groovy +++ b/graphql-java-client-processor/src/test/groovy/com/jacobmountain/POJOBuilderSpec.groovy @@ -54,7 +54,7 @@ class POJOBuilderSpec extends Specification { then: hasMethod(build.typeSpec, "setField") - methodMatchesType(build.typeSpec, "setField", VOID, ParameterSpec.builder(ClassName.get(String.class), "field").build()) + methodMatchesType(build.typeSpec, "setField", VOID, ParameterSpec.builder(ClassName.get(String.class), "set").build()) hasMethod(build.typeSpec, "getField") methodMatchesType(build.typeSpec, "getField", ClassName.get(String.class)) } @@ -67,12 +67,11 @@ class POJOBuilderSpec extends Specification { .build() then: - methodMatchesType(build.typeSpec, "setString", VOID, ParameterSpec.builder(ClassName.get(String.class), "string").build()) + methodMatchesType(build.typeSpec, "setString", VOID, ParameterSpec.builder(ClassName.get(String.class), "set").build()) methodMatchesType(build.typeSpec, "getString", ClassName.get(String.class)) - methodMatchesType(build.typeSpec, "setInteger", VOID, ParameterSpec.builder(INT, "integer").build()) + methodMatchesType(build.typeSpec, "setInteger", VOID, ParameterSpec.builder(INT, "set").build()) methodMatchesType(build.typeSpec, "getInteger", INT) - } def "My Class can implement an interface"() { @@ -88,4 +87,15 @@ class POJOBuilderSpec extends Specification { clazz.typeSpec.superinterfaces.contains(ClassName.get("com.jacobmountain", "Interface")) } + def "I can generate a class with a field prefixed with an _"() { + when: + def build = PojoBuilder.newType("MyPojo", "com.jacobmountain") + .withField(ClassName.get(String.class), "_public") + .build() + + then: + methodMatchesType(build.typeSpec, "setPublic", VOID, ParameterSpec.builder(ClassName.get(String.class), "set").build()) + methodMatchesType(build.typeSpec, "getPublic", ClassName.get(String.class)) + } + }