Skip to content

Commit

Permalink
Track new JavaPoet group and package name
Browse files Browse the repository at this point in the history
Rename the existing interop module to javapoet-deprecated and
introduce a new module called javapoet that integrates with
the Palantir JavaPoet fork.
  • Loading branch information
squarejesse committed Oct 15, 2024
1 parent e4e7a6f commit 1d5b85f
Show file tree
Hide file tree
Showing 22 changed files with 713 additions and 21 deletions.
4 changes: 2 additions & 2 deletions docs/interop-javapoet.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
JavaPoet Extensions for KotlinPoet
==================================

`interop:javapoet` is an interop API for converting [JavaPoet](https://github.com/square/javapoet)
`interop:javapoet` is an interop API for converting [JavaPoet](https://github.com/palantir/javapoet)
types to KotlinPoet types. This is particularly useful for projects that support code gen in
multiple languages and want to easily be able to jump between.

Expand All @@ -15,7 +15,7 @@ in to the `@KotlinPoetJavaPoetPreview` annotation.
```kotlin
// Points to com.squareup.kotlinpoet.TypeName
KTypeName
// Points to com.squareup.javapoet.TypeName
// Points to com.palantir.javapoet.TypeName
JTypeName
```

Expand Down
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ kotlinBinaryCompatibilityValidator = { id = "org.jetbrains.kotlinx.binary-compat
[libraries]
autoCommon = { module = "com.google.auto:auto-common", version = "1.2.2" }
guava = { module = "com.google.guava:guava", version = "33.3.1-jre" }
javapoet = "com.squareup:javapoet:1.13.0"
javapoet-deprecated = "com.squareup:javapoet:1.13.0"
javapoet = "com.palantir.javapoet:javapoet:0.5.0"

autoService = "com.google.auto.service:auto-service-annotations:1.1.1"
autoService-ksp = "dev.zacsweers.autoservice:auto-service-ksp:1.2.0"
Expand Down
22 changes: 22 additions & 0 deletions interop/javapoet-deprecated/api/javapoet-deprecated.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public final class com/squareup/kotlinpoet/javapoet/J2kInteropKt {
public static final fun toKClassName (Lcom/squareup/javapoet/ClassName;)Lcom/squareup/kotlinpoet/ClassName;
public static final fun toKParameterizedTypeName (Lcom/squareup/javapoet/ParameterizedTypeName;)Lcom/squareup/kotlinpoet/ParameterizedTypeName;
public static final fun toKTypeName (Lcom/squareup/javapoet/TypeName;)Lcom/squareup/kotlinpoet/TypeName;
public static final fun toKTypeVariableName (Lcom/squareup/javapoet/TypeVariableName;)Lcom/squareup/kotlinpoet/TypeVariableName;
public static final fun toKWildcardTypeName (Lcom/squareup/javapoet/WildcardTypeName;)Lcom/squareup/kotlinpoet/WildcardTypeName;
}

public final class com/squareup/kotlinpoet/javapoet/K2jInteropKt {
public static final fun toJClassName (Lcom/squareup/kotlinpoet/ClassName;Z)Lcom/squareup/javapoet/TypeName;
public static synthetic fun toJClassName$default (Lcom/squareup/kotlinpoet/ClassName;ZILjava/lang/Object;)Lcom/squareup/javapoet/TypeName;
public static final fun toJParameterizedOrArrayTypeName (Lcom/squareup/kotlinpoet/ParameterizedTypeName;)Lcom/squareup/javapoet/TypeName;
public static final fun toJParameterizedTypeName (Lcom/squareup/kotlinpoet/ParameterizedTypeName;)Lcom/squareup/javapoet/ParameterizedTypeName;
public static final fun toJTypeName (Lcom/squareup/kotlinpoet/TypeName;Z)Lcom/squareup/javapoet/TypeName;
public static synthetic fun toJTypeName$default (Lcom/squareup/kotlinpoet/TypeName;ZILjava/lang/Object;)Lcom/squareup/javapoet/TypeName;
public static final fun toJTypeVariableName (Lcom/squareup/kotlinpoet/TypeVariableName;)Lcom/squareup/javapoet/TypeVariableName;
public static final fun toJWildcardTypeName (Lcom/squareup/kotlinpoet/WildcardTypeName;)Lcom/squareup/javapoet/WildcardTypeName;
}

public abstract interface annotation class com/squareup/kotlinpoet/javapoet/KotlinPoetJavaPoetPreview : java/lang/annotation/Annotation {
}

22 changes: 22 additions & 0 deletions interop/javapoet-deprecated/api/javapoet.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public final class com/squareup/kotlinpoet/javapoet/J2kInteropKt {
public static final fun toKClassName (Lcom/squareup/javapoet/ClassName;)Lcom/squareup/kotlinpoet/ClassName;
public static final fun toKParameterizedTypeName (Lcom/squareup/javapoet/ParameterizedTypeName;)Lcom/squareup/kotlinpoet/ParameterizedTypeName;
public static final fun toKTypeName (Lcom/squareup/javapoet/TypeName;)Lcom/squareup/kotlinpoet/TypeName;
public static final fun toKTypeVariableName (Lcom/squareup/javapoet/TypeVariableName;)Lcom/squareup/kotlinpoet/TypeVariableName;
public static final fun toKWildcardTypeName (Lcom/squareup/javapoet/WildcardTypeName;)Lcom/squareup/kotlinpoet/WildcardTypeName;
}

public final class com/squareup/kotlinpoet/javapoet/K2jInteropKt {
public static final fun toJClassName (Lcom/squareup/kotlinpoet/ClassName;Z)Lcom/squareup/javapoet/TypeName;
public static synthetic fun toJClassName$default (Lcom/squareup/kotlinpoet/ClassName;ZILjava/lang/Object;)Lcom/squareup/javapoet/TypeName;
public static final fun toJParameterizedOrArrayTypeName (Lcom/squareup/kotlinpoet/ParameterizedTypeName;)Lcom/squareup/javapoet/TypeName;
public static final fun toJParameterizedTypeName (Lcom/squareup/kotlinpoet/ParameterizedTypeName;)Lcom/squareup/javapoet/ParameterizedTypeName;
public static final fun toJTypeName (Lcom/squareup/kotlinpoet/TypeName;Z)Lcom/squareup/javapoet/TypeName;
public static synthetic fun toJTypeName$default (Lcom/squareup/kotlinpoet/TypeName;ZILjava/lang/Object;)Lcom/squareup/javapoet/TypeName;
public static final fun toJTypeVariableName (Lcom/squareup/kotlinpoet/TypeVariableName;)Lcom/squareup/javapoet/TypeVariableName;
public static final fun toJWildcardTypeName (Lcom/squareup/kotlinpoet/WildcardTypeName;)Lcom/squareup/javapoet/WildcardTypeName;
}

public abstract interface annotation class com/squareup/kotlinpoet/javapoet/KotlinPoetJavaPoetPreview : java/lang/annotation/Annotation {
}

31 changes: 31 additions & 0 deletions interop/javapoet-deprecated/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2021 Square, Inc.
*
* 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
*
* https://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.
*/
plugins {
kotlin("jvm")
}

tasks.jar {
manifest {
attributes("Automatic-Module-Name" to "com.squareup.kotlinpoet.javapoet")
}
}

dependencies {
api(projects.kotlinpoet)
api(libs.javapoet.deprecated)
testImplementation(libs.kotlin.junit)
testImplementation(libs.truth)
}
4 changes: 4 additions & 0 deletions interop/javapoet-deprecated/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POM_ARTIFACT_ID=kotlinpoet-javapoet
POM_NAME=KotlinPoet (JavaPoet Interop)
POM_DESCRIPTION=Extensions for interop with JavaPoet.
POM_PACKAGING=jar
32 changes: 16 additions & 16 deletions interop/javapoet/api/javapoet.api
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
public final class com/squareup/kotlinpoet/javapoet/J2kInteropKt {
public static final fun toKClassName (Lcom/squareup/javapoet/ClassName;)Lcom/squareup/kotlinpoet/ClassName;
public static final fun toKParameterizedTypeName (Lcom/squareup/javapoet/ParameterizedTypeName;)Lcom/squareup/kotlinpoet/ParameterizedTypeName;
public static final fun toKTypeName (Lcom/squareup/javapoet/TypeName;)Lcom/squareup/kotlinpoet/TypeName;
public static final fun toKTypeVariableName (Lcom/squareup/javapoet/TypeVariableName;)Lcom/squareup/kotlinpoet/TypeVariableName;
public static final fun toKWildcardTypeName (Lcom/squareup/javapoet/WildcardTypeName;)Lcom/squareup/kotlinpoet/WildcardTypeName;
public final class com/squareup/kotlinpoet/palantirjavapoet/J2kInteropKt {
public static final fun toKClassName (Lcom/palantir/javapoet/ClassName;)Lcom/squareup/kotlinpoet/ClassName;
public static final fun toKParameterizedTypeName (Lcom/palantir/javapoet/ParameterizedTypeName;)Lcom/squareup/kotlinpoet/ParameterizedTypeName;
public static final fun toKTypeName (Lcom/palantir/javapoet/TypeName;)Lcom/squareup/kotlinpoet/TypeName;
public static final fun toKTypeVariableName (Lcom/palantir/javapoet/TypeVariableName;)Lcom/squareup/kotlinpoet/TypeVariableName;
public static final fun toKWildcardTypeName (Lcom/palantir/javapoet/WildcardTypeName;)Lcom/squareup/kotlinpoet/WildcardTypeName;
}

public final class com/squareup/kotlinpoet/javapoet/K2jInteropKt {
public static final fun toJClassName (Lcom/squareup/kotlinpoet/ClassName;Z)Lcom/squareup/javapoet/TypeName;
public static synthetic fun toJClassName$default (Lcom/squareup/kotlinpoet/ClassName;ZILjava/lang/Object;)Lcom/squareup/javapoet/TypeName;
public static final fun toJParameterizedOrArrayTypeName (Lcom/squareup/kotlinpoet/ParameterizedTypeName;)Lcom/squareup/javapoet/TypeName;
public static final fun toJParameterizedTypeName (Lcom/squareup/kotlinpoet/ParameterizedTypeName;)Lcom/squareup/javapoet/ParameterizedTypeName;
public static final fun toJTypeName (Lcom/squareup/kotlinpoet/TypeName;Z)Lcom/squareup/javapoet/TypeName;
public static synthetic fun toJTypeName$default (Lcom/squareup/kotlinpoet/TypeName;ZILjava/lang/Object;)Lcom/squareup/javapoet/TypeName;
public static final fun toJTypeVariableName (Lcom/squareup/kotlinpoet/TypeVariableName;)Lcom/squareup/javapoet/TypeVariableName;
public static final fun toJWildcardTypeName (Lcom/squareup/kotlinpoet/WildcardTypeName;)Lcom/squareup/javapoet/WildcardTypeName;
public final class com/squareup/kotlinpoet/palantirjavapoet/K2jInteropKt {
public static final fun toJClassName (Lcom/squareup/kotlinpoet/ClassName;Z)Lcom/palantir/javapoet/TypeName;
public static synthetic fun toJClassName$default (Lcom/squareup/kotlinpoet/ClassName;ZILjava/lang/Object;)Lcom/palantir/javapoet/TypeName;
public static final fun toJParameterizedOrArrayTypeName (Lcom/squareup/kotlinpoet/ParameterizedTypeName;)Lcom/palantir/javapoet/TypeName;
public static final fun toJParameterizedTypeName (Lcom/squareup/kotlinpoet/ParameterizedTypeName;)Lcom/palantir/javapoet/ParameterizedTypeName;
public static final fun toJTypeName (Lcom/squareup/kotlinpoet/TypeName;Z)Lcom/palantir/javapoet/TypeName;
public static synthetic fun toJTypeName$default (Lcom/squareup/kotlinpoet/TypeName;ZILjava/lang/Object;)Lcom/palantir/javapoet/TypeName;
public static final fun toJTypeVariableName (Lcom/squareup/kotlinpoet/TypeVariableName;)Lcom/palantir/javapoet/TypeVariableName;
public static final fun toJWildcardTypeName (Lcom/squareup/kotlinpoet/WildcardTypeName;)Lcom/palantir/javapoet/WildcardTypeName;
}

public abstract interface annotation class com/squareup/kotlinpoet/javapoet/KotlinPoetJavaPoetPreview : java/lang/annotation/Annotation {
public abstract interface annotation class com/squareup/kotlinpoet/palantirjavapoet/KotlinPoetJavaPoetPreview : java/lang/annotation/Annotation {
}

2 changes: 1 addition & 1 deletion interop/javapoet/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ plugins {

tasks.jar {
manifest {
attributes("Automatic-Module-Name" to "com.squareup.kotlinpoet.javapoet")
attributes("Automatic-Module-Name" to "com.squareup.kotlinpoet.palantirjavapoet")
}
}

Expand Down
2 changes: 1 addition & 1 deletion interop/javapoet/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
POM_ARTIFACT_ID=kotlinpoet-javapoet
POM_ARTIFACT_ID=kotlinpoet-palantirjavapoet
POM_NAME=KotlinPoet (JavaPoet Interop)
POM_DESCRIPTION=Extensions for interop with JavaPoet.
POM_PACKAGING=jar
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Copyright (C) 2021 Square, Inc.
*
* 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
*
* https://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.squareup.kotlinpoet.palantirjavapoet

import com.palantir.javapoet.ArrayTypeName
import com.squareup.kotlinpoet.ANY
import com.squareup.kotlinpoet.ARRAY
import com.squareup.kotlinpoet.BOOLEAN
import com.squareup.kotlinpoet.BOOLEAN_ARRAY
import com.squareup.kotlinpoet.BYTE
import com.squareup.kotlinpoet.BYTE_ARRAY
import com.squareup.kotlinpoet.CHAR
import com.squareup.kotlinpoet.CHAR_ARRAY
import com.squareup.kotlinpoet.CHAR_SEQUENCE
import com.squareup.kotlinpoet.DOUBLE
import com.squareup.kotlinpoet.DOUBLE_ARRAY
import com.squareup.kotlinpoet.Dynamic
import com.squareup.kotlinpoet.ENUM
import com.squareup.kotlinpoet.FLOAT
import com.squareup.kotlinpoet.FLOAT_ARRAY
import com.squareup.kotlinpoet.INT
import com.squareup.kotlinpoet.INT_ARRAY
import com.squareup.kotlinpoet.LIST
import com.squareup.kotlinpoet.LONG
import com.squareup.kotlinpoet.LONG_ARRAY
import com.squareup.kotlinpoet.LambdaTypeName
import com.squareup.kotlinpoet.MAP
import com.squareup.kotlinpoet.MUTABLE_LIST
import com.squareup.kotlinpoet.MUTABLE_MAP
import com.squareup.kotlinpoet.MUTABLE_SET
import com.squareup.kotlinpoet.SET
import com.squareup.kotlinpoet.SHORT
import com.squareup.kotlinpoet.SHORT_ARRAY
import com.squareup.kotlinpoet.STAR
import com.squareup.kotlinpoet.STRING
import com.squareup.kotlinpoet.U_BYTE
import com.squareup.kotlinpoet.U_BYTE_ARRAY
import com.squareup.kotlinpoet.U_INT
import com.squareup.kotlinpoet.U_INT_ARRAY
import com.squareup.kotlinpoet.U_LONG
import com.squareup.kotlinpoet.U_LONG_ARRAY
import com.squareup.kotlinpoet.U_SHORT
import com.squareup.kotlinpoet.U_SHORT_ARRAY

@KotlinPoetJavaPoetPreview
public fun KClassName.toJClassName(boxIfPrimitive: Boolean = false): JTypeName {
return when (copy(nullable = false)) {
BOOLEAN -> JTypeName.BOOLEAN.boxIfPrimitive(boxIfPrimitive || isNullable)
BYTE, U_BYTE -> JTypeName.BYTE.boxIfPrimitive(boxIfPrimitive || isNullable)
CHAR -> JTypeName.CHAR.boxIfPrimitive(boxIfPrimitive || isNullable)
SHORT, U_SHORT -> JTypeName.SHORT.boxIfPrimitive(boxIfPrimitive || isNullable)
INT, U_INT -> JTypeName.INT.boxIfPrimitive(boxIfPrimitive || isNullable)
LONG, U_LONG -> JTypeName.LONG.boxIfPrimitive(boxIfPrimitive || isNullable)
FLOAT -> JTypeName.FLOAT.boxIfPrimitive(boxIfPrimitive || isNullable)
DOUBLE -> JTypeName.DOUBLE.boxIfPrimitive(boxIfPrimitive || isNullable)
ANY -> JClassName.OBJECT
CHAR_SEQUENCE -> PoetInterop.CN_JAVA_CHAR_SEQUENCE
STRING -> PoetInterop.CN_JAVA_STRING
LIST, MUTABLE_LIST -> PoetInterop.CN_JAVA_LIST
SET, MUTABLE_SET -> PoetInterop.CN_JAVA_SET
MAP, MUTABLE_MAP -> PoetInterop.CN_JAVA_MAP
BOOLEAN_ARRAY -> ArrayTypeName.of(JTypeName.BOOLEAN)
BYTE_ARRAY, U_BYTE_ARRAY -> ArrayTypeName.of(JTypeName.BYTE)
CHAR_ARRAY -> ArrayTypeName.of(JTypeName.CHAR)
SHORT_ARRAY, U_SHORT_ARRAY -> ArrayTypeName.of(JTypeName.SHORT)
INT_ARRAY, U_INT_ARRAY -> ArrayTypeName.of(JTypeName.INT)
LONG_ARRAY, U_LONG_ARRAY -> ArrayTypeName.of(JTypeName.LONG)
FLOAT_ARRAY -> ArrayTypeName.of(JTypeName.FLOAT)
DOUBLE_ARRAY -> ArrayTypeName.of(JTypeName.DOUBLE)
ENUM -> PoetInterop.CN_JAVA_ENUM
else -> {
if (simpleNames.size == 1) {
JClassName.get(packageName, simpleName)
} else {
JClassName.get(packageName, simpleNames.first(), *simpleNames.drop(1).toTypedArray())
}
}
}
}

@KotlinPoetJavaPoetPreview
public fun KParameterizedTypeName.toJParameterizedOrArrayTypeName(): JTypeName {
return when (rawType) {
ARRAY -> {
val componentType = typeArguments.firstOrNull()?.toJTypeName()
?: throw IllegalStateException("Array with no type! $this")
ArrayTypeName.of(componentType)
}
else -> {
JParameterizedTypeName.get(
rawType.toJClassName() as JClassName,
*typeArguments.map { it.toJTypeName(boxIfPrimitive = true) }.toTypedArray(),
)
}
}
}

@KotlinPoetJavaPoetPreview
public fun KParameterizedTypeName.toJParameterizedTypeName(): JParameterizedTypeName {
check(rawType != ARRAY) {
"Array type! JavaPoet arrays are a custom TypeName. Use this function only for things you know are not arrays"
}
return toJParameterizedOrArrayTypeName() as JParameterizedTypeName
}

@KotlinPoetJavaPoetPreview
public fun KTypeVariableName.toJTypeVariableName(): JTypeVariableName {
return JTypeVariableName.get(name, *bounds.map { it.toJTypeName(boxIfPrimitive = true) }.toTypedArray())
}

@KotlinPoetJavaPoetPreview
public fun KWildcardTypeName.toJWildcardTypeName(): JWildcardTypeName {
return if (this == STAR) {
JWildcardTypeName.subtypeOf(JClassName.OBJECT)
} else if (inTypes.size == 1) {
JWildcardTypeName.supertypeOf(inTypes[0].toJTypeName())
} else {
JWildcardTypeName.subtypeOf(outTypes[0].toJTypeName())
}
}

@KotlinPoetJavaPoetPreview
public fun KTypeName.toJTypeName(boxIfPrimitive: Boolean = false): JTypeName {
return when (this) {
is KClassName -> toJClassName(boxIfPrimitive)
Dynamic -> throw IllegalStateException("Not applicable in Java!")
// TODO should we return a ParameterizedTypeName of the KFunction?
is LambdaTypeName -> throw IllegalStateException("Not applicable in Java!")
is KParameterizedTypeName -> toJParameterizedOrArrayTypeName()
is KTypeVariableName -> toJTypeVariableName()
is KWildcardTypeName -> toJWildcardTypeName()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2021 Square, Inc.
*
* 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
*
* https://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.squareup.kotlinpoet.palantirjavapoet

import kotlin.annotation.AnnotationTarget.CLASS
import kotlin.annotation.AnnotationTarget.FUNCTION
import kotlin.annotation.AnnotationTarget.PROPERTY
import kotlin.annotation.AnnotationTarget.TYPEALIAS

/**
* Indicates that a given API is part of the experimental KotlinPoet JavaPoet support and is
* subject to API changes.
*/
@RequiresOptIn
@Retention(AnnotationRetention.BINARY)
@Target(CLASS, FUNCTION, PROPERTY, TYPEALIAS)
public annotation class KotlinPoetJavaPoetPreview
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2021 Square, Inc.
*
* 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
*
* https://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.squareup.kotlinpoet.palantirjavapoet

/** Various JavaPoet and KotlinPoet representations of some common types. */
@OptIn(KotlinPoetJavaPoetPreview::class)
internal object PoetInterop {
internal val CN_JAVA_CHAR_SEQUENCE = JClassName.get("java.lang", "CharSequence")
internal val CN_JAVA_STRING = JClassName.get("java.lang", "String")
internal val CN_JAVA_LIST = JClassName.get("java.util", "List")
internal val CN_JAVA_SET = JClassName.get("java.util", "Set")
internal val CN_JAVA_MAP = JClassName.get("java.util", "Map")
internal val CN_JAVA_ENUM = JClassName.get("java.lang", "Enum")
}
Loading

0 comments on commit 1d5b85f

Please sign in to comment.