@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); } } } }