This repository was archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 685
Improve Kotlin Binding Support #685
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
99bb73d
Not all the members are available
mattleibow f2b14b4
Add support for ignoring members and classes
mattleibow f884c37
Something goes wrong
mattleibow af85955
Merge branch 'master' into dev/improve-kotlin-binding
mattleibow 309bf3a
Merge branch 'master' into dev/improve-kotlin-binding
mattleibow 8f0dc5c
Merge branch 'master' into dev/improve-kotlin-binding
mattleibow 905fae6
Some more improvements
mattleibow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,9 @@ class ProcessXmlCommand : CliktCommand(name = "KotlinBindingSupport") { | |
val outputFile: File? by option("-o", "--output", help = "path to the output transform file") | ||
.file(fileOkay = true, folderOkay = false) | ||
|
||
val ignoreFile: File? by option("-i", "--ignore", help = "path to the ignore file") | ||
.file(fileOkay = true, folderOkay = false) | ||
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 don't actually thing this is needed anymore since it is working properly with a lower Kotlin version, but just in case. |
||
|
||
val companions: Processor.CompanionProcessing by option("--companions", help = "indicate how to handle companions") | ||
.choice( | ||
"default" to Processor.CompanionProcessing.Default, | ||
|
@@ -32,7 +35,7 @@ class ProcessXmlCommand : CliktCommand(name = "KotlinBindingSupport") { | |
.flag(default = false) | ||
|
||
override fun run() { | ||
val proc = Processor(xmlFile, jarFiles, outputFile) | ||
val proc = Processor(xmlFile, jarFiles, outputFile, ignoreFile) | ||
proc.verbose = verbose | ||
proc.companions = companions | ||
proc.process() | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,14 +10,13 @@ import java.lang.reflect.Constructor | |
import java.lang.reflect.Executable | ||
import java.lang.reflect.Field | ||
import java.lang.reflect.Method | ||
import java.net.URL | ||
import java.net.URLClassLoader | ||
import kotlin.reflect.KFunction | ||
import kotlin.reflect.KVisibility | ||
import kotlin.reflect.jvm.kotlinFunction | ||
import kotlin.reflect.jvm.kotlinProperty | ||
|
||
class Processor(xmlFile: File, jarFiles: List<File>, outputFile: File?) { | ||
class Processor(xmlFile: File, jarFiles: List<File>, outputFile: File?, ignoreFile: File?) { | ||
|
||
private fun expressionClasses() = | ||
"/api/package/*[local-name()='class' or local-name()='interface']" | ||
|
@@ -48,6 +47,8 @@ class Processor(xmlFile: File, jarFiles: List<File>, outputFile: File?) { | |
private val outputFile: File? | ||
private val xtransformsdoc: Document | ||
|
||
private val ignored: List<String> | ||
|
||
init { | ||
val urls = jarFiles.map { file -> file.toURI().toURL() } | ||
loader = URLClassLoader(urls.toTypedArray()) | ||
|
@@ -57,6 +58,12 @@ class Processor(xmlFile: File, jarFiles: List<File>, outputFile: File?) { | |
|
||
xtransformsdoc = Document(Element("metadata")) | ||
this.outputFile = outputFile | ||
|
||
if (ignoreFile != null) { | ||
ignored = ignoreFile.readLines() | ||
} else { | ||
ignored = emptyList() | ||
} | ||
} | ||
|
||
fun process() { | ||
|
@@ -196,13 +203,13 @@ class Processor(xmlFile: File, jarFiles: List<File>, outputFile: File?) { | |
|
||
// remove the members that are not meant to be used | ||
processMembers(xclass, "constructor") { | ||
shouldRemoveMember(it, jclass.declaredConstructors) | ||
shouldRemoveMember(it, jclass.declaredConstructors + jclass.constructors) | ||
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. This is still a required change because several members are not found for some reason. It works now, so... |
||
} | ||
processMembers(xclass, "method") { | ||
shouldRemoveMember(it, jclass.declaredMethods) | ||
shouldRemoveMember(it, jclass.declaredMethods + jclass.methods) | ||
} | ||
processMembers(xclass, "field") { | ||
shouldRemoveField(it, jclass.declaredFields) | ||
shouldRemoveField(it, jclass.declaredFields + jclass.fields) | ||
} | ||
} | ||
|
||
|
@@ -253,6 +260,12 @@ class Processor(xmlFile: File, jarFiles: List<File>, outputFile: File?) { | |
|
||
logVerbose("Checking the class \"${xpackage}.${xname}\"...") | ||
|
||
// make sure we haven't been told to ignore this | ||
if (ignored.contains("${xpackage}.${xname}")) { | ||
logVerbose("Ignoring class \"${xpackage}.${xname}\" because it was found in the ignore file...") | ||
return ProcessResult.Ignore | ||
} | ||
|
||
// before we check anything, make sure that the parent class is visible | ||
var lastPeriod = xname.lastIndexOf(".") | ||
while (lastPeriod != -1) { | ||
|
@@ -319,6 +332,12 @@ class Processor(xmlFile: File, jarFiles: List<File>, outputFile: File?) { | |
val xtype = xmember.localName | ||
val xfullname = "${xpackage}.${xclassname}.${xname}" | ||
|
||
// make sure we haven't been told to ignore this | ||
if (ignored.contains(xfullname)) { | ||
logVerbose("Ignoring member \"${xfullname}\" because it was found in the ignore file...") | ||
return ProcessResult.Ignore | ||
} | ||
|
||
// before doing any complex checks, make sure it is not an invalid member | ||
if (invalidKeywords.any { check -> check(xname) }) | ||
return ProcessResult.RemoveGenerated | ||
|
@@ -410,6 +429,12 @@ class Processor(xmlFile: File, jarFiles: List<File>, outputFile: File?) { | |
val xtype = xmember.getAttributeValue("type") | ||
val xfullname = "${xpackage}.${xclassname}.${xname}" | ||
|
||
// make sure we haven't been told to ignore this | ||
if (ignored.contains(xfullname)) { | ||
logVerbose("Ignoring field \"${xfullname}\" because it was found in the ignore file...") | ||
return ProcessResult.Ignore | ||
} | ||
|
||
// before doing any complex checks, make sure it is not an invalid member | ||
if (invalidKeywords.any { check -> check(xname) }) { | ||
return ProcessResult.RemoveGenerated | ||
|
@@ -767,14 +792,14 @@ private val Element.parentElement: Element | |
|
||
private val KmPackage.functionOverloads: List<Pair<KmFunction, List<KmValueParameter>>> | ||
get() = this.functions.map { | ||
it to it.valueParameters.getOverloads() | ||
it to it.valueParameters.getOverloads(it.receiverParameterType) | ||
}.flatMap { overload -> | ||
overload.second.map { params -> overload.first to params } | ||
} | ||
|
||
private val KmClass.functionOverloads: List<Pair<KmFunction, List<KmValueParameter>>> | ||
get() = this.functions.map { | ||
it to it.valueParameters.getOverloads() | ||
it to it.valueParameters.getOverloads(it.receiverParameterType) | ||
}.flatMap { overload -> | ||
overload.second.map { params -> overload.first to params } | ||
} | ||
|
@@ -786,9 +811,15 @@ private val KmClass.constructorOverloads: List<Pair<KmConstructor, List<KmValueP | |
overload.second.map { params -> overload.first to params } | ||
} | ||
|
||
private fun List<KmValueParameter>.getOverloads(): List<List<KmValueParameter>> { | ||
private fun List<KmValueParameter>.getOverloads(receiverParameterType: KmType? = null): List<List<KmValueParameter>> { | ||
val overloads = mutableListOf<List<KmValueParameter>>() | ||
overloads.add(this) | ||
if (receiverParameterType == null) { | ||
overloads.add(this) | ||
} else { | ||
val t = KmValueParameter(0, "this") | ||
t.type = receiverParameterType | ||
overloads.add(listOf(t) + this) | ||
} | ||
while (Flag.ValueParameter.DECLARES_DEFAULT_VALUE(overloads.last().lastOrNull()?.flags ?: 0)) { | ||
val l = overloads.last() | ||
overloads.add(l.take(l.size - 1)) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Just a note about the version numbers: As I update the version to the later ones, it struggles to find various members. So, I just dropped it all the way down. Not sure what is happening here, but it works now...