protected String getCommandName() { final PsiClass containingClass = method.getContainingClass(); return RefactorJBundle.message( "introduced.parameter.class.command.name", className, containingClass.getName(), method.getName()); }
@Nullable private static PsiMethod existingClassIsCompatible(PsiClass aClass, List<ParameterChunk> params) { if (params.size() == 1) { final ParameterChunk parameterChunk = params.get(0); final PsiType paramType = parameterChunk.parameter.type; if (TypeConversionUtil.isPrimitiveWrapper(aClass.getQualifiedName())) { parameterChunk.setField(aClass.findFieldByName("value", false)); parameterChunk.setGetter(paramType.getCanonicalText() + "Value"); for (PsiMethod constructor : aClass.getConstructors()) { if (constructorIsCompatible(constructor, params)) return constructor; } } } final PsiMethod[] constructors = aClass.getConstructors(); PsiMethod compatibleConstructor = null; for (PsiMethod constructor : constructors) { if (constructorIsCompatible(constructor, params)) { compatibleConstructor = constructor; break; } } if (compatibleConstructor == null) { return null; } final PsiParameterList parameterList = compatibleConstructor.getParameterList(); final PsiParameter[] constructorParams = parameterList.getParameters(); for (int i = 0; i < constructorParams.length; i++) { final PsiParameter param = constructorParams[i]; final ParameterChunk parameterChunk = params.get(i); final PsiField field = findFieldAssigned(param, compatibleConstructor); if (field == null) { return null; } parameterChunk.setField(field); final PsiMethod getterForField = PropertyUtil.findGetterForField(field); if (getterForField != null) { parameterChunk.setGetter(getterForField.getName()); } final PsiMethod setterForField = PropertyUtil.findSetterForField(field); if (setterForField != null) { parameterChunk.setSetter(setterForField.getName()); } } return compatibleConstructor; }
protected void performRefactoring(@NotNull UsageInfo[] usageInfos) { final PsiClass psiClass = buildClass(); if (psiClass != null) { fixJavadocForConstructor(psiClass); super.performRefactoring(usageInfos); if (!myUseExistingClass) { for (PsiReference reference : ReferencesSearch.search(method)) { final PsiElement place = reference.getElement(); VisibilityUtil.escalateVisibility(psiClass, place); for (PsiMethod constructor : psiClass.getConstructors()) { VisibilityUtil.escalateVisibility(constructor, place); } } } } }
private void fixJavadocForConstructor(PsiClass psiClass) { final PsiDocComment docComment = method.getDocComment(); if (docComment != null) { final List<PsiDocTag> mergedTags = new ArrayList<PsiDocTag>(); final PsiDocTag[] paramTags = docComment.findTagsByName("param"); for (PsiDocTag paramTag : paramTags) { final PsiElement[] dataElements = paramTag.getDataElements(); if (dataElements.length > 0) { if (dataElements[0] instanceof PsiDocParamRef) { final PsiReference reference = dataElements[0].getReference(); if (reference != null) { final PsiElement resolve = reference.resolve(); if (resolve instanceof PsiParameter) { final int parameterIndex = method.getParameterList().getParameterIndex((PsiParameter) resolve); if (ArrayUtil.find(paramsToMerge, parameterIndex) < 0) continue; } } } mergedTags.add((PsiDocTag) paramTag.copy()); } } PsiMethod compatibleParamObjectConstructor = null; if (myExistingClassCompatibleConstructor != null && myExistingClassCompatibleConstructor.getDocComment() == null) { compatibleParamObjectConstructor = myExistingClassCompatibleConstructor; } else if (!myUseExistingClass) { compatibleParamObjectConstructor = psiClass.getConstructors()[0]; } if (compatibleParamObjectConstructor != null) { PsiDocComment psiDocComment = JavaPsiFacade.getElementFactory(myProject).createDocCommentFromText("/**\n*/"); psiDocComment = (PsiDocComment) compatibleParamObjectConstructor.addBefore( psiDocComment, compatibleParamObjectConstructor.getFirstChild()); for (PsiDocTag tag : mergedTags) { psiDocComment.add(tag); } } } }
private PsiClass buildClass() { if (existingClass != null) { return existingClass; } final ParameterObjectBuilder beanClassBuilder = new ParameterObjectBuilder(); beanClassBuilder.setVisibility(myCreateInnerClass ? PsiModifier.PRIVATE : PsiModifier.PUBLIC); beanClassBuilder.setProject(myProject); beanClassBuilder.setTypeArguments(typeParams); beanClassBuilder.setClassName(className); beanClassBuilder.setPackageName(packageName); for (ParameterChunk parameterChunk : parameters) { final VariableData parameter = parameterChunk.parameter; final boolean setterRequired = paramsNeedingSetters.contains(parameter.variable); beanClassBuilder.addField( (PsiParameter) parameter.variable, parameter.name, parameter.type, setterRequired); } final String classString = beanClassBuilder.buildBeanClass(); try { final PsiFileFactory factory = PsiFileFactory.getInstance(method.getProject()); final PsiJavaFile newFile = (PsiJavaFile) factory.createFileFromText(className + ".java", JavaFileType.INSTANCE, classString); if (myCreateInnerClass) { 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; if (myMoveDestination != null) { directory = myMoveDestination.getTargetDirectory(containingDirectory); } else { final Module module = ModuleUtil.findModuleForPsiElement(containingFile); directory = PackageUtil.findOrCreateDirectoryForPackage( module, packageName, containingDirectory, true, true); } if (directory != null) { final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(method.getManager().getProject()); final PsiElement shortenedFile = JavaCodeStyleManager.getInstance(newFile.getProject()) .shortenClassReferences(newFile); final PsiElement reformattedFile = codeStyleManager.reformat(shortenedFile); return ((PsiJavaFile) directory.add(reformattedFile)).getClasses()[0]; } } } catch (IncorrectOperationException e) { logger.info(e); } return null; }