[Git][java-team/javapoet][upstream] New upstream version 1.13.0
Tony Mancill
gitlab at salsa.debian.org
Mon Jul 6 18:04:54 BST 2020
Tony Mancill pushed to branch upstream at Debian Java Maintainers / javapoet
Commits:
45d933e8 by tony mancill at 2020-07-06T08:36:41-07:00
New upstream version 1.13.0
- - - - -
7 changed files:
- CHANGELOG.md
- README.md
- pom.xml
- src/main/java/com/squareup/javapoet/MethodSpec.java
- src/main/java/com/squareup/javapoet/ParameterSpec.java
- src/test/java/com/squareup/javapoet/MethodSpecTest.java
- src/test/java/com/squareup/javapoet/ParameterSpecTest.java
Changes:
=====================================
CHANGELOG.md
=====================================
@@ -1,6 +1,19 @@
Change Log
==========
+JavaPoet 1.13.0 *(2020-06-18)*
+-----------------------------
+
+ * New: Add support for explicit receiver parameters.
+ * Fix: Don't copy parameter annotations when creating a `ParameterSpec`.
+
+
+JavaPoet 1.12.1 *(2020-01-20)*
+-----------------------------
+
+ * Fix: Ignore parameter annotations in `MethodSpec.overriding()`.
+
+
JavaPoet 1.12.0 *(2020-01-09)*
-----------------------------
=====================================
README.md
=====================================
@@ -892,12 +892,12 @@ Download [the latest .jar][dl] or depend via Maven:
<dependency>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
- <version>1.12.0</version>
+ <version>1.12.1</version>
</dependency>
```
or Gradle:
```groovy
-compile 'com.squareup:javapoet:1.12.0'
+compile 'com.squareup:javapoet:1.12.1'
```
Snapshots of the development version are available in [Sonatype's `snapshots` repository][snap].
=====================================
pom.xml
=====================================
@@ -11,7 +11,7 @@
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
- <version>1.12.1</version>
+ <version>1.13.0</version>
<name>JavaPoet</name>
<description>Use beautiful Java code to generate beautiful Java code.</description>
=====================================
src/main/java/com/squareup/javapoet/MethodSpec.java
=====================================
@@ -24,7 +24,6 @@ import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
-import java.util.stream.Collectors;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
@@ -233,16 +232,7 @@ public final class MethodSpec {
}
methodBuilder.returns(TypeName.get(method.getReturnType()));
- // Copying parameter annotations from the overridden method can be incorrect so we're
- // deliberately dropping them. See https://github.com/square/javapoet/issues/482.
- methodBuilder.addParameters(ParameterSpec.parametersOf(method)
- .stream()
- .map(parameterSpec -> {
- ParameterSpec.Builder builder = parameterSpec.toBuilder();
- builder.annotations.clear();
- return builder.build();
- })
- .collect(Collectors.toList()));
+ methodBuilder.addParameters(ParameterSpec.parametersOf(method));
methodBuilder.varargs(method.isVarArgs());
for (TypeMirror thrownType : method.getThrownTypes()) {
=====================================
src/main/java/com/squareup/javapoet/ParameterSpec.java
=====================================
@@ -21,7 +21,6 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
-import java.util.stream.Collectors;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
@@ -87,17 +86,12 @@ public final class ParameterSpec {
public static ParameterSpec get(VariableElement element) {
checkArgument(element.getKind().equals(ElementKind.PARAMETER), "element is not a parameter");
- // Copy over any annotations from element.
- List<AnnotationSpec> annotations = element.getAnnotationMirrors()
- .stream()
- .map((mirror) -> AnnotationSpec.get(mirror))
- .collect(Collectors.toList());
-
TypeName type = TypeName.get(element.asType());
String name = element.getSimpleName().toString();
+ // Copying parameter annotations can be incorrect so we're deliberately not including them.
+ // See https://github.com/square/javapoet/issues/482.
return ParameterSpec.builder(type, name)
.addModifiers(element.getModifiers())
- .addAnnotations(annotations)
.build();
}
@@ -109,9 +103,18 @@ public final class ParameterSpec {
return result;
}
+ private static boolean isValidParameterName(String name) {
+ // Allow "this" for explicit receiver parameters
+ // See https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.1.
+ if (name.endsWith(".this")) {
+ return SourceVersion.isIdentifier(name.substring(0, name.length() - ".this".length()));
+ }
+ return name.equals("this") || SourceVersion.isName(name);
+ }
+
public static Builder builder(TypeName type, String name, Modifier... modifiers) {
checkNotNull(type, "type == null");
- checkArgument(SourceVersion.isName(name), "not a valid name: %s", name);
+ checkArgument(isValidParameterName(name), "not a valid name: %s", name);
return new Builder(type, name)
.addModifiers(modifiers);
}
=====================================
src/test/java/com/squareup/javapoet/MethodSpecTest.java
=====================================
@@ -15,6 +15,7 @@
*/
package com.squareup.javapoet;
+import com.google.testing.compile.Compilation;
import com.google.testing.compile.CompilationRule;
import java.io.Closeable;
import java.io.IOException;
@@ -30,15 +31,19 @@ import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
+import javax.lang.model.util.ElementFilter;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
+import javax.tools.JavaFileObject;
+
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.truth.Truth.assertThat;
-import static com.squareup.javapoet.MethodSpec.CONSTRUCTOR;
+import static com.google.testing.compile.CompilationSubject.assertThat;
+import static com.google.testing.compile.Compiler.javac;
import static com.squareup.javapoet.TestUtil.findFirst;
import static javax.lang.model.util.ElementFilter.methodsIn;
import static org.junit.Assert.fail;
@@ -247,6 +252,26 @@ public final class MethodSpecTest {
}
}
+ abstract static class AbstractClassWithPrivateAnnotation {
+
+ private @interface PrivateAnnotation{ }
+
+ abstract void foo(@PrivateAnnotation final String bar);
+ }
+
+ @Test public void overrideDoesNotCopyParameterAnnotations() {
+ TypeElement abstractTypeElement = getElement(AbstractClassWithPrivateAnnotation.class);
+ ExecutableElement fooElement = ElementFilter.methodsIn(abstractTypeElement.getEnclosedElements()).get(0);
+ ClassName implClassName = ClassName.get("com.squareup.javapoet", "Impl");
+ TypeSpec type = TypeSpec.classBuilder(implClassName)
+ .superclass(abstractTypeElement.asType())
+ .addMethod(MethodSpec.overriding(fooElement).build())
+ .build();
+ JavaFileObject jfo = JavaFile.builder(implClassName.packageName, type).build().toJavaFileObject();
+ Compilation compilation = javac().compile(jfo);
+ assertThat(compilation).succeeded();
+ }
+
@Test public void equalsAndHashCode() {
MethodSpec a = MethodSpec.constructorBuilder().build();
MethodSpec b = MethodSpec.constructorBuilder().build();
=====================================
src/test/java/com/squareup/javapoet/ParameterSpecTest.java
=====================================
@@ -60,6 +60,25 @@ public class ParameterSpecTest {
assertThat(a.toString()).isEqualTo(b.toString());
}
+ @Test public void receiverParameterInstanceMethod() {
+ ParameterSpec.Builder builder = ParameterSpec.builder(int.class, "this");
+ assertThat(builder.build().name).isEqualTo("this");
+ }
+
+ @Test public void receiverParameterNestedClass() {
+ ParameterSpec.Builder builder = ParameterSpec.builder(int.class, "Foo.this");
+ assertThat(builder.build().name).isEqualTo("Foo.this");
+ }
+
+ @Test public void keywordName() {
+ try {
+ ParameterSpec.builder(int.class, "super");
+ fail();
+ } catch (Exception e) {
+ assertThat(e.getMessage()).isEqualTo("not a valid name: super");
+ }
+ }
+
@Test public void nullAnnotationsAddition() {
try {
ParameterSpec.builder(int.class, "foo").addAnnotations(null);
@@ -99,7 +118,7 @@ public class ParameterSpecTest {
VariableElement parameterElement = element.getParameters().get(0);
assertThat(ParameterSpec.get(parameterElement).toString())
- .isEqualTo("@javax.annotation.Nullable java.lang.String arg0");
+ .isEqualTo("java.lang.String arg0");
}
@Test public void addNonFinalModifier() {
View it on GitLab: https://salsa.debian.org/java-team/javapoet/-/commit/45d933e8a4c74d29235bd5d58db51cb10028d68e
--
View it on GitLab: https://salsa.debian.org/java-team/javapoet/-/commit/45d933e8a4c74d29235bd5d58db51cb10028d68e
You're receiving this email because of your account on salsa.debian.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/pkg-java-commits/attachments/20200706/13a8a2cd/attachment.html>
More information about the pkg-java-commits
mailing list