Kapt: provide a default constructor if PsiClass does not have any
(cherry picked from commit 550b1c0)
This commit is contained in:
committed by
Yan Zhulanow
parent
7a00b028af
commit
6ceaac63dc
+27
@@ -21,6 +21,7 @@ import com.intellij.psi.impl.source.PsiClassReferenceType
|
||||
import com.intellij.psi.util.ClassUtil
|
||||
import com.intellij.psi.util.PsiTypesUtil
|
||||
import org.jetbrains.kotlin.java.model.*
|
||||
import org.jetbrains.kotlin.java.model.internal.DefaultConstructorPsiMethod
|
||||
import org.jetbrains.kotlin.java.model.types.JeNoneType
|
||||
import org.jetbrains.kotlin.java.model.types.toJeType
|
||||
import javax.lang.model.element.*
|
||||
@@ -75,9 +76,35 @@ class JeTypeElement(override val psi: PsiClass) : JeElement, TypeElement, JeAnno
|
||||
psi.fields.forEach { declarations += JeVariableElement(it) }
|
||||
psi.methods.forEach { declarations += JeMethodExecutableElement(it) }
|
||||
psi.innerClasses.forEach { declarations += JeTypeElement(it) }
|
||||
|
||||
// Add default constructor if possible
|
||||
if (!psi.isInterface && !psi.isAnnotationType && psi.constructors.isEmpty()) {
|
||||
val superClass = psi.superClass
|
||||
val canHaveDefaultConstructor = superClass == null || run {
|
||||
val constructors = superClass.constructors
|
||||
constructors.isEmpty() || constructors.any {
|
||||
(it.hasModifierProperty(PsiModifier.PUBLIC) || it.hasModifierProperty(PsiModifier.PROTECTED) || run {
|
||||
it.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) && psi.packageName == superClass.packageName
|
||||
}) && it.parameterList.parametersCount == 0
|
||||
}
|
||||
}
|
||||
|
||||
if (canHaveDefaultConstructor) {
|
||||
declarations += JeMethodExecutableElement(DefaultConstructorPsiMethod(psi, psi.language).apply {
|
||||
val containingClass = psi.containingClass
|
||||
if (containingClass != null && !psi.hasModifierProperty(PsiModifier.STATIC)) {
|
||||
addParameter("\$instance", PsiTypesUtil.getClassType(containingClass))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return declarations
|
||||
}
|
||||
|
||||
private val PsiClass.packageName: String
|
||||
get() = (containingFile as? PsiJavaFile)?.packageName ?: ""
|
||||
|
||||
fun getAllMembers(): List<Element> {
|
||||
val declarations = mutableListOf<Element>()
|
||||
psi.allFields.forEach { declarations += JeVariableElement(it) }
|
||||
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.java.model.internal
|
||||
|
||||
import com.intellij.lang.Language
|
||||
import com.intellij.psi.PsiClass
|
||||
import com.intellij.psi.PsiModifier.*
|
||||
import com.intellij.psi.impl.light.LightMethodBuilder
|
||||
|
||||
internal class DefaultConstructorPsiMethod(
|
||||
clazz: PsiClass,
|
||||
language: Language
|
||||
) : LightMethodBuilder(clazz, language) {
|
||||
init {
|
||||
val modifier = when {
|
||||
clazz.hasModifierProperty(PUBLIC) -> PUBLIC
|
||||
clazz.hasModifierProperty(PRIVATE) -> PRIVATE
|
||||
clazz.hasModifierProperty(PROTECTED) -> PROTECTED
|
||||
else -> PACKAGE_LOCAL
|
||||
}
|
||||
setModifiers(modifier)
|
||||
isConstructor = true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user