Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#42 java keywords #50

Merged
merged 4 commits into from
Dec 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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()
);
Expand All @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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"() {
Expand All @@ -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))
}

}