use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class CreateLocalVariableFromUsageFix method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final PsiFile file = element.getContainingFile();
PsiClassType type = JavaPsiFacade.getInstance(project).getElementFactory().createTypeByFQClassName("Object", GlobalSearchScope.allScope(project));
GrVariableDeclaration decl = GroovyPsiElementFactory.getInstance(project).createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, "", type, myRefExpression.getReferenceName());
int offset = myRefExpression.getTextRange().getStartOffset();
GrStatement anchor = findAnchor(file, offset);
TypeConstraint[] constraints = GroovyExpectedTypesProvider.calculateTypeConstraints(myRefExpression);
if (myRefExpression.equals(anchor)) {
decl = myRefExpression.replaceWithStatement(decl);
} else {
decl = myOwner.addVariableDeclarationBefore(decl, anchor);
}
GrTypeElement typeElement = decl.getTypeElementGroovy();
assert typeElement != null;
ChooseTypeExpression expr = new ChooseTypeExpression(constraints, PsiManager.getInstance(project), typeElement.getResolveScope());
TemplateBuilderImpl builder = new TemplateBuilderImpl(decl);
builder.replaceElement(typeElement, expr);
decl = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(decl);
Template template = builder.buildTemplate();
Editor newEditor = positionCursor(project, myOwner.getContainingFile(), decl);
TextRange range = decl.getTextRange();
newEditor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
TemplateManager manager = TemplateManager.getInstance(project);
manager.startTemplate(newEditor, template);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class ExpressionGenerator method visitNewExpression.
@Override
public void visitNewExpression(@NotNull GrNewExpression newExpression) {
boolean hasFieldInitialization = hasFieldInitialization(newExpression);
StringBuilder builder;
final PsiType type = newExpression.getType();
final String varName;
if (hasFieldInitialization) {
builder = new StringBuilder();
varName = GenerationUtil.suggestVarName(type, newExpression, context);
TypeWriter.writeType(builder, type, newExpression);
builder.append(' ').append(varName).append(" = ");
} else {
varName = null;
builder = this.builder;
}
final GrTypeElement typeElement = newExpression.getTypeElement();
final GrArrayDeclaration arrayDeclaration = newExpression.getArrayDeclaration();
final GrCodeReferenceElement referenceElement = newExpression.getReferenceElement();
builder.append("new ");
if (typeElement != null) {
final PsiType builtIn = typeElement.getType();
LOG.assertTrue(builtIn instanceof PsiPrimitiveType);
final PsiType boxed = TypesUtil.boxPrimitiveType(builtIn, newExpression.getManager(), newExpression.getResolveScope());
TypeWriter.writeTypeForNew(builder, boxed, newExpression);
} else if (referenceElement != null) {
GenerationUtil.writeCodeReferenceElement(builder, referenceElement);
}
final GrArgumentList argList = newExpression.getArgumentList();
if (argList != null) {
GrClosureSignature signature = null;
final GroovyResolveResult resolveResult = newExpression.advancedResolve();
final PsiElement constructor = resolveResult.getElement();
if (constructor instanceof PsiMethod) {
signature = GrClosureSignatureUtil.createSignature((PsiMethod) constructor, resolveResult.getSubstitutor());
} else if (referenceElement != null) {
final GroovyResolveResult clazzResult = referenceElement.advancedResolve();
final PsiElement clazz = clazzResult.getElement();
if (clazz instanceof PsiClass && ((PsiClass) clazz).getConstructors().length == 0) {
signature = GrClosureSignatureUtil.createSignature(PsiParameter.EMPTY_ARRAY, null);
}
}
final GrNamedArgument[] namedArgs = hasFieldInitialization ? GrNamedArgument.EMPTY_ARRAY : argList.getNamedArguments();
new ArgumentListGenerator(builder, context).generate(signature, argList.getExpressionArguments(), namedArgs, GrClosableBlock.EMPTY_ARRAY, newExpression);
}
final GrAnonymousClassDefinition anonymous = newExpression.getAnonymousClassDefinition();
if (anonymous != null) {
writeTypeBody(builder, anonymous);
}
if (arrayDeclaration != null) {
final GrExpression[] boundExpressions = arrayDeclaration.getBoundExpressions();
for (GrExpression boundExpression : boundExpressions) {
builder.append('[');
boundExpression.accept(this);
builder.append(']');
}
if (boundExpressions.length == 0) {
builder.append("[]");
}
}
if (hasFieldInitialization) {
builder.append(';');
context.myStatements.add(builder.toString());
final GrNamedArgument[] namedArguments = argList.getNamedArguments();
for (GrNamedArgument namedArgument : namedArguments) {
final String fieldName = namedArgument.getLabelName();
if (fieldName == null) {
//todo try to initialize field
final GrArgumentLabel label = namedArgument.getLabel();
LOG.info("cannot initialize field " + (label == null ? "<null>" : label.getText()));
} else {
final GroovyResolveResult resolveResult = referenceElement.advancedResolve();
final PsiElement resolved = resolveResult.getElement();
LOG.assertTrue(resolved instanceof PsiClass);
initializeField(varName, type, ((PsiClass) resolved), resolveResult.getSubstitutor(), fieldName, namedArgument.getExpression());
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class ExpressionGenerator method visitCastExpression.
@Override
public void visitCastExpression(@NotNull GrTypeCastExpression typeCastExpression) {
final GrTypeElement typeElement = typeCastExpression.getCastTypeElement();
final GrExpression operand = typeCastExpression.getOperand();
generateCast(typeElement, operand);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class GroovyTypeCheckVisitor method visitParameterList.
@Override
public void visitParameterList(@NotNull final GrParameterList parameterList) {
super.visitParameterList(parameterList);
PsiElement parent = parameterList.getParent();
if (!(parent instanceof GrClosableBlock))
return;
GrParameter[] parameters = parameterList.getParameters();
if (parameters.length > 0) {
List<PsiType[]> signatures = ClosureParamsEnhancer.findFittingSignatures((GrClosableBlock) parent);
final List<PsiType> paramTypes = ContainerUtil.map(parameters, parameter -> parameter.getType());
if (signatures.size() > 1) {
final PsiType[] fittingSignature = ContainerUtil.find(signatures, types -> {
for (int i = 0; i < types.length; i++) {
if (!TypesUtil.isAssignableByMethodCallConversion(paramTypes.get(i), types[i], parameterList)) {
return false;
}
}
return true;
});
if (fittingSignature == null) {
registerError(parameterList, GroovyInspectionBundle.message("no.applicable.signature.found"), null, ProblemHighlightType.GENERIC_ERROR);
}
} else if (signatures.size() == 1) {
PsiType[] types = signatures.get(0);
for (int i = 0; i < types.length; i++) {
GrTypeElement typeElement = parameters[i].getTypeElementGroovy();
if (typeElement == null)
continue;
PsiType expected = types[i];
PsiType actual = paramTypes.get(i);
if (!TypesUtil.isAssignableByMethodCallConversion(actual, expected, parameterList)) {
registerError(typeElement, GroovyInspectionBundle.message("expected.type.0", expected.getCanonicalText(false), actual.getCanonicalText(false)), null, ProblemHighlightType.GENERIC_ERROR);
}
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class AsTypeTransformation method apply.
@Override
protected void apply(@NotNull GrSafeCastExpression expression) {
GrExpression lhsParenthesized = addParenthesesIfNeeded(expression.getOperand());
GrTypeElement typeElement = Objects.requireNonNull(expression.getCastTypeElement());
replaceExpression(expression, format("%s.asType(%s)", lhsParenthesized.getText(), typeElement.getText()));
}
Aggregations