use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class GrTraitType method createTraitType.
// todo move this method to org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.types.GrSafeCastExpressionImpl
@Nullable
public static PsiType createTraitType(@NotNull GrSafeCastExpression safeCastExpression) {
GrExpression operand = safeCastExpression.getOperand();
PsiType exprType = operand.getType();
if (!(exprType instanceof PsiClassType) && !(exprType instanceof GrTraitType))
return null;
GrTypeElement typeElement = safeCastExpression.getCastTypeElement();
if (typeElement == null)
return null;
PsiType type = typeElement.getType();
if (!GrTraitUtil.isTrait(PsiTypesUtil.getPsiClass(type)))
return null;
return createTraitType(exprType, ContainerUtil.newSmartList(type));
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class GroovyPsiElementFactoryImpl method createReferenceElementFromText.
@NotNull
@Override
public GrCodeReferenceElement createReferenceElementFromText(@NotNull String refName, final PsiElement context) {
GroovyFile file = createGroovyFileChecked("(" + refName + ")foo", false, context);
GrTypeElement typeElement = ((GrTypeCastExpression) file.getTopStatements()[0]).getCastTypeElement();
return ((GrClassTypeElement) typeElement).getReferenceElement();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class GrVariableBaseImpl method getTypeGroovy.
@Override
@Nullable
public PsiType getTypeGroovy() {
final GrExpression initializer = getInitializerGroovy();
GrTypeElement typeElement = getTypeElementGroovy();
PsiType declaredType = null;
if (typeElement != null) {
declaredType = typeElement.getType();
if (!(declaredType instanceof PsiClassType)) {
return declaredType;
}
}
if (initializer != null) {
PsiType initializerType = ourGuard.doPreventingRecursion(this, true, initializer::getType);
if (declaredType == null)
return initializerType;
if (initializerType instanceof PsiClassType && TypesUtil.isAssignable(declaredType, initializerType, this)) {
final PsiClassType.ClassResolveResult initializerResult = ((PsiClassType) initializerType).resolveGenerics();
final PsiClass initializerClass = initializerResult.getElement();
if (initializerClass != null && !com.intellij.psi.util.PsiUtil.isRawSubstitutor(initializerClass, initializerResult.getSubstitutor())) {
return initializerType;
}
}
}
return declaredType;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class GrVariableBaseImpl method setType.
@Override
public void setType(@Nullable PsiType type) {
final GrVariableDeclaration variableDeclaration = getDeclaration();
if (variableDeclaration == null)
return;
final GrTypeElement typeElement = variableDeclaration.getTypeElementGroovyForVariable(this);
if (type == null) {
if (typeElement != null) {
if (!variableDeclaration.isTuple() && variableDeclaration.getModifierList().getModifiers().length == 0) {
variableDeclaration.getModifierList().setModifierProperty(GrModifier.DEF, true);
}
typeElement.delete();
}
return;
}
type = TypesUtil.unboxPrimitiveTypeWrapper(type);
GrTypeElement newTypeElement;
try {
newTypeElement = GroovyPsiElementFactory.getInstance(getProject()).createTypeElement(type);
} catch (IncorrectOperationException e) {
LOG.error(e);
return;
}
if (typeElement == null) {
newTypeElement = (GrTypeElement) getParent().addBefore(newTypeElement, this);
} else {
newTypeElement = (GrTypeElement) typeElement.replace(newTypeElement);
}
JavaCodeStyleManager.getInstance(getProject()).shortenClassReferences(newTypeElement);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class GrVariableDeclarationImpl method setType.
@Override
public void setType(@Nullable PsiType type) {
final GrTypeElement typeElement = getTypeElementGroovy();
if (type == null) {
if (typeElement == null)
return;
if (getModifierList().getModifiers().length == 0) {
getModifierList().setModifierProperty(GrModifier.DEF, true);
}
typeElement.delete();
return;
}
type = TypesUtil.unboxPrimitiveTypeWrapper(type);
GrTypeElement newTypeElement;
try {
newTypeElement = GroovyPsiElementFactory.getInstance(getProject()).createTypeElement(type);
} catch (IncorrectOperationException e) {
LOG.error(e);
return;
}
if (typeElement == null) {
getModifierList().setModifierProperty(GrModifier.DEF, false);
final GrVariable[] variables = getVariables();
if (variables.length == 0)
return;
newTypeElement = (GrTypeElement) addBefore(newTypeElement, variables[0]);
} else {
newTypeElement = (GrTypeElement) typeElement.replace(newTypeElement);
}
JavaCodeStyleManager.getInstance(getProject()).shortenClassReferences(newTypeElement);
}
Aggregations