Skip to content

Commit

Permalink
Implement LuaMethod builder
Browse files Browse the repository at this point in the history
  • Loading branch information
matshou committed Feb 4, 2021
1 parent 953b3be commit 7ce1ba0
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 177 deletions.
9 changes: 5 additions & 4 deletions src/main/java/io/yooksi/pz/zdoc/compile/LuaCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,17 @@ public Set<ZomboidLuaDoc> compile() throws CompilerException {
Set<LuaMethod> luaMethods = new HashSet<>();
for (IMethod method : javaDoc.getMethods())
{
LuaType returnType = resolveLuaType(method.getReturnType());

List<LuaParameter> parameters = new ArrayList<>();
for (IParameter param : method.getParams())
{
LuaType paramClass = resolveLuaType(param.getType());
parameters.add(new LuaParameter(paramClass, param.getName()));
}
luaMethods.add(new LuaMethod(method.getName(), luaClass, method.getModifier(),
returnType, parameters, method.hasVarArg(), method.getComment()));
luaMethods.add(LuaMethod.Builder.create(method.getName())
.withOwner(luaClass).withModifier(method.getModifier())
.withReturnType(resolveLuaType(method.getReturnType()))
.withParams(parameters).withVarArg(method.hasVarArg())
.withComment(method.getComment()).build());
}
result.add(new ZomboidLuaDoc(luaClass, luaFields, luaMethods));
}
Expand Down
90 changes: 65 additions & 25 deletions src/main/java/io/yooksi/pz/zdoc/element/lua/LuaMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,19 @@ public class LuaMethod implements IMethod, Annotated {
private final List<EmmyLua> annotations;
private final String comment;

public LuaMethod(String name, @Nullable LuaClass owner, MemberModifier modifier,
LuaType returnType, List<LuaParameter> params, boolean hasVarArg, String comment) {
private LuaMethod(Builder builder) {

this.name = EmmyLua.getSafeLuaName(name);
this.owner = owner;
this.returnType = returnType;
this.params = Collections.unmodifiableList(params);
this.modifier = modifier;
this.name = EmmyLua.getSafeLuaName(builder.name);
this.owner = builder.owner;
this.returnType = builder.returnType != null ? builder.returnType : new LuaType("void");
this.modifier = builder.modifier != null ? builder.modifier : MemberModifier.UNDECLARED;
this.params = Collections.unmodifiableList(builder.params);

List<EmmyLua> annotations = new ArrayList<>();
if (!modifier.hasAccess(AccessModifierKey.DEFAULT)) {
annotations.add(new EmmyLuaAccess(modifier.getAccess()));
}
if (hasVarArg)
if (builder.hasVarArg)
{
if (!params.isEmpty())
{
Expand All @@ -74,30 +73,16 @@ public LuaMethod(String name, @Nullable LuaClass owner, MemberModifier modifier,
annotations.add(new EmmyLuaVarArg(params.get(params.size() - 1).getType()));
}
else {
hasVarArg = false;
builder.hasVarArg = false;
Logger.error("Method %s marked with hasVarArg with no parameters", toString());
}
}
else params.forEach(p -> annotations.addAll(p.getAnnotations()));

annotations.add(new EmmyLuaReturn(returnType));
this.annotations = Collections.unmodifiableList(annotations);
this.hasVarArg = hasVarArg;
this.comment = comment;
}

public LuaMethod(String name, @Nullable LuaClass owner, MemberModifier modifier,
LuaType returnType, List<LuaParameter> params, boolean hasVarArg) {
this(name, owner, modifier, returnType, params, hasVarArg, "");
}

public LuaMethod(String name, @Nullable LuaClass owner, MemberModifier modifier,
LuaType returnType, List<LuaParameter> params) {
this(name, owner, modifier, returnType, params, false, "");
}

public LuaMethod(String name, MemberModifier modifier, LuaType returnType, List<LuaParameter> params) {
this(name, null, modifier, returnType, params);
this.hasVarArg = builder.hasVarArg;
this.comment = builder.comment;
}

public void appendParameterSignature(StringBuilder sb) {
Expand Down Expand Up @@ -206,4 +191,59 @@ public int hashCode() {
result = 31 * result + modifier.hashCode();
return 31 * result + (hasVarArg ? 1 : 0);
}

public static class Builder {

private final String name;

private @Nullable LuaClass owner;
private @Nullable MemberModifier modifier;
private @Nullable LuaType returnType;

private List<LuaParameter> params = new ArrayList<>();
private boolean hasVarArg = false;
private String comment = "";

private Builder(String name) {
this.name = name;
}

public static Builder create(String name) {
return new Builder(name);
}

public Builder withOwner(LuaClass owner) {
this.owner = owner;
return this;
}

public Builder withModifier(MemberModifier modifier) {
this.modifier = modifier;
return this;
}

public Builder withReturnType(LuaType returnType) {
this.returnType = returnType;
return this;
}

public Builder withVarArg(boolean hasVarArg) {
this.hasVarArg = hasVarArg;
return this;
}

public Builder withComment(String comment) {
this.comment = comment;
return this;
}

public Builder withParams(List<LuaParameter> params) {
this.params = params;
return this;
}

public LuaMethod build() {
return new LuaMethod(this);
}
}
}
60 changes: 33 additions & 27 deletions src/test/java/io/yooksi/pz/zdoc/compile/LuaCompilerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,17 @@ void shouldCompileValidLuaMethodsFromZomboidJavaDocs() throws CompilerException
)
);
Set<LuaMethod> expectedMethods = ImmutableSet.of(
new LuaMethod("getText", OWNER_CLASS, MODIFIER, new LuaType("String"),
ImmutableList.of(new LuaParameter(new LuaType("Integer"), "iParam"))
),
new LuaMethod("getNumber", OWNER_CLASS, MODIFIER, new LuaType("Integer"),
ImmutableList.of(new LuaParameter(new LuaType("Object"), "oParam"))
),
new LuaMethod("getObject", OWNER_CLASS, MODIFIER, new LuaType("Object"),
ImmutableList.of(new LuaParameter(new LuaType("String"), "sParam"))
)
LuaMethod.Builder.create("getText").withOwner(OWNER_CLASS)
.withModifier(MODIFIER).withReturnType(new LuaType("String")).withParams(
ImmutableList.of(new LuaParameter(new LuaType("Integer"), "iParam"))).build(),

LuaMethod.Builder.create("getNumber").withOwner(OWNER_CLASS)
.withModifier(MODIFIER).withReturnType(new LuaType("Integer")).withParams(
ImmutableList.of(new LuaParameter(new LuaType("Object"), "oParam"))).build(),

LuaMethod.Builder.create("getObject").withOwner(OWNER_CLASS)
.withModifier(MODIFIER).withReturnType(new LuaType("Object")).withParams(
ImmutableList.of(new LuaParameter(new LuaType("String"), "sParam"))).build()
);
ZomboidJavaDoc zJavaDoc = new ZomboidJavaDoc(
new JavaClass(LuaCompilerTest.class), new ArrayList<>(), javaMethods
Expand All @@ -268,15 +270,17 @@ void shouldCompileValidLuaMethodsWithArraysFromZomboidJavaDocs() throws Compiler
)
);
Set<LuaMethod> expectedMethods = ImmutableSet.of(
new LuaMethod("getFloat", OWNER_CLASS, MODIFIER, new LuaType("Float[]"),
ImmutableList.of(new LuaParameter(new LuaType("Integer[]"), "iParam"))
),
new LuaMethod("getDouble", OWNER_CLASS, MODIFIER, new LuaType("Double[]"),
ImmutableList.of(new LuaParameter(new LuaType("Object[]"), "oParam"))
),
new LuaMethod("getByte", OWNER_CLASS, MODIFIER, new LuaType("Byte[]"),
ImmutableList.of(new LuaParameter(new LuaType("String[]"), "sParam"))
)
LuaMethod.Builder.create("getFloat").withOwner(OWNER_CLASS)
.withModifier(MODIFIER).withReturnType(new LuaType("Float[]")).withParams(
ImmutableList.of(new LuaParameter(new LuaType("Integer[]"), "iParam"))).build(),

LuaMethod.Builder.create("getDouble").withOwner(OWNER_CLASS)
.withModifier(MODIFIER).withReturnType(new LuaType("Double[]")).withParams(
ImmutableList.of(new LuaParameter(new LuaType("Object[]"), "oParam"))).build(),

LuaMethod.Builder.create("getByte").withOwner(OWNER_CLASS)
.withModifier(MODIFIER).withReturnType(new LuaType("Byte[]")).withParams(
ImmutableList.of(new LuaParameter(new LuaType("String[]"), "sParam"))).build()
);
ZomboidJavaDoc zJavaDoc = new ZomboidJavaDoc(
new JavaClass(LuaCompilerTest.class), new ArrayList<>(), javaMethods
Expand Down Expand Up @@ -311,15 +315,17 @@ void shouldCompileValidLuaMethodsWithParameterizedTypesFromZomboidJavaDocs() thr
)
);
Set<LuaMethod> expectedMethods = ImmutableSet.of(
new LuaMethod("getText", OWNER_CLASS, MODIFIER, LUA_ARRAY_LIST_OBJECT,
ImmutableList.of(new LuaParameter(LUA_ARRAY_LIST_STRING_OBJECT, "sParam"))
),
new LuaMethod("getNumber", OWNER_CLASS, MODIFIER, LUA_ARRAY_LIST_STRING_OBJECT,
ImmutableList.of(new LuaParameter(LUA_ARRAY_LIST_OBJECT_STRING, "nParam"))
),
new LuaMethod("getObject", OWNER_CLASS, MODIFIER, LUA_ARRAY_LIST_OBJECT_STRING,
ImmutableList.of(new LuaParameter(LUA_ARRAY_LIST_UNKNOWN, "oParam"))
)
LuaMethod.Builder.create("getText").withOwner(OWNER_CLASS)
.withModifier(MODIFIER).withReturnType(LUA_ARRAY_LIST_OBJECT).withParams(
ImmutableList.of(new LuaParameter(LUA_ARRAY_LIST_STRING_OBJECT, "sParam"))).build(),

LuaMethod.Builder.create("getNumber").withOwner(OWNER_CLASS)
.withModifier(MODIFIER).withReturnType(LUA_ARRAY_LIST_STRING_OBJECT).withParams(
ImmutableList.of(new LuaParameter(LUA_ARRAY_LIST_OBJECT_STRING, "nParam"))).build(),

LuaMethod.Builder.create("getObject").withOwner(OWNER_CLASS)
.withModifier(MODIFIER).withReturnType(LUA_ARRAY_LIST_OBJECT_STRING).withParams(
ImmutableList.of(new LuaParameter(LUA_ARRAY_LIST_UNKNOWN, "oParam"))).build()
);
ZomboidJavaDoc zJavaDoc = new ZomboidJavaDoc(
new JavaClass(LuaCompilerTest.class), new ArrayList<>(), javaMethods
Expand Down
Loading

0 comments on commit 7ce1ba0

Please sign in to comment.