Skip to content

Commit 14db479

Browse files
committed
Do not generate light classes repeatedly
1 parent 8df2f44 commit 14db479

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

kotlin-eclipse-core/src/org/jetbrains/kotlin/core/asJava/KotlinLightClassGeneration.kt

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*******************************************************************************
2+
* Copyright 2000-2015 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*******************************************************************************/
117
package org.jetbrains.kotlin.core.asJava
218

319
import org.eclipse.core.resources.IFile
@@ -12,6 +28,15 @@ import org.jetbrains.kotlin.core.model.KotlinEnvironment
1228
import org.jetbrains.kotlin.core.model.KotlinJavaManager
1329
import org.jetbrains.kotlin.psi.KtFile
1430
import com.intellij.openapi.project.Project
31+
import org.jetbrains.kotlin.psi.KtClassOrObject
32+
import org.jetbrains.kotlin.psi.KtScript
33+
import org.jetbrains.kotlin.fileClasses.NoResolveFileClassesProvider
34+
import org.jetbrains.kotlin.codegen.binding.PsiCodegenPredictor
35+
import org.eclipse.core.runtime.Path
36+
import org.jetbrains.kotlin.fileClasses.getFileClassInternalName
37+
import org.jetbrains.kotlin.psi.KtObjectDeclaration
38+
import org.jetbrains.kotlin.psi.KtClass
39+
import com.intellij.psi.util.PsiTreeUtil
1540

1641
object KotlinLightClassGeneration {
1742
fun updateLightClasses(javaProject: IJavaProject, affectedFiles: Set<IFile>) {
@@ -21,16 +46,50 @@ object KotlinLightClassGeneration {
2146
KotlinLightClassManager.getInstance(javaProject).updateLightClasses(affectedFiles)
2247
}
2348

24-
fun buildLightClasses(analysisResult: AnalysisResult, javaProject: IJavaProject, jetFiles: List<KtFile>) : GenerationState {
49+
fun buildLightClasses(
50+
analysisResult: AnalysisResult,
51+
javaProject: IJavaProject,
52+
jetFiles: List<KtFile>,
53+
requestedClassName: String): GenerationState {
2554
val state = GenerationState(
2655
KotlinEnvironment.getEnvironment(javaProject).project,
2756
LightClassBuilderFactory(),
2857
analysisResult.moduleDescriptor,
2958
analysisResult.bindingContext,
30-
jetFiles)
59+
jetFiles,
60+
generateDeclaredClassFilter = object : GenerationState.GenerateClassFilter() {
61+
override fun shouldAnnotateClass(classOrObject: KtClassOrObject): Boolean = true
62+
63+
override fun shouldGenerateClass(classOrObject: KtClassOrObject): Boolean {
64+
val internalName = PsiCodegenPredictor.getPredefinedJvmInternalName(classOrObject, NoResolveFileClassesProvider)
65+
return checkByInternalName(internalName, requestedClassName)
66+
}
67+
68+
override fun shouldGeneratePackagePart(ktFile: KtFile): Boolean {
69+
val internalName = NoResolveFileClassesProvider.getFileClassInternalName(ktFile)
70+
return checkByInternalName(internalName, requestedClassName)
71+
}
72+
73+
override fun shouldGenerateScript(script: KtScript): Boolean = false
74+
})
3175

3276
KotlinCodegenFacade.compileCorrectFiles(state) { exception, fileUrl -> Unit }
3377

3478
return state
3579
}
80+
81+
private fun checkByInternalName(internalName: String?, requestedClassFileName: String): Boolean {
82+
if (internalName == null) return false
83+
84+
val classFileName = Path(internalName).lastSegment()
85+
val requestedInternalName = requestedClassFileName.dropLast(".class".length)
86+
87+
if (requestedInternalName.startsWith(classFileName)) {
88+
if (requestedInternalName.length == classFileName.length) return true
89+
90+
if (requestedInternalName[classFileName.length] == '$') return true
91+
}
92+
93+
return false
94+
}
3695
}

kotlin-eclipse-core/src/org/jetbrains/kotlin/core/filesystem/KotlinFileStore.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*******************************************************************************
2+
* Copyright 2000-2015 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*******************************************************************************/
117
package org.jetbrains.kotlin.core.filesystem
218

319
import java.io.ByteArrayInputStream
@@ -43,8 +59,8 @@ public class KotlinFileStore(file: File) : LocalFile(file) {
4359
val analysisResult = KotlinAnalysisProjectCache.getAnalysisResultIfCached(javaProject) ?:
4460
KotlinAnalyzer.analyzeFiles(javaProject, jetFiles).analysisResult
4561

46-
val state = KotlinLightClassGeneration.buildLightClasses(analysisResult, javaProject, jetFiles)
4762
val requestedClassName = Path(file.getAbsolutePath()).lastSegment()
63+
val state = KotlinLightClassGeneration.buildLightClasses(analysisResult, javaProject, jetFiles, requestedClassName)
4864
val generatedClass = state.factory.asList().find {
4965
val generatedClassName = Path(it.relativePath).lastSegment()
5066
requestedClassName == generatedClassName

0 commit comments

Comments
 (0)