mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[groovy] extract type calculator for 'new Xxx()' expression
This commit is contained in:
+3
-35
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2016 JetBrains s.r.o.
|
||||
* Copyright 2000-2017 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.
|
||||
@@ -18,14 +18,11 @@ package org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions;
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.source.resolve.ResolveCache;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.NullableFunction;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -38,18 +35,16 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrArrayD
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrBuiltInTypeElement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.dataFlow.types.TypeInferenceHelper;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.impl.GrAnonymousClassType;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.impl.GrClassReferenceType;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.impl.GrMapType;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.path.GrCallExpressionImpl;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.util.GrInnerClassConstructorUtil;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil;
|
||||
import org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil;
|
||||
import org.jetbrains.plugins.groovy.lang.typing.GrTypeCalculator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -59,33 +54,6 @@ import java.util.List;
|
||||
*/
|
||||
public class GrNewExpressionImpl extends GrCallExpressionImpl implements GrNewExpression {
|
||||
|
||||
private static final Function<GrNewExpressionImpl,PsiType> MY_TYPE_CALCULATOR =
|
||||
(NullableFunction<GrNewExpressionImpl, PsiType>)newExpression -> {
|
||||
final GrAnonymousClassDefinition anonymous = newExpression.getAnonymousClassDefinition();
|
||||
if (anonymous != null) {
|
||||
return new GrAnonymousClassType(LanguageLevel.JDK_1_5, anonymous.getResolveScope(),
|
||||
JavaPsiFacade.getInstance(newExpression.getProject()), anonymous);
|
||||
}
|
||||
PsiType type = null;
|
||||
GrCodeReferenceElement refElement = newExpression.getReferenceElement();
|
||||
if (refElement != null) {
|
||||
type = new GrClassReferenceType(refElement);
|
||||
}
|
||||
else {
|
||||
GrBuiltInTypeElement builtin = newExpression.findChildByClass(GrBuiltInTypeElement.class);
|
||||
if (builtin != null) type = builtin.getType();
|
||||
}
|
||||
|
||||
if (type != null) {
|
||||
for (int i = 0; i < newExpression.getArrayCount(); i++) {
|
||||
type = type.createArrayType();
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
private static final ResolveCache.PolyVariantResolver<MyFakeReference> RESOLVER = new ResolveCache.PolyVariantResolver<MyFakeReference>() {
|
||||
@NotNull
|
||||
@Override
|
||||
@@ -111,7 +79,7 @@ public class GrNewExpressionImpl extends GrCallExpressionImpl implements GrNewEx
|
||||
|
||||
@Override
|
||||
public PsiType getType() {
|
||||
return TypeInferenceHelper.getCurrentContext().getExpressionType(this, MY_TYPE_CALCULATOR);
|
||||
return TypeInferenceHelper.getCurrentContext().getExpressionType(this, GrTypeCalculator::getTypeFromCalculators);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2000-2017 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.plugins.groovy.lang.typing
|
||||
|
||||
import com.intellij.pom.java.LanguageLevel
|
||||
import com.intellij.psi.JavaPsiFacade
|
||||
import com.intellij.psi.PsiType
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrBuiltInTypeElement
|
||||
import org.jetbrains.plugins.groovy.lang.psi.impl.GrAnonymousClassType
|
||||
import org.jetbrains.plugins.groovy.lang.psi.impl.GrClassReferenceType
|
||||
|
||||
class DefaultNewExpressionTypeCalculator : GrTypeCalculator<GrNewExpression> {
|
||||
|
||||
override fun getType(expression: GrNewExpression): PsiType? {
|
||||
return getAnonymousType(expression) ?:
|
||||
getRegularType(expression)
|
||||
}
|
||||
|
||||
private fun getAnonymousType(expression: GrNewExpression): PsiType? {
|
||||
val anonymous = expression.anonymousClassDefinition ?: return null
|
||||
return GrAnonymousClassType(
|
||||
LanguageLevel.JDK_1_5,
|
||||
anonymous.resolveScope,
|
||||
JavaPsiFacade.getInstance(expression.project),
|
||||
anonymous
|
||||
)
|
||||
}
|
||||
|
||||
private fun getRegularType(expression: GrNewExpression): PsiType? {
|
||||
var type: PsiType = expression.referenceElement?.let { GrClassReferenceType(it) } ?:
|
||||
(expression.typeElement as? GrBuiltInTypeElement)?.type ?:
|
||||
return null
|
||||
repeat(expression.arrayCount) {
|
||||
type = type.createArrayType()
|
||||
}
|
||||
return type
|
||||
}
|
||||
}
|
||||
@@ -170,6 +170,8 @@
|
||||
<typeCalculator forClass="org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall"
|
||||
implementationClass="org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.path.GrDescriptorReturnTypeCalculator"/>
|
||||
|
||||
<typeCalculator forClass="org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression"
|
||||
implementationClass="org.jetbrains.plugins.groovy.lang.typing.DefaultNewExpressionTypeCalculator" order="last"/>
|
||||
<typeCalculator forClass="org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap" order="last"
|
||||
implementationClass="org.jetbrains.plugins.groovy.lang.typing.DefaultListOrMapTypeCalculator"/>
|
||||
<typeCalculator forClass="org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty" order="last"
|
||||
|
||||
Reference in New Issue
Block a user