use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.
the class CreateParameterFromUsageFix method showDialog.
private static void showDialog(final PsiMethod method, final GrReferenceExpression ref, final Project project) {
final String name = ref.getReferenceName();
final List<PsiType> types = GroovyExpectedTypesProvider.getDefaultExpectedTypes(ref);
PsiType unboxed = types.isEmpty() ? null : TypesUtil.unboxPrimitiveTypeWrapper(types.get(0));
@NotNull final PsiType type = unboxed != null ? unboxed : PsiType.getJavaLangObject(ref.getManager(), ref.getResolveScope());
if (method instanceof GrMethod) {
GrMethodDescriptor descriptor = new GrMethodDescriptor((GrMethod) method);
GrChangeSignatureDialog dialog = new GrChangeSignatureDialog(project, descriptor, true, ref);
List<GrParameterInfo> parameters = dialog.getParameters();
parameters.add(createParameterInfo(name, type));
dialog.setParameterInfos(parameters);
dialog.show();
} else if (method != null) {
JavaChangeSignatureDialog dialog = new JavaChangeSignatureDialog(project, method, false, ref);
final List<ParameterInfoImpl> parameterInfos = new ArrayList<>(Arrays.asList(ParameterInfoImpl.fromMethod(method)));
ParameterInfoImpl parameterInfo = new ParameterInfoImpl(-1, name, type, PsiTypesUtil.getDefaultValueOfType(type), false);
if (!method.isVarArgs()) {
parameterInfos.add(parameterInfo);
} else {
parameterInfos.add(parameterInfos.size() - 1, parameterInfo);
}
dialog.setParameterInfos(parameterInfos);
dialog.show();
}
}
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 ChangeSignatureBaseTest method doTest.
protected void doTest(@Nullable String returnType, @Nullable final String[] parameters, @Nullable final String[] exceptions, boolean delegate) {
GenParams genParams = parameters == null ? new SimpleParameterGen() : method -> {
ParameterInfoImpl[] parameterInfos = new ParameterInfoImpl[parameters.length];
for (int i = 0; i < parameters.length; i++) {
PsiType type = myFactory.createTypeFromText(parameters[i], method);
parameterInfos[i] = new ParameterInfoImpl(-1, "p" + (i + 1), type);
}
return parameterInfos;
};
GenExceptions genExceptions = exceptions == null ? new SimpleExceptionsGen() : method -> {
ThrownExceptionInfo[] exceptionInfos = new ThrownExceptionInfo[exceptions.length];
for (int i = 0; i < exceptions.length; i++) {
PsiType type = myFactory.createTypeFromText(exceptions[i], method);
exceptionInfos[i] = new JavaThrownExceptionInfo(-1, (PsiClassType) type);
}
return exceptionInfos;
};
doTest(null, null, returnType, genParams, genExceptions, delegate);
}
Aggregations