use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.
the class IntroduceParameterObjectDialog method createClassDescriptor.
@Override
protected JavaIntroduceParameterObjectClassDescriptor createClassDescriptor() {
final boolean useExistingClass = useExistingClass();
final String className;
final String packageName;
final boolean createInnerClass = myCreateInnerClassRadioButton.isSelected();
if (createInnerClass) {
className = getInnerClassName();
packageName = "";
} else if (useExistingClass) {
final String existingClassName = getExistingClassName();
className = StringUtil.getShortName(existingClassName);
packageName = StringUtil.getPackageName(existingClassName);
} else {
packageName = getPackageName();
className = getClassName();
}
final String newVisibility = myEscalateVisibilityCheckBox.isEnabled() && myEscalateVisibilityCheckBox.isSelected() ? VisibilityUtil.ESCALATE_VISIBILITY : null;
final MoveDestination moveDestination = ((DestinationFolderComboBox) myDestinationCb).selectDirectory(new PackageWrapper(PsiManager.getInstance(myProject), packageName), false);
final PsiParameterList parameterList = mySourceMethod.getParameterList();
final List<ParameterInfoImpl> parameters = new ArrayList<>();
for (VariableData data : myParameterTablePanel.getVariableData()) {
if (data.passAsParameter) {
parameters.add(new ParameterInfoImpl(parameterList.getParameterIndex((PsiParameter) data.variable), data.name, data.type));
}
}
final ParameterInfoImpl[] infos = parameters.toArray(new ParameterInfoImpl[parameters.size()]);
return new JavaIntroduceParameterObjectClassDescriptor(className, packageName, moveDestination, useExistingClass, createInnerClass, newVisibility, infos, mySourceMethod, myGenerateAccessorsCheckBox.isSelected());
}
use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.
the class JavaIntroduceParameterObjectClassDescriptor method findCompatibleConstructor.
@Nullable
private PsiMethod findCompatibleConstructor(@NotNull PsiClass aClass) {
ParameterInfoImpl[] paramsToMerge = getParamsToMerge();
if (paramsToMerge.length == 1) {
final ParameterInfoImpl parameterInfo = paramsToMerge[0];
final PsiType paramType = parameterInfo.getTypeWrapper().getType(aClass);
if (TypeConversionUtil.isPrimitiveWrapper(aClass.getQualifiedName())) {
ParameterBean bean = new ParameterBean();
bean.setField(aClass.findFieldByName("value", false));
bean.setGetter(paramType.getCanonicalText() + "Value");
myExistingClassProperties.put(parameterInfo, bean);
for (PsiMethod constructor : aClass.getConstructors()) {
if (isConstructorCompatible(constructor, new ParameterInfoImpl[] { parameterInfo }, aClass))
return constructor;
}
}
}
final PsiMethod[] constructors = aClass.getConstructors();
PsiMethod compatibleConstructor = null;
for (PsiMethod constructor : constructors) {
if (isConstructorCompatible(constructor, paramsToMerge, aClass)) {
compatibleConstructor = constructor;
break;
}
}
PsiField[] fields = aClass.getFields();
if (compatibleConstructor == null && !areTypesCompatible(getParamsToMerge(), fields, aClass)) {
return null;
}
final PsiVariable[] constructorParams = compatibleConstructor != null ? compatibleConstructor.getParameterList().getParameters() : fields;
for (int i = 0; i < getParamsToMerge().length; i++) {
final int oldIndex = getParamsToMerge()[i].getOldIndex();
final ParameterInfoImpl methodParam = getParameterInfo(oldIndex);
final ParameterBean bean = new ParameterBean();
myExistingClassProperties.put(methodParam, bean);
final PsiVariable var = constructorParams[i];
final PsiField field = var instanceof PsiParameter ? findFieldAssigned((PsiParameter) var, compatibleConstructor) : (PsiField) var;
if (field == null) {
return null;
}
bean.setField(field);
final PsiMethod getterForField = PropertyUtil.findGetterForField(field);
if (getterForField != null) {
bean.setGetter(getterForField.getName());
}
final PsiMethod setterForField = PropertyUtil.findSetterForField(field);
if (setterForField != null) {
bean.setSetter(setterForField.getName());
}
}
return compatibleConstructor;
}
use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.
the class JavaIntroduceParameterObjectClassDescriptor method createClass.
@Override
public PsiClass createClass(PsiMethod method, ReadWriteAccessDetector.Access[] accessors) {
if (isUseExistingClass()) {
return getExistingClass();
}
final ParameterObjectBuilder beanClassBuilder = new ParameterObjectBuilder();
beanClassBuilder.setVisibility(isCreateInnerClass() ? PsiModifier.PRIVATE : PsiModifier.PUBLIC);
beanClassBuilder.setProject(method.getProject());
beanClassBuilder.setTypeArguments(getTypeParameters());
beanClassBuilder.setClassName(getClassName());
beanClassBuilder.setPackageName(getPackageName());
PsiParameter[] parameters = method.getParameterList().getParameters();
final ParameterInfoImpl[] parameterInfos = getParamsToMerge();
for (int i = 0; i < parameterInfos.length; i++) {
PsiParameter parameter = parameters[parameterInfos[i].getOldIndex()];
final boolean setterRequired = accessors[i] == ReadWriteAccessDetector.Access.Write;
final String newName = parameterInfos[i].getName();
beanClassBuilder.addField(parameter, newName, parameterInfos[i].getTypeWrapper().getType(method), setterRequired);
}
final String classString = beanClassBuilder.buildBeanClass();
try {
final PsiFileFactory factory = PsiFileFactory.getInstance(method.getProject());
final PsiJavaFile newFile = (PsiJavaFile) factory.createFileFromText(getClassName() + ".java", JavaFileType.INSTANCE, classString);
if (isCreateInnerClass()) {
final PsiClass containingClass = method.getContainingClass();
final PsiClass[] classes = newFile.getClasses();
assert classes.length > 0 : classString;
final PsiClass innerClass = (PsiClass) containingClass.add(classes[0]);
PsiUtil.setModifierProperty(innerClass, PsiModifier.STATIC, true);
return (PsiClass) JavaCodeStyleManager.getInstance(newFile.getProject()).shortenClassReferences(innerClass);
} else {
final PsiFile containingFile = method.getContainingFile();
final PsiDirectory containingDirectory = containingFile.getContainingDirectory();
final PsiDirectory directory;
final MoveDestination moveDestination = getMoveDestination();
if (moveDestination != null) {
directory = moveDestination.getTargetDirectory(containingDirectory);
} else {
final Module module = ModuleUtilCore.findModuleForPsiElement(containingFile);
directory = PackageUtil.findOrCreateDirectoryForPackage(module, getPackageName(), containingDirectory, true, true);
}
if (directory != null) {
PsiFile file = directory.findFile(newFile.getName());
if (file == null) {
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(method.getManager().getProject());
final PsiElement shortenedFile = JavaCodeStyleManager.getInstance(newFile.getProject()).shortenClassReferences(newFile);
final PsiElement reformattedFile = codeStyleManager.reformat(shortenedFile);
file = (PsiFile) directory.add(reformattedFile);
}
return ((PsiJavaFile) file).getClasses()[0];
}
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
return null;
}
use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.
the class VariableTypeFix method changeMethodSignatureIfNeeded.
private boolean changeMethodSignatureIfNeeded(PsiVariable myVariable) {
if (myVariable instanceof PsiParameter) {
final PsiElement scope = ((PsiParameter) myVariable).getDeclarationScope();
if (scope instanceof PsiMethod) {
final PsiMethod method = (PsiMethod) scope;
final PsiMethod psiMethod = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringBundle.message("to.refactor"));
if (psiMethod == null)
return true;
final int parameterIndex = method.getParameterList().getParameterIndex((PsiParameter) myVariable);
if (!FileModificationService.getInstance().prepareFileForWrite(psiMethod.getContainingFile()))
return true;
final ArrayList<ParameterInfoImpl> infos = new ArrayList<>();
int i = 0;
for (PsiParameter parameter : psiMethod.getParameterList().getParameters()) {
final boolean changeType = i == parameterIndex;
infos.add(new ParameterInfoImpl(i++, parameter.getName(), changeType ? getReturnType() : parameter.getType()));
}
if (!ApplicationManager.getApplication().isUnitTestMode()) {
final JavaChangeSignatureDialog dialog = new JavaChangeSignatureDialog(psiMethod.getProject(), psiMethod, false, myVariable);
dialog.setParameterInfos(infos);
dialog.show();
} else {
ChangeSignatureProcessor processor = new ChangeSignatureProcessor(psiMethod.getProject(), psiMethod, false, null, psiMethod.getName(), psiMethod.getReturnType(), infos.toArray(new ParameterInfoImpl[infos.size()]));
processor.run();
}
return true;
}
}
return false;
}
use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.
the class MatchUtil method changeSignature.
public static void changeSignature(@NotNull Match match, @NotNull PsiMethod psiMethod) {
final PsiType expressionType = match.getChangedReturnType(psiMethod);
if (expressionType == null && match.myChangedParams.isEmpty())
return;
final List<ParameterInfoImpl> newParameters = patchParams(match.myChangedParams, psiMethod);
final ChangeSignatureProcessor csp = new ChangeSignatureProcessor(psiMethod.getProject(), psiMethod, false, null, psiMethod.getName(), expressionType != null ? expressionType : psiMethod.getReturnType(), newParameters.toArray(new ParameterInfoImpl[newParameters.size()]));
csp.run();
}
Aggregations