-
Notifications
You must be signed in to change notification settings - Fork 288
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
Move Java reflect specific extensions into a separate package #299
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright (C) 2017 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 | ||
* | ||
* 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. | ||
*/ | ||
@file:JvmName("ClassNamesJvm") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally that should remain as "ClassNames", but there's the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I doubt it'll work across multiple modules, but I can give it a try There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There won't be multiple modules. You'll have the common module with common code and then the jvm module will implement the common module and add JVM-specific things. There will only be one class file in the final artifact. And then for JS there will be a JS module, etc., and they're free to have platform-specific things as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I see. Didn't think about it from the perspective of cross-platform modules, rather that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah the nice thing about using multiplatform rather than multiple modules is that you pick one artifact based on the platform on which your code generator runs. And if your code generator is multiplatform, then you only get the common stuff. If we can do multifile classes for now let's do that. If we end up doing two artifacts it's an easy change to make. |
||
|
||
package com.squareup.kotlinpoet.jvm | ||
|
||
import com.squareup.kotlinpoet.ClassName | ||
import com.squareup.kotlinpoet.isOneOf | ||
import javax.lang.model.element.Element | ||
import javax.lang.model.element.ElementKind | ||
import javax.lang.model.element.NestingKind | ||
import javax.lang.model.element.PackageElement | ||
import javax.lang.model.element.TypeElement | ||
|
||
@JvmName("get") | ||
fun Class<*>.asClassName(): ClassName { | ||
require(!isPrimitive) { "primitive types cannot be represented as a ClassName" } | ||
require(Void.TYPE != this) { "'void' type cannot be represented as a ClassName" } | ||
require(!isArray) { "array types cannot be represented as a ClassName" } | ||
val names = mutableListOf<String>() | ||
var c = this | ||
while (true) { | ||
names += c.simpleName | ||
val enclosing = c.enclosingClass ?: break | ||
c = enclosing | ||
} | ||
// Avoid unreliable Class.getPackage(). https://github.com/square/javapoet/issues/295 | ||
val lastDot = c.name.lastIndexOf('.') | ||
if (lastDot != -1) names += c.name.substring(0, lastDot) | ||
names.reverse() | ||
return ClassName(names) | ||
} | ||
|
||
/** Returns the class name for `element`. */ | ||
@JvmName("get") | ||
fun TypeElement.asClassName(): ClassName { | ||
fun isClassOrInterface(e: Element) = e.kind.isClass || e.kind.isInterface | ||
|
||
fun getPackage(type: Element): PackageElement { | ||
var t = type | ||
while (t.kind != ElementKind.PACKAGE) { | ||
t = t.enclosingElement | ||
} | ||
return t as PackageElement | ||
} | ||
|
||
val names = mutableListOf<String>() | ||
var e: Element = this | ||
while (isClassOrInterface(e)) { | ||
val eType = e as TypeElement | ||
require(eType.nestingKind.isOneOf(NestingKind.TOP_LEVEL, NestingKind.MEMBER)) { | ||
"unexpected type testing" | ||
} | ||
names += eType.simpleName.toString() | ||
e = eType.enclosingElement | ||
} | ||
names += getPackage(this).qualifiedName.toString() | ||
names.reverse() | ||
return ClassName(names) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (C) 2017 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 | ||
* | ||
* 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. | ||
*/ | ||
@file:JvmName("ParameterizedTypeNames") | ||
|
||
package com.squareup.kotlinpoet.jvm | ||
|
||
import com.squareup.kotlinpoet.ParameterizedTypeName | ||
import java.lang.reflect.ParameterizedType | ||
|
||
/** Returns a parameterized type equivalent to `type`. */ | ||
@JvmName("get") | ||
fun ParameterizedType.asParameterizedTypeName() = ParameterizedTypeName.get(this, mutableMapOf()) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (C) 2017 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 | ||
* | ||
* 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. | ||
*/ | ||
@file:JvmName("TypeNamesJvm") | ||
|
||
package com.squareup.kotlinpoet.jvm | ||
|
||
import com.squareup.kotlinpoet.TypeName | ||
import java.lang.reflect.Type | ||
import javax.lang.model.type.TypeMirror | ||
|
||
/** Returns a [TypeName] equivalent to this [TypeMirror]. */ | ||
@JvmName("get") | ||
fun TypeMirror.asTypeName() = TypeName.get(this, mutableMapOf()) | ||
|
||
/** Returns a [TypeName] equivalent to this [Type]. */ | ||
@JvmName("get") | ||
fun Type.asTypeName() = TypeName.get(this, mutableMapOf()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These imports will disappear when the factory methods that use them are moved into "jvm"