Skip to content

Commit b588797

Browse files
committed
#1 Fix example for Kotlin 1.1.0
1 parent 4cf8099 commit b588797

File tree

3 files changed

+64
-40
lines changed

3 files changed

+64
-40
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.idea/
12
target/
23
pom.xml.tag
34
pom.xml.releaseBackup

pom.xml

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<version>1.0-SNAPSHOT</version>
1111

1212
<properties>
13-
<kotlin.version>1.0.0-beta-1038</kotlin.version>
13+
<kotlin.version>1.1.0</kotlin.version>
1414
<junit.version>4.10</junit.version>
1515

1616
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -24,7 +24,7 @@
2424
</dependency>
2525
<dependency>
2626
<groupId>org.jetbrains.kotlin</groupId>
27-
<artifactId>kotlin-compiler-embeddable</artifactId>
27+
<artifactId>kotlin-compiler</artifactId>
2828
<version>${kotlin.version}</version>
2929
</dependency>
3030

@@ -37,6 +37,7 @@
3737
</dependencies>
3838

3939
<build>
40+
<defaultGoal>clean install</defaultGoal>
4041
<plugins>
4142
<plugin>
4243
<artifactId>kotlin-maven-plugin</artifactId>

src/main/java/hello/CompileTest.kt

+60-38
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,60 @@ package hello
22

33
import com.intellij.openapi.util.Disposer
44
import com.intellij.psi.search.GlobalSearchScope
5+
import org.jetbrains.kotlin.analyzer.ModuleContent
6+
import org.jetbrains.kotlin.analyzer.ModuleInfo
7+
import org.jetbrains.kotlin.analyzer.PlatformAnalysisParameters
8+
import org.jetbrains.kotlin.analyzer.common.DefaultAnalyzerFacade
59
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
6-
import org.jetbrains.kotlin.cli.common.messages.*
7-
import org.jetbrains.kotlin.cli.jvm.compiler.*
8-
import org.jetbrains.kotlin.cli.jvm.config.JVMConfigurationKeys
10+
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation
11+
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
12+
import org.jetbrains.kotlin.cli.common.messages.GroupingMessageCollector
13+
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
14+
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
15+
import org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider
16+
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
917
import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoots
10-
import org.jetbrains.kotlin.cli.jvm.config.getModuleName
1118
import org.jetbrains.kotlin.config.CompilerConfiguration
1219
import org.jetbrains.kotlin.config.addKotlinSourceRoot
13-
import org.jetbrains.kotlin.descriptors.PackageFragmentProvider
14-
import org.jetbrains.kotlin.frontend.java.di.createContainerForTopDownAnalyzerForJvm
15-
import org.jetbrains.kotlin.incremental.components.LookupTracker
20+
import org.jetbrains.kotlin.container.get
21+
import org.jetbrains.kotlin.context.ProjectContext
22+
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
1623
import org.jetbrains.kotlin.load.java.JvmAbi
24+
import org.jetbrains.kotlin.name.Name
1725
import org.jetbrains.kotlin.psi.KtBlockExpression
18-
import org.jetbrains.kotlin.resolve.AnalyzerScriptParameter
26+
import org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer
27+
import org.jetbrains.kotlin.resolve.MultiTargetPlatform
1928
import org.jetbrains.kotlin.resolve.TopDownAnalysisContext
2029
import org.jetbrains.kotlin.resolve.TopDownAnalysisMode
21-
import org.jetbrains.kotlin.resolve.jvm.JvmAnalyzerFacade
22-
import org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM
23-
import org.jetbrains.kotlin.resolve.lazy.declarations.FileBasedDeclarationProviderFactory
2430
import org.jetbrains.kotlin.utils.PathUtil
2531
import java.io.File
2632
import java.util.*
2733
import java.util.logging.Logger
2834

2935
class KotlinScriptParser {
36+
private class SourceModuleInfo(
37+
override val name: Name,
38+
override val capabilities: Map<ModuleDescriptor.Capability<*>, Any?>,
39+
private val dependOnOldBuiltIns: Boolean
40+
) : ModuleInfo {
41+
override fun dependencies() = listOf(this)
42+
43+
override fun dependencyOnBuiltIns(): ModuleInfo.DependencyOnBuiltIns =
44+
if (dependOnOldBuiltIns) ModuleInfo.DependenciesOnBuiltIns.LAST else ModuleInfo.DependenciesOnBuiltIns.NONE
45+
}
46+
3047
companion object {
31-
private val LOG = Logger.getLogger(KotlinScriptParser.javaClass.name)
48+
private val LOG = Logger.getLogger(KotlinScriptParser::class.java.name)
3249
private val messageCollector = object : MessageCollector {
50+
private var hasErrors = false
51+
override fun clear() {
52+
hasErrors = false
53+
}
54+
55+
override fun hasErrors(): Boolean {
56+
return hasErrors
57+
}
58+
3359
override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation) {
3460
val path = location.path
3561
val position = if (path == null) "" else "$path: (${location.line}, ${location.column}) "
@@ -40,6 +66,7 @@ class KotlinScriptParser {
4066
LOG.finest(text)
4167
} else if (CompilerMessageSeverity.ERRORS.contains(severity)) {
4268
LOG.severe(text)
69+
hasErrors = true
4370
} else if (severity == CompilerMessageSeverity.INFO) {
4471
LOG.info(text)
4572
} else {
@@ -60,7 +87,7 @@ class KotlinScriptParser {
6087
val configuration = CompilerConfiguration()
6188

6289
val groupingCollector = GroupingMessageCollector(messageCollector)
63-
val severityCollector = MessageSeverityCollector(groupingCollector)
90+
val severityCollector = GroupingMessageCollector(groupingCollector)
6491
configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, severityCollector)
6592

6693

@@ -69,38 +96,33 @@ class KotlinScriptParser {
6996
files.forEach { configuration.addKotlinSourceRoot(it) }
7097
// Configuring Kotlin class path
7198
configuration.addJvmClasspathRoots(classPath)
72-
configuration.put(JVMConfigurationKeys.MODULE_NAME, JvmAbi.DEFAULT_MODULE_NAME)
73-
configuration.put<List<AnalyzerScriptParameter>>(JVMConfigurationKeys.SCRIPT_PARAMETERS, CommandLineScriptUtils.scriptParameters())
7499

75100
val rootDisposable = Disposer.newDisposable()
76101
try {
77102
val environment = KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES)
78103
val ktFiles = environment.getSourceFiles()
79-
val sharedTrace = CliLightClassGenerationSupport.NoScopeRecordCliBindingTrace()
80-
val moduleContext = TopDownAnalyzerFacadeForJVM.createContextWithSealedModule(environment.project,
81-
environment.getModuleName())
82-
83-
val project = moduleContext.project
84-
val allFiles = JvmAnalyzerFacade.getAllFilesToAnalyze(project, null, ktFiles)
85-
val providerFactory = FileBasedDeclarationProviderFactory(moduleContext.storageManager, allFiles)
86-
val lookupTracker = LookupTracker.DO_NOTHING
87-
val packagePartProvider = JvmPackagePartProvider(environment)
88-
val container = createContainerForTopDownAnalyzerForJvm(
89-
moduleContext,
90-
sharedTrace,
91-
providerFactory,
92-
GlobalSearchScope.allScope(project),
93-
lookupTracker,
94-
packagePartProvider)
95-
96-
val additionalProviders = ArrayList<PackageFragmentProvider>()
97-
98-
additionalProviders.add(container.javaDescriptorResolver.packageFragmentProvider)
99-
100-
return container.lazyTopDownAnalyzerForTopLevel.analyzeFiles(TopDownAnalysisMode.LocalDeclarations, allFiles, additionalProviders)
104+
105+
val capabilities: Map<ModuleDescriptor.Capability<*>, Any?> = mapOf(MultiTargetPlatform.CAPABILITY to MultiTargetPlatform.Common)
106+
107+
val moduleInfo = SourceModuleInfo(Name.special("<${JvmAbi.DEFAULT_MODULE_NAME}"), capabilities, false)
108+
val project = ktFiles.firstOrNull()?.project ?: throw AssertionError("No files to analyze")
109+
val resolver = DefaultAnalyzerFacade.setupResolverForProject(
110+
"sources for metadata serializer",
111+
ProjectContext(project), listOf(moduleInfo),
112+
{ ModuleContent(ktFiles, GlobalSearchScope.allScope(project)) },
113+
object : PlatformAnalysisParameters {},
114+
packagePartProviderFactory = {
115+
_, content -> JvmPackagePartProvider(environment, content.moduleContentScope)
116+
},
117+
modulePlatforms = { MultiTargetPlatform.Common }
118+
)
119+
120+
val container = resolver.resolverForModule(moduleInfo).componentProvider
121+
122+
return container.get<LazyTopDownAnalyzer>().analyzeDeclarations(TopDownAnalysisMode.TopLevelDeclarations, ktFiles)
101123
} finally {
102124
rootDisposable.dispose()
103-
if (severityCollector.anyReported(CompilerMessageSeverity.ERROR)) {
125+
if (severityCollector.hasErrors()) {
104126
throw RuntimeException("Compilation error")
105127
}
106128
}

0 commit comments

Comments
 (0)