use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class DynamicDialog method doOKAction.
@Override
protected void doOKAction() {
super.doOKAction();
mySettings.setContainingClassName((String) myClassComboBox.getSelectedItem());
mySettings.setStatic(myStaticCheckBox.isSelected());
GrTypeElement typeElement = getEnteredTypeName();
if (typeElement == null) {
mySettings.setType(CommonClassNames.JAVA_LANG_OBJECT);
} else {
PsiType type = typeElement.getType();
if (type instanceof PsiPrimitiveType) {
type = TypesUtil.boxPrimitiveType(type, typeElement.getManager(), ProjectScope.getAllScope(myProject));
}
final String typeQualifiedName = type.getCanonicalText();
if (typeQualifiedName != null) {
mySettings.setType(typeQualifiedName);
} else {
mySettings.setType(type.getPresentableText());
}
}
final Document document = PsiDocumentManager.getInstance(myProject).getDocument(myContext.getContainingFile());
CommandProcessor.getInstance().executeCommand(myProject, () -> {
UndoManager.getInstance(myProject).undoableActionPerformed(new GlobalUndoableAction(document) {
@Override
public void undo() throws UnexpectedUndoException {
final DItemElement itemElement;
if (mySettings.isMethod()) {
final List<ParamInfo> myPairList = mySettings.getParams();
final String[] argumentsTypes = QuickfixUtil.getArgumentsTypes(myPairList);
itemElement = myDynamicManager.findConcreteDynamicMethod(mySettings.getContainingClassName(), mySettings.getName(), argumentsTypes);
} else {
itemElement = myDynamicManager.findConcreteDynamicProperty(mySettings.getContainingClassName(), mySettings.getName());
}
if (itemElement == null) {
Messages.showWarningDialog(myProject, GroovyInspectionBundle.message("Cannot.perform.undo.operation"), GroovyInspectionBundle.message("Undo.disable"));
return;
}
final DClassElement classElement = myDynamicManager.getClassElementByItem(itemElement);
if (classElement == null) {
Messages.showWarningDialog(myProject, GroovyInspectionBundle.message("Cannot.perform.undo.operation"), GroovyInspectionBundle.message("Undo.disable"));
return;
}
removeElement(itemElement);
if (classElement.getMethods().isEmpty() && classElement.getProperties().isEmpty()) {
myDynamicManager.removeClassElement(classElement);
}
}
@Override
public void redo() throws UnexpectedUndoException {
addElement(mySettings);
}
});
addElement(mySettings);
}, "Add dynamic element", null);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class DynamicDialog method doValidate.
@Override
protected ValidationInfo doValidate() {
final GrTypeElement typeElement = getEnteredTypeName();
if (typeElement == null) {
return new ValidationInfo(GroovyInspectionBundle.message("no.type.specified"), myTypeComboBox);
}
final PsiType type = typeElement.getType();
if (type instanceof PsiClassType && ((PsiClassType) type).resolve() == null) {
return new ValidationInfo(GroovyInspectionBundle.message("unresolved.type.status", type.getPresentableText()), myTypeComboBox);
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class GrChangeSignatureUsageProcessor method processPrimaryMethodInner.
private static boolean processPrimaryMethodInner(JavaChangeInfo changeInfo, GrMethod method, @Nullable PsiMethod baseMethod) {
if (changeInfo.isNameChanged()) {
String newName = baseMethod == null ? changeInfo.getNewName() : RefactoringUtil.suggestNewOverriderName(method.getName(), baseMethod.getName(), changeInfo.getNewName());
if (newName != null && !newName.equals(method.getName())) {
method.setName(changeInfo.getNewName());
}
}
final GrModifierList modifierList = method.getModifierList();
if (changeInfo.isVisibilityChanged()) {
modifierList.setModifierProperty(changeInfo.getNewVisibility(), true);
}
PsiSubstitutor substitutor = baseMethod != null ? calculateSubstitutor(method, baseMethod) : PsiSubstitutor.EMPTY;
final PsiMethod context = changeInfo.getMethod();
GrTypeElement oldReturnTypeElement = method.getReturnTypeElementGroovy();
if (changeInfo.isReturnTypeChanged()) {
CanonicalTypes.Type newReturnType = changeInfo.getNewReturnType();
if (newReturnType == null) {
if (oldReturnTypeElement != null) {
oldReturnTypeElement.delete();
if (modifierList.getModifiers().length == 0) {
modifierList.setModifierProperty(GrModifier.DEF, true);
}
}
} else {
PsiType type = newReturnType.getType(context, method.getManager());
GrReferenceAdjuster.shortenAllReferencesIn(method.setReturnType(substitutor.substitute(type)));
if (oldReturnTypeElement == null) {
modifierList.setModifierProperty(GrModifier.DEF, false);
}
}
}
JavaParameterInfo[] newParameters = changeInfo.getNewParameters();
final GrParameterList parameterList = method.getParameterList();
GrParameter[] oldParameters = parameterList.getParameters();
final PsiParameter[] oldBaseParams = baseMethod != null ? baseMethod.getParameterList().getParameters() : null;
Set<GrParameter> toRemove = new HashSet<>(oldParameters.length);
ContainerUtil.addAll(toRemove, oldParameters);
GrParameter anchor = null;
final GrDocComment docComment = method.getDocComment();
final GrDocTag[] tags = docComment == null ? null : docComment.getTags();
int newParamIndex = 0;
for (JavaParameterInfo newParameter : newParameters) {
//if old parameter name differs from base method parameter name we don't change it
final String newName;
final int oldIndex = newParameter.getOldIndex();
if (oldIndex >= 0 && oldBaseParams != null) {
final String oldName = oldParameters[oldIndex].getName();
if (oldName.equals(oldBaseParams[oldIndex].getName())) {
newName = newParameter.getName();
} else {
newName = oldName;
}
} else {
newName = newParameter.getName();
}
final GrParameter oldParameter = oldIndex >= 0 ? oldParameters[oldIndex] : null;
if (docComment != null && oldParameter != null) {
final String oldName = oldParameter.getName();
for (GrDocTag tag : tags) {
if ("@param".equals(tag.getName())) {
final GrDocParameterReference parameterReference = tag.getDocParameterReference();
if (parameterReference != null && oldName.equals(parameterReference.getText())) {
parameterReference.handleElementRename(newName);
}
}
}
}
GrParameter grParameter = createNewParameter(substitutor, context, parameterList, newParameter, newName);
if (oldParameter != null) {
grParameter.getModifierList().replace(oldParameter.getModifierList());
}
if ("def".equals(newParameter.getTypeText())) {
grParameter.getModifierList().setModifierProperty(GrModifier.DEF, true);
} else if (StringUtil.isEmpty(newParameter.getTypeText())) {
grParameter.getModifierList().setModifierProperty(GrModifier.DEF, false);
}
anchor = (GrParameter) parameterList.addAfter(grParameter, anchor);
if (newParamIndex < oldParameters.length) {
GrParameter oldParam = oldParameters[newParamIndex];
PsiElement prev = oldParam.getPrevSibling();
if (prev instanceof PsiWhiteSpace) {
parameterList.addBefore(prev, anchor);
}
}
newParamIndex++;
}
for (GrParameter oldParameter : toRemove) {
oldParameter.delete();
}
JavaCodeStyleManager.getInstance(parameterList.getProject()).shortenClassReferences(parameterList);
CodeStyleManager.getInstance(parameterList.getProject()).reformat(parameterList);
if (changeInfo.isExceptionSetOrOrderChanged()) {
final ThrownExceptionInfo[] infos = changeInfo.getNewExceptions();
PsiClassType[] exceptionTypes = new PsiClassType[infos.length];
for (int i = 0; i < infos.length; i++) {
ThrownExceptionInfo info = infos[i];
exceptionTypes[i] = (PsiClassType) info.createType(method, method.getManager());
}
PsiReferenceList thrownList = GroovyPsiElementFactory.getInstance(method.getProject()).createThrownList(exceptionTypes);
thrownList = (PsiReferenceList) method.getThrowsList().replace(thrownList);
JavaCodeStyleManager.getInstance(thrownList.getProject()).shortenClassReferences(thrownList);
CodeStyleManager.getInstance(method.getProject()).reformat(method.getThrowsList());
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class ExpressionGenerator method visitInstanceofExpression.
@Override
public void visitInstanceofExpression(@NotNull GrInstanceOfExpression expression) {
final GrExpression operand = expression.getOperand();
final GrTypeElement typeElement = expression.getTypeElement();
writeInstanceof(operand, typeElement != null ? typeElement.getType() : null, expression);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class ExpressionGenerator method visitSafeCastExpression.
@Override
public void visitSafeCastExpression(@NotNull GrSafeCastExpression typeCastExpression) {
final GrExpression operand = (GrExpression) PsiUtil.skipParenthesesIfSensibly(typeCastExpression.getOperand(), false);
final GrTypeElement typeElement = typeCastExpression.getCastTypeElement();
if (operand instanceof GrListOrMap && ((GrListOrMap) operand).isMap() && typeElement != null) {
AnonymousFromMapGenerator.writeAnonymousMap((GrListOrMap) operand, typeElement, builder, context);
return;
}
final PsiType type = typeElement.getType();
if (operand instanceof GrListOrMap && !((GrListOrMap) operand).isMap() && type instanceof PsiArrayType) {
builder.append("new ");
final GrExpression[] initializers = ((GrListOrMap) operand).getInitializers();
if (initializers.length == 0) {
TypeWriter.writeTypeForNew(builder, ((PsiArrayType) type).getComponentType(), typeCastExpression);
builder.append("[0]");
} else {
TypeWriter.writeTypeForNew(builder, type, typeCastExpression);
builder.append('{');
for (GrExpression initializer : initializers) {
initializer.accept(this);
builder.append(", ");
}
if (initializers.length > 0) {
builder.delete(builder.length() - 2, builder.length());
//builder.removeFromTheEnd(2);
}
builder.append('}');
}
return;
}
final GroovyResolveResult resolveResult = PsiImplUtil.extractUniqueResult(typeCastExpression.multiResolve(false));
final PsiElement resolved = resolveResult.getElement();
if (resolved instanceof PsiMethod) {
final GrExpression typeParam;
try {
typeParam = factory.createExpressionFromText(typeElement.getText(), typeCastExpression);
} catch (IncorrectOperationException e) {
generateCast(typeElement, operand);
return;
}
invokeMethodOn(((PsiMethod) resolved), operand, new GrExpression[] { typeParam }, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), typeCastExpression);
} else {
generateCast(typeElement, operand);
}
}
Aggregations