private static boolean doCreate( Project project, Editor editor, PsiParameter[] parameters, SmartPsiElementPointer constructorPointer, ParameterInfoImpl[] parameterInfos, HashMap<PsiField, String> fields) { PsiMethod constructor = (PsiMethod) constructorPointer.getElement(); assert constructor != null; PsiParameter[] newParameters = constructor.getParameterList().getParameters(); if (newParameters == parameters) return false; // user must have canceled dialog boolean created = false; // do not introduce assignment in chanined constructor if (JavaHighlightUtil.getChainedConstructors(constructor) == null) { for (PsiField field : fields.keySet()) { final String defaultParamName = fields.get(field); PsiParameter parameter = findParamByName(defaultParamName, field.getType(), newParameters, parameterInfos); if (parameter == null) { continue; } notNull(project, field, parameter); AssignFieldFromParameterAction.addFieldAssignmentStatement( project, field, parameter, editor); created = true; } } return created; }
/** array('foo' => 'bar', 'foo1' => 'bar', 1 => 'foo') */ public static HashMap<String, String> getArrayKeyValueMap( @NotNull ArrayCreationExpression arrayCreationExpression) { HashMap<String, String> keys = new HashMap<String, String>(); for (ArrayHashElement arrayHashElement : arrayCreationExpression.getHashElements()) { PhpPsiElement child = arrayHashElement.getKey(); if (child != null && ((child instanceof StringLiteralExpression) || PhpPatterns.psiElement(PhpElementTypes.NUMBER).accepts(child))) { String key; if (child instanceof StringLiteralExpression) { key = ((StringLiteralExpression) child).getContents(); } else { key = child.getText(); } if (StringUtils.isBlank(key)) { continue; } String value = null; if (arrayHashElement.getValue() instanceof StringLiteralExpression) { value = ((StringLiteralExpression) arrayHashElement.getValue()).getContents(); } keys.put(key, value); } } return keys; }
private static boolean isUnique( PsiParameter[] params, String newName, HashMap<PsiField, String> usedNames) { if (usedNames.containsValue(newName)) return false; for (PsiParameter parameter : params) { if (Comparing.strEqual(parameter.getName(), newName)) { return false; } } return true; }
private static boolean addParameterToConstructor( final Project project, final PsiFile file, final Editor editor, final PsiMethod constructor, final PsiField[] fields) throws IncorrectOperationException { final PsiParameterList parameterList = constructor.getParameterList(); final PsiParameter[] parameters = parameterList.getParameters(); ParameterInfoImpl[] newParamInfos = new ParameterInfoImpl[parameters.length + fields.length]; final List<PsiVariable> params = new ArrayList<PsiVariable>(Arrays.asList(parameters)); Collections.addAll(params, fields); Collections.sort(params, new FieldParameterComparator(parameterList)); int i = 0; final HashMap<PsiField, String> usedFields = new HashMap<PsiField, String>(); for (PsiVariable param : params) { final PsiType paramType = param.getType(); if (param instanceof PsiParameter) { newParamInfos[i++] = new ParameterInfoImpl( parameterList.getParameterIndex((PsiParameter) param), param.getName(), paramType, param.getName()); } else { final String uniqueParameterName = getUniqueParameterName(parameters, param, usedFields); usedFields.put((PsiField) param, uniqueParameterName); newParamInfos[i++] = new ParameterInfoImpl(-1, uniqueParameterName, paramType, uniqueParameterName); } } final SmartPointerManager manager = SmartPointerManager.getInstance(project); final SmartPsiElementPointer constructorPointer = manager.createSmartPsiElementPointer(constructor); final PsiMethod fromText = JavaPsiFacade.getElementFactory(project) .createMethodFromText(createDummyMethod(constructor, newParamInfos), constructor); final PsiClass containingClass = constructor.getContainingClass(); if (containingClass == null) return false; final int minUsagesNumber = containingClass.findMethodsBySignature(fromText, false).length > 0 ? 0 : 1; final List<ParameterInfoImpl> parameterInfos = ChangeMethodSignatureFromUsageFix.performChange( project, editor, file, constructor, minUsagesNumber, newParamInfos, true, true); final ParameterInfoImpl[] resultParams = parameterInfos != null ? parameterInfos.toArray(new ParameterInfoImpl[parameterInfos.size()]) : newParamInfos; return ApplicationManager.getApplication() .runWriteAction( new Computable<Boolean>() { @Override public Boolean compute() { return doCreate( project, editor, parameters, constructorPointer, resultParams, usedFields); } }); }
@Nullable public static PsiElement doCopyClasses( final Map<PsiFile, PsiClass[]> fileToClasses, @Nullable HashMap<PsiFile, String> map, final String copyClassName, final PsiDirectory targetDirectory, final Project project) throws IncorrectOperationException { PsiElement newElement = null; final Map<PsiClass, PsiElement> oldToNewMap = new HashMap<PsiClass, PsiElement>(); for (final PsiClass[] psiClasses : fileToClasses.values()) { if (psiClasses != null) { for (PsiClass aClass : psiClasses) { if (aClass instanceof SyntheticElement) { continue; } oldToNewMap.put(aClass, null); } } } final List<PsiFile> createdFiles = new ArrayList<PsiFile>(fileToClasses.size()); int[] choice = fileToClasses.size() > 1 ? new int[] {-1} : null; List<PsiFile> files = new ArrayList<PsiFile>(); for (final Map.Entry<PsiFile, PsiClass[]> entry : fileToClasses.entrySet()) { final PsiFile psiFile = entry.getKey(); final PsiClass[] sources = entry.getValue(); if (psiFile instanceof PsiClassOwner && sources != null) { final PsiFile createdFile = copy( psiFile, targetDirectory, copyClassName, map == null ? null : map.get(psiFile), choice); if (createdFile == null) return null; for (final PsiClass destination : ((PsiClassOwner) createdFile).getClasses()) { if (destination instanceof SyntheticElement) { continue; } PsiClass source = findByName(sources, destination.getName()); if (source != null) { final PsiClass copy = copy(source, copyClassName); newElement = destination.replace(copy); oldToNewMap.put(source, newElement); } else { destination.delete(); } } createdFiles.add(createdFile); } else { files.add(psiFile); } } for (PsiFile file : files) { try { PsiDirectory finalTarget = targetDirectory; final String relativePath = map != null ? map.get(file) : null; if (relativePath != null && !relativePath.isEmpty()) { finalTarget = buildRelativeDir(targetDirectory, relativePath).findOrCreateTargetDirectory(); } final PsiFile fileCopy = CopyFilesOrDirectoriesHandler.copyToDirectory( file, getNewFileName(file, copyClassName), finalTarget, choice); if (fileCopy != null) { createdFiles.add(fileCopy); } } catch (IOException e) { throw new IncorrectOperationException(e.getMessage()); } } final Set<PsiElement> rebindExpressions = new HashSet<PsiElement>(); for (PsiElement element : oldToNewMap.values()) { if (element == null) { LOG.error(oldToNewMap.keySet()); continue; } decodeRefs(element, oldToNewMap, rebindExpressions); } final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project); for (PsiFile psiFile : createdFiles) { if (psiFile instanceof PsiJavaFile) { codeStyleManager.removeRedundantImports((PsiJavaFile) psiFile); } } for (PsiElement expression : rebindExpressions) { codeStyleManager.shortenClassReferences(expression); } new OptimizeImportsProcessor( project, createdFiles.toArray(new PsiFile[createdFiles.size()]), null) .run(); return newElement != null ? newElement : createdFiles.size() > 0 ? createdFiles.get(0) : null; }