From 10b293b476dfdc41b4f41041d1431a4631c5e2ed Mon Sep 17 00:00:00 2001 From: summerji Date: Mon, 27 Jul 2020 13:47:10 -0700 Subject: [PATCH 01/16] Add ThisObjectValue --- .../generator/engine/ast/ThisObjectValue.java | 41 +++++++++++++++++++ .../engine/ast/ThisObjectValueTest.java | 7 ++++ 2 files changed, 48 insertions(+) create mode 100644 src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java create mode 100644 src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java diff --git a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java new file mode 100644 index 0000000000..05265bc1d0 --- /dev/null +++ b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java @@ -0,0 +1,41 @@ +package com.google.api.generator.engine.ast; + +import com.google.auto.value.AutoValue; +import com.google.common.base.Preconditions; + +@AutoValue +public abstract class ThisObjectValue implements ObjectValue { + private static final String THIS_VALUE = "this"; + public abstract TypeNode type(); + + @Override + public String value() {return THIS_VALUE;} + + public static ThisObjectValue withType(TypeNode type) { + return builder().setType(type).build(); + } + + private static Builder builder() { + return new AutoValue_ThisObjectValue.Builder(); + } + + @AutoValue.Builder + public abstract static class Builder { + public abstract Builder setType(TypeNode type); + + public abstract ThisObjectValue autoBuild(); + + public ThisObjectValue build() { + ThisObjectValue thisObjectValue = autoBuild(); + Preconditions.checkState( + TypeNode.isReferenceType(thisObjectValue.type()), + "this can only refer to object types" + ); + Preconditions.checkState( + !thisObjectValue.type().equals(TypeNode.NULL), + "this can not refer to null type." + ); + return thisObjectValue; + } + } +} diff --git a/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java b/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java new file mode 100644 index 0000000000..d477160f3b --- /dev/null +++ b/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java @@ -0,0 +1,7 @@ +package com.google.api.generator.engine.ast; + +public class ThisObjectValueTest { + + + +} From faa93675328f15fe76515f33b239c798cd3b2c91 Mon Sep 17 00:00:00 2001 From: summerji Date: Tue, 28 Jul 2020 01:34:03 -0700 Subject: [PATCH 02/16] Add unit test --- .../generator/engine/ast/ThisObjectValue.java | 4 +- .../api/generator/engine/ast/TypeNode.java | 5 +++ .../api/generator/engine/ast/BUILD.bazel | 1 + .../engine/ast/ThisObjectValueTest.java | 39 ++++++++++++++++++- 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java index 05265bc1d0..8511f87dd7 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java +++ b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java @@ -32,8 +32,8 @@ public ThisObjectValue build() { "this can only refer to object types" ); Preconditions.checkState( - !thisObjectValue.type().equals(TypeNode.NULL), - "this can not refer to null type." + !TypeNode.isJavaLang(thisObjectValue.type()), + "The class type should belongs to custom object" ); return thisObjectValue; } diff --git a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java index bdc7837ac2..59a23a4c29 100644 --- a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java +++ b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java @@ -16,6 +16,7 @@ import com.google.auto.value.AutoValue; import com.google.common.base.Preconditions; +import java.lang.reflect.Type; import java.util.Objects; import javax.annotation.Nullable; @@ -105,6 +106,10 @@ public boolean isPrimitiveType() { return isPrimitiveType(typeKind()); } + public static boolean isJavaLang(TypeNode type) { + return type.reference().fullName().startsWith("java.lang"); + } + public boolean isSupertypeOrEquals(TypeNode other) { return !isPrimitiveType() && !other.isPrimitiveType() diff --git a/src/test/java/com/google/api/generator/engine/ast/BUILD.bazel b/src/test/java/com/google/api/generator/engine/ast/BUILD.bazel index 70e53dd433..88ed8a8fdd 100644 --- a/src/test/java/com/google/api/generator/engine/ast/BUILD.bazel +++ b/src/test/java/com/google/api/generator/engine/ast/BUILD.bazel @@ -25,6 +25,7 @@ TESTS = [ "VariableTest", "VaporReferenceTest", "MethodInvocationExprTest", + "ThisObjectValueTest", ] filegroup( diff --git a/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java b/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java index d477160f3b..b591979afb 100644 --- a/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java @@ -1,7 +1,42 @@ package com.google.api.generator.engine.ast; -public class ThisObjectValueTest { +import static org.junit.Assert.assertThrows; +import org.junit.Assert; +import org.junit.Test; +public class ThisObjectValueTest{ + @Test + public void testThisObjectValueTest() { + VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.examples.v1").build(); + TypeNode typeNode = TypeNode.withReference(ref); + ThisObjectValue thisObjectValue = ThisObjectValue.withType(TypeNode.withReference(ref)); -} + Assert.assertEquals(thisObjectValue.value(), "this"); + Assert.assertEquals(thisObjectValue.type(), typeNode); + } + + @Test + public void testThisObjectValueTest_invalid() { + ClassDefinition classDefinition = ClassDefinition.builder() + .setPackageString("com.google.example.library.v1.stub") + .setName("LibraryServiceStub") + .setScope(ScopeNode.PUBLIC) + .build(); + ConcreteReference ref = ConcreteReference.withClazz(classDefinition.getClass()); + TypeNode typeNode = TypeNode.withReference(ref); + ThisObjectValue thisObjectValue = ThisObjectValue.withType(TypeNode.withReference(ref)); + Assert.assertEquals(thisObjectValue.value(), "this"); + Assert.assertEquals(thisObjectValue.type(), typeNode); + } + + @Test + public void testThisObjectValueTest_invalid_String() { + ConcreteReference ref = ConcreteReference.withClazz(Integer.class); + assertThrows( + IllegalStateException.class, + () -> { + ThisObjectValue.withType(TypeNode.withReference(ref)); + }); + } +} \ No newline at end of file From 35eb16aac6d88b933d1adb45674f6fae91913253 Mon Sep 17 00:00:00 2001 From: summerji Date: Tue, 28 Jul 2020 11:43:11 -0700 Subject: [PATCH 03/16] Add ThisObjectValue usage example in writervisitor test --- .../generator/engine/ast/ThisObjectValue.java | 12 ++--- .../api/generator/engine/ast/TypeNode.java | 1 - .../engine/ast/ThisObjectValueTest.java | 28 ++++++---- .../engine/writer/JavaWriterVisitorTest.java | 51 +++++++++++++++++++ 4 files changed, 76 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java index 8511f87dd7..509c04bcd1 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java +++ b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java @@ -6,10 +6,13 @@ @AutoValue public abstract class ThisObjectValue implements ObjectValue { private static final String THIS_VALUE = "this"; + public abstract TypeNode type(); @Override - public String value() {return THIS_VALUE;} + public String value() { + return THIS_VALUE; + } public static ThisObjectValue withType(TypeNode type) { return builder().setType(type).build(); @@ -28,13 +31,10 @@ public abstract static class Builder { public ThisObjectValue build() { ThisObjectValue thisObjectValue = autoBuild(); Preconditions.checkState( - TypeNode.isReferenceType(thisObjectValue.type()), - "this can only refer to object types" - ); + TypeNode.isReferenceType(thisObjectValue.type()), "this can only refer to object types"); Preconditions.checkState( !TypeNode.isJavaLang(thisObjectValue.type()), - "The class type should belongs to custom object" - ); + "The class type should belongs to custom object"); return thisObjectValue; } } diff --git a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java index 59a23a4c29..f7372a9e7d 100644 --- a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java +++ b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java @@ -16,7 +16,6 @@ import com.google.auto.value.AutoValue; import com.google.common.base.Preconditions; -import java.lang.reflect.Type; import java.util.Objects; import javax.annotation.Nullable; diff --git a/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java b/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java index b591979afb..3c832cf5fa 100644 --- a/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java @@ -5,10 +5,14 @@ import org.junit.Assert; import org.junit.Test; -public class ThisObjectValueTest{ +public class ThisObjectValueTest { @Test public void testThisObjectValueTest() { - VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.examples.v1").build(); + VaporReference ref = + VaporReference.builder() + .setName("Student") + .setPakkage("com.google.example.examples.v1") + .build(); TypeNode typeNode = TypeNode.withReference(ref); ThisObjectValue thisObjectValue = ThisObjectValue.withType(TypeNode.withReference(ref)); @@ -18,11 +22,12 @@ public void testThisObjectValueTest() { @Test public void testThisObjectValueTest_invalid() { - ClassDefinition classDefinition = ClassDefinition.builder() - .setPackageString("com.google.example.library.v1.stub") - .setName("LibraryServiceStub") - .setScope(ScopeNode.PUBLIC) - .build(); + ClassDefinition classDefinition = + ClassDefinition.builder() + .setPackageString("com.google.example.library.v1.stub") + .setName("LibraryServiceStub") + .setScope(ScopeNode.PUBLIC) + .build(); ConcreteReference ref = ConcreteReference.withClazz(classDefinition.getClass()); TypeNode typeNode = TypeNode.withReference(ref); ThisObjectValue thisObjectValue = ThisObjectValue.withType(TypeNode.withReference(ref)); @@ -31,12 +36,17 @@ public void testThisObjectValueTest_invalid() { } @Test - public void testThisObjectValueTest_invalid_String() { + public void testThisObjectValueTest_invalid_type() { ConcreteReference ref = ConcreteReference.withClazz(Integer.class); assertThrows( IllegalStateException.class, () -> { ThisObjectValue.withType(TypeNode.withReference(ref)); }); + assertThrows( + IllegalStateException.class, + () -> { + ThisObjectValue.withType(TypeNode.BOOLEAN); + }); } -} \ No newline at end of file +} diff --git a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java index 153fdb78d3..3d848f3558 100644 --- a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java @@ -44,6 +44,7 @@ import com.google.api.generator.engine.ast.Statement; import com.google.api.generator.engine.ast.StringObjectValue; import com.google.api.generator.engine.ast.TernaryExpr; +import com.google.api.generator.engine.ast.ThisObjectValue; import com.google.api.generator.engine.ast.ThrowExpr; import com.google.api.generator.engine.ast.TryCatchStatement; import com.google.api.generator.engine.ast.TypeNode; @@ -1638,6 +1639,56 @@ public void writeClassDefinition_statementsAndMethods() { "}\n")); } + @Test + public void writeMethodReturnThisObjectValue() { + VaporReference ref = + VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build(); + TypeNode classType = TypeNode.withReference(ref); + MethodDefinition methodDefinition = + MethodDefinition.builder() + .setName("apply") + .setScope(ScopeNode.PUBLIC) + .setReturnType(TypeNode.withReference(ref)) + .setReturnExpr( + ValueExpr.builder().setValue(ThisObjectValue.withType(classType)).build()) + .build(); + methodDefinition.accept(writerVisitor); + assertEquals( + writerVisitor.write(), + String.format("public Student apply() {\n" + "return this;\n" + "}\n")); + } + + @Test + public void writeExprStatementForThisObjectValue() { + // Example of ThisObjectValue access its fields + VaporReference ref = + VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build(); + TypeNode classType = TypeNode.withReference(ref); + ThisObjectValue thisObjectValue = ThisObjectValue.withType(classType); + ValueExpr thisValueExpr = ValueExpr.withValue(thisObjectValue); + VariableExpr varExpr = + VariableExpr.builder() + .setVariable(Variable.builder().setName("id").setType(TypeNode.STRING).build()) + .build(); + Variable subVariable = Variable.builder().setName("name").setType(TypeNode.STRING).build(); + VariableExpr thisVariableExpr = + VariableExpr.builder().setVariable(subVariable).setExprReferenceExpr(thisValueExpr).build(); + + // Example of ThisObjectValue invoke its method with arguments + MethodInvocationExpr methodExpr = + MethodInvocationExpr.builder() + .setMethodName("getName") + .setExprReferenceExpr(ValueExpr.withValue(thisObjectValue)) + .setArguments(Arrays.asList(varExpr)) + .setReturnType(TypeNode.STRING) + .build(); + AssignmentExpr assignmentExpr = + AssignmentExpr.builder().setVariableExpr(thisVariableExpr).setValueExpr(methodExpr).build(); + + assignmentExpr.accept(writerVisitor); + assertThat(writerVisitor.write()).isEqualTo("this.name = this.getName(id)"); + } + private static String createLines(int numLines) { return new String(new char[numLines]).replace("\0", "%s"); } From 434b8d835a3e525329eab45821221b13b92252da Mon Sep 17 00:00:00 2001 From: summerji Date: Tue, 28 Jul 2020 11:47:22 -0700 Subject: [PATCH 04/16] Add license --- .../api/generator/engine/ast/ThisObjectValue.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java index 509c04bcd1..bac3c7f574 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java +++ b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java @@ -1,3 +1,17 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package com.google.api.generator.engine.ast; import com.google.auto.value.AutoValue; From 3b77f643cecb5e6b16a513c1721f6edc3a299b1c Mon Sep 17 00:00:00 2001 From: summerji Date: Tue, 28 Jul 2020 13:45:24 -0700 Subject: [PATCH 05/16] add license and rename a function --- .../api/generator/engine/ast/ThisObjectValue.java | 2 +- .../google/api/generator/engine/ast/TypeNode.java | 2 +- .../generator/engine/ast/ThisObjectValueTest.java | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java index bac3c7f574..e7d820f22f 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java +++ b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java @@ -47,7 +47,7 @@ public ThisObjectValue build() { Preconditions.checkState( TypeNode.isReferenceType(thisObjectValue.type()), "this can only refer to object types"); Preconditions.checkState( - !TypeNode.isJavaLang(thisObjectValue.type()), + !TypeNode.isJavaLangObject(thisObjectValue.type()), "The class type should belongs to custom object"); return thisObjectValue; } diff --git a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java index f7372a9e7d..7b793997d5 100644 --- a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java +++ b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java @@ -105,7 +105,7 @@ public boolean isPrimitiveType() { return isPrimitiveType(typeKind()); } - public static boolean isJavaLang(TypeNode type) { + public static boolean isJavaLangObject(TypeNode type) { return type.reference().fullName().startsWith("java.lang"); } diff --git a/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java b/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java index 3c832cf5fa..a717dda7ec 100644 --- a/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java @@ -1,3 +1,17 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package com.google.api.generator.engine.ast; import static org.junit.Assert.assertThrows; From 121f405710fd9294fb88b5d95fd8534596b7a491 Mon Sep 17 00:00:00 2001 From: summerji Date: Mon, 27 Jul 2020 13:47:10 -0700 Subject: [PATCH 06/16] Add ThisObjectValue --- .../generator/engine/ast/ThisObjectValue.java | 41 +++++++++++++++++++ .../engine/ast/ThisObjectValueTest.java | 7 ++++ 2 files changed, 48 insertions(+) create mode 100644 src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java create mode 100644 src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java diff --git a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java new file mode 100644 index 0000000000..05265bc1d0 --- /dev/null +++ b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java @@ -0,0 +1,41 @@ +package com.google.api.generator.engine.ast; + +import com.google.auto.value.AutoValue; +import com.google.common.base.Preconditions; + +@AutoValue +public abstract class ThisObjectValue implements ObjectValue { + private static final String THIS_VALUE = "this"; + public abstract TypeNode type(); + + @Override + public String value() {return THIS_VALUE;} + + public static ThisObjectValue withType(TypeNode type) { + return builder().setType(type).build(); + } + + private static Builder builder() { + return new AutoValue_ThisObjectValue.Builder(); + } + + @AutoValue.Builder + public abstract static class Builder { + public abstract Builder setType(TypeNode type); + + public abstract ThisObjectValue autoBuild(); + + public ThisObjectValue build() { + ThisObjectValue thisObjectValue = autoBuild(); + Preconditions.checkState( + TypeNode.isReferenceType(thisObjectValue.type()), + "this can only refer to object types" + ); + Preconditions.checkState( + !thisObjectValue.type().equals(TypeNode.NULL), + "this can not refer to null type." + ); + return thisObjectValue; + } + } +} diff --git a/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java b/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java new file mode 100644 index 0000000000..d477160f3b --- /dev/null +++ b/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java @@ -0,0 +1,7 @@ +package com.google.api.generator.engine.ast; + +public class ThisObjectValueTest { + + + +} From 9c5fb8bdf35831736278b18b10a76820a35780dc Mon Sep 17 00:00:00 2001 From: summerji Date: Tue, 28 Jul 2020 01:34:03 -0700 Subject: [PATCH 07/16] Add unit test --- .../generator/engine/ast/ThisObjectValue.java | 4 +- .../api/generator/engine/ast/TypeNode.java | 5 +++ .../api/generator/engine/ast/BUILD.bazel | 1 + .../engine/ast/ThisObjectValueTest.java | 39 ++++++++++++++++++- 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java index 05265bc1d0..8511f87dd7 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java +++ b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java @@ -32,8 +32,8 @@ public ThisObjectValue build() { "this can only refer to object types" ); Preconditions.checkState( - !thisObjectValue.type().equals(TypeNode.NULL), - "this can not refer to null type." + !TypeNode.isJavaLang(thisObjectValue.type()), + "The class type should belongs to custom object" ); return thisObjectValue; } diff --git a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java index bdc7837ac2..59a23a4c29 100644 --- a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java +++ b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java @@ -16,6 +16,7 @@ import com.google.auto.value.AutoValue; import com.google.common.base.Preconditions; +import java.lang.reflect.Type; import java.util.Objects; import javax.annotation.Nullable; @@ -105,6 +106,10 @@ public boolean isPrimitiveType() { return isPrimitiveType(typeKind()); } + public static boolean isJavaLang(TypeNode type) { + return type.reference().fullName().startsWith("java.lang"); + } + public boolean isSupertypeOrEquals(TypeNode other) { return !isPrimitiveType() && !other.isPrimitiveType() diff --git a/src/test/java/com/google/api/generator/engine/ast/BUILD.bazel b/src/test/java/com/google/api/generator/engine/ast/BUILD.bazel index 70e53dd433..88ed8a8fdd 100644 --- a/src/test/java/com/google/api/generator/engine/ast/BUILD.bazel +++ b/src/test/java/com/google/api/generator/engine/ast/BUILD.bazel @@ -25,6 +25,7 @@ TESTS = [ "VariableTest", "VaporReferenceTest", "MethodInvocationExprTest", + "ThisObjectValueTest", ] filegroup( diff --git a/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java b/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java index d477160f3b..b591979afb 100644 --- a/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java @@ -1,7 +1,42 @@ package com.google.api.generator.engine.ast; -public class ThisObjectValueTest { +import static org.junit.Assert.assertThrows; +import org.junit.Assert; +import org.junit.Test; +public class ThisObjectValueTest{ + @Test + public void testThisObjectValueTest() { + VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.examples.v1").build(); + TypeNode typeNode = TypeNode.withReference(ref); + ThisObjectValue thisObjectValue = ThisObjectValue.withType(TypeNode.withReference(ref)); -} + Assert.assertEquals(thisObjectValue.value(), "this"); + Assert.assertEquals(thisObjectValue.type(), typeNode); + } + + @Test + public void testThisObjectValueTest_invalid() { + ClassDefinition classDefinition = ClassDefinition.builder() + .setPackageString("com.google.example.library.v1.stub") + .setName("LibraryServiceStub") + .setScope(ScopeNode.PUBLIC) + .build(); + ConcreteReference ref = ConcreteReference.withClazz(classDefinition.getClass()); + TypeNode typeNode = TypeNode.withReference(ref); + ThisObjectValue thisObjectValue = ThisObjectValue.withType(TypeNode.withReference(ref)); + Assert.assertEquals(thisObjectValue.value(), "this"); + Assert.assertEquals(thisObjectValue.type(), typeNode); + } + + @Test + public void testThisObjectValueTest_invalid_String() { + ConcreteReference ref = ConcreteReference.withClazz(Integer.class); + assertThrows( + IllegalStateException.class, + () -> { + ThisObjectValue.withType(TypeNode.withReference(ref)); + }); + } +} \ No newline at end of file From 29fa1a05ab5d8c456d77f511c65f4ae95af23ba0 Mon Sep 17 00:00:00 2001 From: summerji Date: Tue, 28 Jul 2020 11:43:11 -0700 Subject: [PATCH 08/16] Add ThisObjectValue usage example in writervisitor test --- .../generator/engine/ast/ThisObjectValue.java | 12 ++--- .../api/generator/engine/ast/TypeNode.java | 1 - .../engine/ast/ThisObjectValueTest.java | 28 ++++++---- .../engine/writer/JavaWriterVisitorTest.java | 51 +++++++++++++++++++ 4 files changed, 76 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java index 8511f87dd7..509c04bcd1 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java +++ b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java @@ -6,10 +6,13 @@ @AutoValue public abstract class ThisObjectValue implements ObjectValue { private static final String THIS_VALUE = "this"; + public abstract TypeNode type(); @Override - public String value() {return THIS_VALUE;} + public String value() { + return THIS_VALUE; + } public static ThisObjectValue withType(TypeNode type) { return builder().setType(type).build(); @@ -28,13 +31,10 @@ public abstract static class Builder { public ThisObjectValue build() { ThisObjectValue thisObjectValue = autoBuild(); Preconditions.checkState( - TypeNode.isReferenceType(thisObjectValue.type()), - "this can only refer to object types" - ); + TypeNode.isReferenceType(thisObjectValue.type()), "this can only refer to object types"); Preconditions.checkState( !TypeNode.isJavaLang(thisObjectValue.type()), - "The class type should belongs to custom object" - ); + "The class type should belongs to custom object"); return thisObjectValue; } } diff --git a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java index 59a23a4c29..f7372a9e7d 100644 --- a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java +++ b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java @@ -16,7 +16,6 @@ import com.google.auto.value.AutoValue; import com.google.common.base.Preconditions; -import java.lang.reflect.Type; import java.util.Objects; import javax.annotation.Nullable; diff --git a/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java b/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java index b591979afb..3c832cf5fa 100644 --- a/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java @@ -5,10 +5,14 @@ import org.junit.Assert; import org.junit.Test; -public class ThisObjectValueTest{ +public class ThisObjectValueTest { @Test public void testThisObjectValueTest() { - VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.examples.v1").build(); + VaporReference ref = + VaporReference.builder() + .setName("Student") + .setPakkage("com.google.example.examples.v1") + .build(); TypeNode typeNode = TypeNode.withReference(ref); ThisObjectValue thisObjectValue = ThisObjectValue.withType(TypeNode.withReference(ref)); @@ -18,11 +22,12 @@ public void testThisObjectValueTest() { @Test public void testThisObjectValueTest_invalid() { - ClassDefinition classDefinition = ClassDefinition.builder() - .setPackageString("com.google.example.library.v1.stub") - .setName("LibraryServiceStub") - .setScope(ScopeNode.PUBLIC) - .build(); + ClassDefinition classDefinition = + ClassDefinition.builder() + .setPackageString("com.google.example.library.v1.stub") + .setName("LibraryServiceStub") + .setScope(ScopeNode.PUBLIC) + .build(); ConcreteReference ref = ConcreteReference.withClazz(classDefinition.getClass()); TypeNode typeNode = TypeNode.withReference(ref); ThisObjectValue thisObjectValue = ThisObjectValue.withType(TypeNode.withReference(ref)); @@ -31,12 +36,17 @@ public void testThisObjectValueTest_invalid() { } @Test - public void testThisObjectValueTest_invalid_String() { + public void testThisObjectValueTest_invalid_type() { ConcreteReference ref = ConcreteReference.withClazz(Integer.class); assertThrows( IllegalStateException.class, () -> { ThisObjectValue.withType(TypeNode.withReference(ref)); }); + assertThrows( + IllegalStateException.class, + () -> { + ThisObjectValue.withType(TypeNode.BOOLEAN); + }); } -} \ No newline at end of file +} diff --git a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java index 0188c74797..7799feff78 100644 --- a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java @@ -44,6 +44,7 @@ import com.google.api.generator.engine.ast.Statement; import com.google.api.generator.engine.ast.StringObjectValue; import com.google.api.generator.engine.ast.TernaryExpr; +import com.google.api.generator.engine.ast.ThisObjectValue; import com.google.api.generator.engine.ast.ThrowExpr; import com.google.api.generator.engine.ast.TryCatchStatement; import com.google.api.generator.engine.ast.TypeNode; @@ -1654,6 +1655,56 @@ public void writeClassDefinition_statementsAndMethods() { "}\n")); } + @Test + public void writeMethodReturnThisObjectValue() { + VaporReference ref = + VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build(); + TypeNode classType = TypeNode.withReference(ref); + MethodDefinition methodDefinition = + MethodDefinition.builder() + .setName("apply") + .setScope(ScopeNode.PUBLIC) + .setReturnType(TypeNode.withReference(ref)) + .setReturnExpr( + ValueExpr.builder().setValue(ThisObjectValue.withType(classType)).build()) + .build(); + methodDefinition.accept(writerVisitor); + assertEquals( + writerVisitor.write(), + String.format("public Student apply() {\n" + "return this;\n" + "}\n")); + } + + @Test + public void writeExprStatementForThisObjectValue() { + // Example of ThisObjectValue access its fields + VaporReference ref = + VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build(); + TypeNode classType = TypeNode.withReference(ref); + ThisObjectValue thisObjectValue = ThisObjectValue.withType(classType); + ValueExpr thisValueExpr = ValueExpr.withValue(thisObjectValue); + VariableExpr varExpr = + VariableExpr.builder() + .setVariable(Variable.builder().setName("id").setType(TypeNode.STRING).build()) + .build(); + Variable subVariable = Variable.builder().setName("name").setType(TypeNode.STRING).build(); + VariableExpr thisVariableExpr = + VariableExpr.builder().setVariable(subVariable).setExprReferenceExpr(thisValueExpr).build(); + + // Example of ThisObjectValue invoke its method with arguments + MethodInvocationExpr methodExpr = + MethodInvocationExpr.builder() + .setMethodName("getName") + .setExprReferenceExpr(ValueExpr.withValue(thisObjectValue)) + .setArguments(Arrays.asList(varExpr)) + .setReturnType(TypeNode.STRING) + .build(); + AssignmentExpr assignmentExpr = + AssignmentExpr.builder().setVariableExpr(thisVariableExpr).setValueExpr(methodExpr).build(); + + assignmentExpr.accept(writerVisitor); + assertThat(writerVisitor.write()).isEqualTo("this.name = this.getName(id)"); + } + private static String createLines(int numLines) { return new String(new char[numLines]).replace("\0", "%s"); } From 13b842ed04b4f3081b7f91cb55210638cb042bf2 Mon Sep 17 00:00:00 2001 From: summerji Date: Tue, 28 Jul 2020 11:47:22 -0700 Subject: [PATCH 09/16] Add license --- .../api/generator/engine/ast/ThisObjectValue.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java index 509c04bcd1..bac3c7f574 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java +++ b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java @@ -1,3 +1,17 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package com.google.api.generator.engine.ast; import com.google.auto.value.AutoValue; From 2f1d1b448f170e600e5657ad403cf2e5d3bdf880 Mon Sep 17 00:00:00 2001 From: summerji Date: Tue, 28 Jul 2020 13:45:24 -0700 Subject: [PATCH 10/16] add license and rename a function --- .../api/generator/engine/ast/ThisObjectValue.java | 2 +- .../google/api/generator/engine/ast/TypeNode.java | 2 +- .../generator/engine/ast/ThisObjectValueTest.java | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java index bac3c7f574..e7d820f22f 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java +++ b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java @@ -47,7 +47,7 @@ public ThisObjectValue build() { Preconditions.checkState( TypeNode.isReferenceType(thisObjectValue.type()), "this can only refer to object types"); Preconditions.checkState( - !TypeNode.isJavaLang(thisObjectValue.type()), + !TypeNode.isJavaLangObject(thisObjectValue.type()), "The class type should belongs to custom object"); return thisObjectValue; } diff --git a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java index f7372a9e7d..7b793997d5 100644 --- a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java +++ b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java @@ -105,7 +105,7 @@ public boolean isPrimitiveType() { return isPrimitiveType(typeKind()); } - public static boolean isJavaLang(TypeNode type) { + public static boolean isJavaLangObject(TypeNode type) { return type.reference().fullName().startsWith("java.lang"); } diff --git a/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java b/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java index 3c832cf5fa..a717dda7ec 100644 --- a/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java @@ -1,3 +1,17 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package com.google.api.generator.engine.ast; import static org.junit.Assert.assertThrows; From f3b533d4c78799ddaf8a3ac8e16be420eeb531d4 Mon Sep 17 00:00:00 2001 From: summerji Date: Tue, 28 Jul 2020 14:42:59 -0700 Subject: [PATCH 11/16] use buildin function --- .../com/google/api/generator/engine/ast/ThisObjectValue.java | 3 ++- .../java/com/google/api/generator/engine/ast/TypeNode.java | 4 ---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java index e7d820f22f..e6ecb2dc10 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java +++ b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java @@ -20,6 +20,7 @@ @AutoValue public abstract class ThisObjectValue implements ObjectValue { private static final String THIS_VALUE = "this"; + private static final String PKG_JAVA_LANG = "java.lang"; public abstract TypeNode type(); @@ -47,7 +48,7 @@ public ThisObjectValue build() { Preconditions.checkState( TypeNode.isReferenceType(thisObjectValue.type()), "this can only refer to object types"); Preconditions.checkState( - !TypeNode.isJavaLangObject(thisObjectValue.type()), + !thisObjectValue.type().reference().isFromPackage(PKG_JAVA_LANG), "The class type should belongs to custom object"); return thisObjectValue; } diff --git a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java index 7b793997d5..bdc7837ac2 100644 --- a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java +++ b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java @@ -105,10 +105,6 @@ public boolean isPrimitiveType() { return isPrimitiveType(typeKind()); } - public static boolean isJavaLangObject(TypeNode type) { - return type.reference().fullName().startsWith("java.lang"); - } - public boolean isSupertypeOrEquals(TypeNode other) { return !isPrimitiveType() && !other.isPrimitiveType() From 109cf32b73ee08cfce66102139f8e5b288be0aa0 Mon Sep 17 00:00:00 2001 From: summerji Date: Tue, 28 Jul 2020 14:47:34 -0700 Subject: [PATCH 12/16] fix the merge msg --- .../google/api/generator/engine/ast/ThisObjectValue.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java index 2f05197900..e6ecb2dc10 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java +++ b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java @@ -20,10 +20,7 @@ @AutoValue public abstract class ThisObjectValue implements ObjectValue { private static final String THIS_VALUE = "this"; -<<<<<<< HEAD private static final String PKG_JAVA_LANG = "java.lang"; -======= ->>>>>>> 3b77f643cecb5e6b16a513c1721f6edc3a299b1c public abstract TypeNode type(); @@ -51,11 +48,7 @@ public ThisObjectValue build() { Preconditions.checkState( TypeNode.isReferenceType(thisObjectValue.type()), "this can only refer to object types"); Preconditions.checkState( -<<<<<<< HEAD !thisObjectValue.type().reference().isFromPackage(PKG_JAVA_LANG), -======= - !TypeNode.isJavaLangObject(thisObjectValue.type()), ->>>>>>> 3b77f643cecb5e6b16a513c1721f6edc3a299b1c "The class type should belongs to custom object"); return thisObjectValue; } From 2af92af0bcb0b769b22e049d8e957d2e3e48c4fe Mon Sep 17 00:00:00 2001 From: summerji Date: Wed, 29 Jul 2020 00:36:05 -0700 Subject: [PATCH 13/16] Address the comments --- .../generator/engine/ast/ThisObjectValue.java | 12 +++---- .../api/generator/engine/ast/TypeNode.java | 4 --- .../engine/ast/ThisObjectValueTest.java | 35 ++++++------------- .../engine/writer/JavaWriterVisitorTest.java | 6 ++-- 4 files changed, 16 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java index e6ecb2dc10..570b0f0dc5 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java +++ b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java @@ -20,7 +20,6 @@ @AutoValue public abstract class ThisObjectValue implements ObjectValue { private static final String THIS_VALUE = "this"; - private static final String PKG_JAVA_LANG = "java.lang"; public abstract TypeNode type(); @@ -38,18 +37,15 @@ private static Builder builder() { } @AutoValue.Builder - public abstract static class Builder { - public abstract Builder setType(TypeNode type); + abstract static class Builder { + abstract Builder setType(TypeNode type); - public abstract ThisObjectValue autoBuild(); + abstract ThisObjectValue autoBuild(); - public ThisObjectValue build() { + private ThisObjectValue build() { ThisObjectValue thisObjectValue = autoBuild(); Preconditions.checkState( TypeNode.isReferenceType(thisObjectValue.type()), "this can only refer to object types"); - Preconditions.checkState( - !thisObjectValue.type().reference().isFromPackage(PKG_JAVA_LANG), - "The class type should belongs to custom object"); return thisObjectValue; } } diff --git a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java index 7b793997d5..bdc7837ac2 100644 --- a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java +++ b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java @@ -105,10 +105,6 @@ public boolean isPrimitiveType() { return isPrimitiveType(typeKind()); } - public static boolean isJavaLangObject(TypeNode type) { - return type.reference().fullName().startsWith("java.lang"); - } - public boolean isSupertypeOrEquals(TypeNode other) { return !isPrimitiveType() && !other.isPrimitiveType() diff --git a/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java b/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java index a717dda7ec..dd6e5abfa8 100644 --- a/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java @@ -16,51 +16,36 @@ import static org.junit.Assert.assertThrows; -import org.junit.Assert; import org.junit.Test; public class ThisObjectValueTest { @Test - public void testThisObjectValueTest() { + public void validThisObjectValue_basic() { VaporReference ref = VaporReference.builder() .setName("Student") .setPakkage("com.google.example.examples.v1") .build(); TypeNode typeNode = TypeNode.withReference(ref); - ThisObjectValue thisObjectValue = ThisObjectValue.withType(TypeNode.withReference(ref)); - - Assert.assertEquals(thisObjectValue.value(), "this"); - Assert.assertEquals(thisObjectValue.type(), typeNode); + ThisObjectValue.withType(typeNode); + // No exception thrown, we're good. } @Test - public void testThisObjectValueTest_invalid() { - ClassDefinition classDefinition = - ClassDefinition.builder() - .setPackageString("com.google.example.library.v1.stub") - .setName("LibraryServiceStub") - .setScope(ScopeNode.PUBLIC) - .build(); - ConcreteReference ref = ConcreteReference.withClazz(classDefinition.getClass()); - TypeNode typeNode = TypeNode.withReference(ref); - ThisObjectValue thisObjectValue = ThisObjectValue.withType(TypeNode.withReference(ref)); - Assert.assertEquals(thisObjectValue.value(), "this"); - Assert.assertEquals(thisObjectValue.type(), typeNode); - } - - @Test - public void testThisObjectValueTest_invalid_type() { - ConcreteReference ref = ConcreteReference.withClazz(Integer.class); + public void invalidThisObjectValue_NonReferenceType() { assertThrows( IllegalStateException.class, () -> { - ThisObjectValue.withType(TypeNode.withReference(ref)); + ThisObjectValue.withType(TypeNode.DOUBLE); }); + } + + @Test + public void invalidThisObjectValue_NullType() { assertThrows( IllegalStateException.class, () -> { - ThisObjectValue.withType(TypeNode.BOOLEAN); + ThisObjectValue.withType(TypeNode.NULL); }); } } diff --git a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java index 7799feff78..d3f9fb4412 100644 --- a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java @@ -1656,7 +1656,7 @@ public void writeClassDefinition_statementsAndMethods() { } @Test - public void writeMethodReturnThisObjectValue() { + public void writeThisObjectValue_methodReturn() { VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build(); TypeNode classType = TypeNode.withReference(ref); @@ -1675,8 +1675,7 @@ public void writeMethodReturnThisObjectValue() { } @Test - public void writeExprStatementForThisObjectValue() { - // Example of ThisObjectValue access its fields + public void writeThisObjectValue_accessFieldAndInvokeMethod() { VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build(); TypeNode classType = TypeNode.withReference(ref); @@ -1690,7 +1689,6 @@ public void writeExprStatementForThisObjectValue() { VariableExpr thisVariableExpr = VariableExpr.builder().setVariable(subVariable).setExprReferenceExpr(thisValueExpr).build(); - // Example of ThisObjectValue invoke its method with arguments MethodInvocationExpr methodExpr = MethodInvocationExpr.builder() .setMethodName("getName") From f2e5983ec1a11c39ddadecd05c031f0300bb29bd Mon Sep 17 00:00:00 2001 From: summerji Date: Thu, 30 Jul 2020 20:32:27 -0700 Subject: [PATCH 14/16] correct the test name and add overwrite --- .../com/google/api/generator/engine/ast/ThisObjectValue.java | 1 + .../google/api/generator/engine/ast/ThisObjectValueTest.java | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java index 570b0f0dc5..40e96eb010 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java +++ b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java @@ -21,6 +21,7 @@ public abstract class ThisObjectValue implements ObjectValue { private static final String THIS_VALUE = "this"; + @Override public abstract TypeNode type(); @Override diff --git a/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java b/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java index dd6e5abfa8..83e7c75ab8 100644 --- a/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/ThisObjectValueTest.java @@ -32,7 +32,7 @@ public void validThisObjectValue_basic() { } @Test - public void invalidThisObjectValue_NonReferenceType() { + public void invalidThisObjectValue_nonReferenceType() { assertThrows( IllegalStateException.class, () -> { @@ -41,7 +41,7 @@ public void invalidThisObjectValue_NonReferenceType() { } @Test - public void invalidThisObjectValue_NullType() { + public void invalidThisObjectValue_nullType() { assertThrows( IllegalStateException.class, () -> { From b7592556e1912aff2054e9e2b053df94e06c62e1 Mon Sep 17 00:00:00 2001 From: summerji Date: Fri, 31 Jul 2020 17:13:48 -0700 Subject: [PATCH 15/16] clear error msg --- .../com/google/api/generator/engine/ast/ThisObjectValue.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java index 40e96eb010..96724a4a5f 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java +++ b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java @@ -46,7 +46,7 @@ abstract static class Builder { private ThisObjectValue build() { ThisObjectValue thisObjectValue = autoBuild(); Preconditions.checkState( - TypeNode.isReferenceType(thisObjectValue.type()), "this can only refer to object types"); + TypeNode.isReferenceType(thisObjectValue.type()), "The \"this\" object can only refer to object types"); return thisObjectValue; } } From 340553cf44bb5aca535670a3ee2b1c2c0e4e32a5 Mon Sep 17 00:00:00 2001 From: summerji Date: Fri, 31 Jul 2020 17:16:17 -0700 Subject: [PATCH 16/16] formatting --- .../com/google/api/generator/engine/ast/ThisObjectValue.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java index 96724a4a5f..e72f291936 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java +++ b/src/main/java/com/google/api/generator/engine/ast/ThisObjectValue.java @@ -46,7 +46,8 @@ abstract static class Builder { private ThisObjectValue build() { ThisObjectValue thisObjectValue = autoBuild(); Preconditions.checkState( - TypeNode.isReferenceType(thisObjectValue.type()), "The \"this\" object can only refer to object types"); + TypeNode.isReferenceType(thisObjectValue.type()), + "The \"this\" object can only refer to object types"); return thisObjectValue; } }