@@ -22,18 +22,18 @@ import java.lang.UnsatisfiedLinkError
22
22
import java.lang.reflect.Constructor
23
23
import java.lang.reflect.Modifier
24
24
import java.net.URLClassLoader
25
- import java.nio.file.FileVisitResult
26
- import java.nio.file.Files
27
- import java.nio.file.Path
28
- import java.nio.file.SimpleFileVisitor
29
- import java.nio.file.attribute.BasicFileAttributes
30
25
import java.util.*
31
26
import java.util.function.Function
32
27
import kotlin.reflect.KFunction
33
28
import kotlin.reflect.full.companionObjectInstance
34
29
import kotlin.reflect.full.memberFunctions
35
30
36
- class PlugGenerator internal constructor(toSearches : List <File >, toLinkAgainst : Set <File >) {
31
+ class PlugGenerator
32
+ internal constructor (
33
+ plugToSocket: Map <String , String >,
34
+ toSearches: List <File >,
35
+ toLinkAgainst: Set <File >
36
+ ) {
37
37
@JvmField val atplugInf: SortedMap <String , String > = TreeMap ()
38
38
39
39
/* * A cache from a plugin interface to a function that converts a class into its metadata. */
@@ -56,22 +56,7 @@ class PlugGenerator internal constructor(toSearches: List<File>, toLinkAgainst:
56
56
}!!
57
57
as KFunction <Function <Any , String >>
58
58
try {
59
- val parser = PlugParser ()
60
- // walk toSearch, passing each classfile to load()
61
- for (toSearch in toSearches) {
62
- if (toSearch.isDirectory) {
63
- Files .walkFileTree(
64
- toSearch.toPath(),
65
- object : SimpleFileVisitor <Path >() {
66
- override fun visitFile (file : Path , attrs : BasicFileAttributes ): FileVisitResult {
67
- if (file.toString().endsWith(EXT_CLASS )) {
68
- maybeGeneratePlugin(parser, file)
69
- }
70
- return FileVisitResult .CONTINUE
71
- }
72
- })
73
- }
74
- }
59
+ plugToSocket.forEach { (plug, socket) -> atplugInf[plug] = generatePlugin(plug, socket) }
75
60
} finally {
76
61
classLoader.close()
77
62
System .setProperty(" atplug.generate" , " " )
@@ -82,18 +67,13 @@ class PlugGenerator internal constructor(toSearches: List<File>, toLinkAgainst:
82
67
* Loads a class by its FQN. If it's concrete and implements a plugin, then we'll call
83
68
* generatePlugin.
84
69
*/
85
- private fun maybeGeneratePlugin (parser : PlugParser , path : Path ) {
86
- parser.parse(path.toFile())
87
- if (! parser.hasPlug()) {
88
- return
89
- }
90
- val plugClass = classLoader.loadClass(parser.plugClassName)
91
- val socketClass = classLoader.loadClass(parser.socketClassName)
70
+ private fun generatePlugin (plugClassName : String , socketClassName : String ): String {
71
+ val plugClass = classLoader.loadClass(plugClassName)
72
+ val socketClass = classLoader.loadClass(socketClassName)
92
73
require(! Modifier .isAbstract(plugClass.modifiers)) {
93
74
" Class $plugClass has @Plug($socketClass ) but it is abstract."
94
75
}
95
- val atplugInfContent = generatePlugin<Any , Any >(plugClass, socketClass)
96
- atplugInf[plugClass.name] = atplugInfContent
76
+ return generatePlugin<Any , Any >(plugClass, socketClass)
97
77
}
98
78
99
79
private fun <SocketT , PlugT : SocketT > generatePlugin (
@@ -128,14 +108,18 @@ class PlugGenerator internal constructor(toSearches: List<File>, toLinkAgainst:
128
108
* Returns a Map from a plugin's name to its ATPLUG-INF content.
129
109
*
130
110
* @param toSearch a directory containing class files where we will look for plugin
131
- * implementations
111
+ * implementations
132
112
* @param toLinkAgainst the classes that these plugins implementations need
133
113
* @return a map from component name to is ATPLUG-INF string content
134
114
*/
135
- fun generate (toSearch : List <File >, toLinkAgainst : Set <File >): SortedMap <String , String > {
115
+ fun generate (
116
+ plugToSocket : Map <String , String >,
117
+ toSearch : List <File >,
118
+ toLinkAgainst : Set <File >
119
+ ): SortedMap <String , String > {
136
120
return try {
137
- val ext = PlugGeneratorJavaExecable (toSearch, toLinkAgainst)
138
- val metadataGen = PlugGenerator (ext.toSearch, ext.toLinkAgainst)
121
+ val ext = PlugGeneratorJavaExecable (plugToSocket, toSearch, toLinkAgainst)
122
+ val metadataGen = PlugGenerator (plugToSocket, ext.toSearch, ext.toLinkAgainst)
139
123
// save our results, with no reference to the guts of what happened inside PluginMetadataGen
140
124
metadataGen.atplugInf
141
125
} catch (e: Exception ) {
0 commit comments