[groovy] use standard RecursionManager#doPreventingRecursion in transformations

GitOrigin-RevId: 9ce3a7b2a7214d6f5ad1beb3de0ff7a4ad930426
This commit is contained in:
Daniil Ovchinnikov
2019-12-17 18:02:22 +00:00
committed by intellij-monorepo-bot
parent 51719630c3
commit f633ee7073
5 changed files with 36 additions and 59 deletions
@@ -1,11 +1,10 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.groovy.lang.psi.impl.javaView
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiClass
import com.intellij.psi.impl.light.LightElement
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.plugins.groovy.transformations.isUnderTransformation
import org.jetbrains.plugins.groovy.util.getPackageAndShortName
class GroovyLightInnerClassFinder(project: Project) : GroovyClassFinder(project) {
@@ -22,11 +21,6 @@ class GroovyLightInnerClassFinder(project: Project) : GroovyClassFinder(project)
}
private fun findInnerLightClass(clazz: PsiClass, name: String): PsiClass? {
return if (isUnderTransformation(clazz)) {
null
}
else {
clazz.findInnerClassByName(name, false) as? LightElement as? PsiClass
}
return clazz.findInnerClassByName(name, false) as? LightElement as? PsiClass
}
}
@@ -1,18 +1,4 @@
/*
* Copyright 2000-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.
*/
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.groovy.lang.psi.impl.statements.typedef;
import com.intellij.psi.PsiClass;
@@ -83,7 +69,6 @@ public class GrTypeDefinitionMembersCache<T extends GrTypeDefinition> {
}
public PsiMethod[] getConstructors() {
assert !TransformationUtilKt.isUnderTransformation(myDefinition);
return CachedValuesManager.getCachedValue(myDefinition, () -> CachedValueProvider.Result.create(
GrClassImplUtil.getConstructors(myDefinition), myDependencies
)).clone();
@@ -95,7 +80,6 @@ public class GrTypeDefinitionMembersCache<T extends GrTypeDefinition> {
@NotNull
public PsiClassType[] getExtendsListTypes(boolean includeSynthetic) {
if (includeSynthetic && TransformationUtilKt.isUnderTransformation(myDefinition)) includeSynthetic = false;
return CachedValuesManager.getCachedValue(myDefinition, includeSynthetic ? () -> {
PsiClassType[] extendsTypes = getTransformationResult().getExtendsTypes();
return CachedValueProvider.Result.create(extendsTypes, myDependencies);
@@ -107,7 +91,6 @@ public class GrTypeDefinitionMembersCache<T extends GrTypeDefinition> {
@NotNull
public PsiClassType[] getImplementsListTypes(boolean includeSynthetic) {
if (includeSynthetic && TransformationUtilKt.isUnderTransformation(myDefinition)) includeSynthetic = false;
return CachedValuesManager.getCachedValue(myDefinition, includeSynthetic ? () -> {
PsiClassType[] implementsTypes = getTransformationResult().getImplementsTypes();
return CachedValueProvider.Result.create(implementsTypes, myDependencies);
@@ -12,7 +12,6 @@ import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierL
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition;
import org.jetbrains.plugins.groovy.lang.psi.util.GrTraitUtil;
import org.jetbrains.plugins.groovy.transformations.TransformationUtilKt;
import java.util.*;
@@ -58,7 +57,6 @@ public class CollectClassMembersUtil {
PsiClass current = queue.remove();
if (current instanceof ClsClassImpl) continue;
if (visited.add(current)) {
if (TransformationUtilKt.isUnderTransformation(current)) return false;
for (PsiClass superClass : getSupers(current, false)) {
queue.offer(superClass);
}
@@ -16,7 +16,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.ClassUtil;
import org.jetbrains.plugins.groovy.lang.resolve.processors.MultiProcessor;
import org.jetbrains.plugins.groovy.transformations.TransformationUtilKt;
import java.util.ArrayList;
import java.util.Collection;
@@ -147,7 +146,6 @@ public abstract class NonCodeMembersContributor {
ensureInit();
final PsiClass aClass = PsiTypesUtil.getPsiClass(qualifierType);
if (TransformationUtilKt.isUnderTransformation(aClass)) return true;
final Iterable<? extends PsiScopeProcessor> unwrappedOriginals = MultiProcessor.allProcessors(processor);
for (PsiScopeProcessor each : unwrappedOriginals) {
@@ -1,26 +1,17 @@
/*
* Copyright 2000-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.
*/
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.groovy.transformations
import com.intellij.openapi.Disposable
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.RecursionManager
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiClassType
import com.intellij.psi.PsiMethod
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition
import kotlin.reflect.jvm.javaField
class TransformationResult(
val methods: Array<PsiMethod>,
@@ -30,29 +21,42 @@ class TransformationResult(
val extendsTypes: Array<PsiClassType>
)
private val ourTransformationContext = object : ThreadLocal<MutableSet<GrTypeDefinition>>() {
override fun initialValue(): MutableSet<GrTypeDefinition> = HashSet()
private val emptyTransformationResult = TransformationResult(
PsiMethod.EMPTY_ARRAY,
GrField.EMPTY_ARRAY,
PsiClass.EMPTY_ARRAY,
PsiClassType.EMPTY_ARRAY,
PsiClassType.EMPTY_ARRAY
)
private val LOG: Logger = Logger.getInstance(::LOG.javaField!!.javaClass)
private var ourAssertOnRecursion: Boolean = true
fun disableAssertOnRecursion(disposable: Disposable) {
if (!ourAssertOnRecursion) {
return
}
RecursionManager.disableMissedCacheAssertions(disposable)
ourAssertOnRecursion = false
Disposer.register(disposable, Disposable {
ourAssertOnRecursion = true
})
}
private inline val transformationContext get() = ourTransformationContext.get()
fun transformDefinition(definition: GrTypeDefinition): TransformationResult {
assert(transformationContext.add(definition))
try {
return RecursionManager.doPreventingRecursion(definition, false) {
val transformationContext = TransformationContextImpl(definition)
for (transformation in AstTransformationSupport.EP_NAME.extensions) {
ProgressManager.checkCanceled()
transformation.applyTransformation(transformationContext)
}
return transformationContext.transformationResult
transformationContext.transformationResult
} ?: run {
if (ourAssertOnRecursion) {
LOG.error("recursion")
}
emptyTransformationResult
}
finally {
transformationContext.remove(definition)
}
}
fun isUnderTransformation(clazz: PsiClass?): Boolean {
return clazz is GrTypeDefinition && clazz in transformationContext
}
infix operator fun TransformationContext.plusAssign(method: PsiMethod): Unit = addMethod(method)