public static Set<PsiType> getDefaultExpectedTypes(@NotNull GrExpression element) { final LinkedHashSet<PsiType> result = new LinkedHashSet<PsiType>(); for (TypeConstraint constraint : calculateTypeConstraints(element)) { result.add(constraint.getDefaultType()); } return result; }
public void bePatternConfiguration(List<PsiClass> classes, PsiMethod method) { data.TEST_OBJECT = TestType.PATTERN.getType(); final String suffix; if (method != null) { data.METHOD_NAME = method.getName(); suffix = "," + data.METHOD_NAME; } else { suffix = ""; } LinkedHashSet<String> patterns = new LinkedHashSet<String>(); for (PsiClass pattern : classes) { patterns.add(JavaExecutionUtil.getRuntimeQualifiedName(pattern) + suffix); } data.setPatterns(patterns); final Module module = RunConfigurationProducer.getInstance(TestNGPatternConfigurationProducer.class) .findModule(this, getConfigurationModule().getModule(), patterns); if (module == null) { data.setScope(TestSearchScope.WHOLE_PROJECT); setModule(null); } else { setModule(module); } setGeneratedName(); }
@Override @NotNull public SuggestedNameInfo suggestUniqueVariableName( @NotNull final SuggestedNameInfo baseNameInfo, PsiElement place, boolean ignorePlaceName, boolean lookForward) { final String[] names = baseNameInfo.names; final LinkedHashSet<String> uniqueNames = new LinkedHashSet<String>(names.length); for (String name : names) { if (ignorePlaceName && place instanceof PsiNamedElement) { final String placeName = ((PsiNamedElement) place).getName(); if (Comparing.strEqual(placeName, name)) { uniqueNames.add(name); continue; } } uniqueNames.add(suggestUniqueVariableName(name, place, lookForward)); } return new SuggestedNameInfo(ArrayUtil.toStringArray(uniqueNames)) { @Override public void nameChosen(String name) { baseNameInfo.nameChosen(name); } }; }
/** * Begins the in-place refactoring operation. * * @return true if the in-place refactoring was successfully started, false if it failed to start * and a dialog should be shown instead. */ public boolean startInplaceIntroduceTemplate() { final boolean replaceAllOccurrences = isReplaceAllOccurrences(); final Ref<Boolean> result = new Ref<>(); CommandProcessor.getInstance() .executeCommand( myProject, () -> { final String[] names = suggestNames(replaceAllOccurrences, getLocalVariable()); final V variable = createFieldToStartTemplateOn(replaceAllOccurrences, names); boolean started = false; if (variable != null) { int caretOffset = getCaretOffset(); myEditor.getCaretModel().moveToOffset(caretOffset); myEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); final LinkedHashSet<String> nameSuggestions = new LinkedHashSet<>(); nameSuggestions.add(variable.getName()); nameSuggestions.addAll(Arrays.asList(names)); initOccurrencesMarkers(); setElementToRename(variable); updateTitle(getVariable()); started = super.performInplaceRefactoring(nameSuggestions); if (started) { onRenameTemplateStarted(); myDocumentAdapter = new DocumentAdapter() { @Override public void documentChanged(DocumentEvent e) { if (myPreview == null) return; final TemplateState templateState = TemplateManagerImpl.getTemplateState(myEditor); if (templateState != null) { final TextResult value = templateState.getVariableValue( InplaceRefactoring.PRIMARY_VARIABLE_NAME); if (value != null) { updateTitle(getVariable(), value.getText()); } } } }; myEditor.getDocument().addDocumentListener(myDocumentAdapter); updateTitle(getVariable()); if (TemplateManagerImpl.getTemplateState(myEditor) != null) { myEditor.putUserData(ACTIVE_INTRODUCE, this); } } } result.set(started); if (!started) { finish(true); } }, getCommandName(), getCommandName()); return result.get(); }
@Override public PsiType[] guessTypeToCast( PsiExpression expr) { // TODO : make better guess based on control flow LinkedHashSet<PsiType> types = new LinkedHashSet<>(); ContainerUtil.addIfNotNull(types, getControlFlowExpressionType(expr)); addExprTypesWhenContainerElement(types, expr); addExprTypesByDerivedClasses(types, expr); return types.toArray(PsiType.createArray(types.size())); }
public void moveFieldInitializations() throws IncorrectOperationException { LOG.assertTrue(myMembersAfterMove != null); final LinkedHashSet<PsiField> movedFields = new LinkedHashSet<>(); for (PsiMember member : myMembersAfterMove) { if (member instanceof PsiField) { movedFields.add((PsiField) member); } } if (movedFields.isEmpty()) return; PullUpHelper<MemberInfo> processor = getProcessor(myTargetSuperClass); LOG.assertTrue(processor != null, myTargetSuperClass); processor.moveFieldInitializations(movedFields); }
@Override public void readExternal(Element element) throws InvalidDataException { PathMacroManager.getInstance(getProject()).expandPaths(element); super.readExternal(element); JavaRunConfigurationExtensionManager.getInstance().readExternal(this, element); readModule(element); DefaultJDOMExternalizer.readExternal(this, element); DefaultJDOMExternalizer.readExternal(getPersistantData(), element); EnvironmentVariablesComponent.readExternal(element, getPersistantData().getEnvs()); Map<String, String> properties = getPersistantData().TEST_PROPERTIES; properties.clear(); Element propertiesElement = element.getChild("properties"); if (propertiesElement != null) { List<Element> children = propertiesElement.getChildren("property"); for (Element property : children) { properties.put(property.getAttributeValue("name"), property.getAttributeValue("value")); } } List<String> listeners = getPersistantData().TEST_LISTENERS; listeners.clear(); Element listenersElement = element.getChild("listeners"); if (listenersElement != null) { List<Element> children = listenersElement.getChildren("listener"); for (Element listenerClassName : children) { listeners.add(listenerClassName.getAttributeValue("class")); } } final Element patternsElement = element.getChild(PATTERNS_EL_NAME); if (patternsElement != null) { final LinkedHashSet<String> tests = new LinkedHashSet<String>(); for (Object o : patternsElement.getChildren(PATTERN_EL_NAME)) { Element patternElement = (Element) o; tests.add(patternElement.getAttributeValue(TEST_CLASS_ATT_NAME)); } getPersistantData().setPatterns(tests); } }
private void addExprTypesByDerivedClasses(LinkedHashSet<PsiType> set, PsiExpression expr) { PsiType type = expr.getType(); if (!(type instanceof PsiClassType)) return; PsiClass refClass = PsiUtil.resolveClassInType(type); if (refClass == null) return; PsiManager manager = PsiManager.getInstance(myProject); PsiElementProcessor.CollectElementsWithLimit<PsiClass> processor = new PsiElementProcessor.CollectElementsWithLimit<>(5); ClassInheritorsSearch.search(refClass).forEach(new PsiElementProcessorAdapter<>(processor)); if (processor.isOverflow()) return; for (PsiClass derivedClass : processor.getCollection()) { if (derivedClass instanceof PsiAnonymousClass) continue; PsiType derivedType = JavaPsiFacade.getInstance(manager.getProject()) .getElementFactory() .createType(derivedClass); set.add(derivedType); } }
private void addExprTypesWhenContainerElement(LinkedHashSet<PsiType> set, PsiExpression expr) { if (expr instanceof PsiMethodCallExpression) { PsiMethodCallExpression callExpr = (PsiMethodCallExpression) expr; PsiReferenceExpression methodExpr = callExpr.getMethodExpression(); String methodName = methodExpr.getReferenceName(); MethodPattern pattern = myMethodPatternMap.findPattern( methodName, callExpr.getArgumentList().getExpressions().length); if (pattern != null && pattern.parameterIndex < 0 /* return value */) { PsiExpression qualifier = methodExpr.getQualifierExpression(); if (qualifier != null) { PsiType[] types = guessContainerElementType(qualifier, null); for (PsiType type : types) { if (type instanceof PsiClassType) { if (((PsiClassType) type).resolve() instanceof PsiAnonymousClass) continue; } set.add(type); } } } } }
@Override public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException { if (!FileModificationService.getInstance().prepareFileForWrite(file)) return; PsiMethod[] constructors = myClass.getConstructors(); if (constructors.length == 0) { final AddDefaultConstructorFix defaultConstructorFix = new AddDefaultConstructorFix(myClass); ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { defaultConstructorFix.invoke(project, editor, file); } }); constructors = myClass.getConstructors(); } Arrays.sort( constructors, new Comparator<PsiMethod>() { @Override public int compare(PsiMethod c1, PsiMethod c2) { final PsiMethod cc1 = RefactoringUtil.getChainedConstructor(c1); final PsiMethod cc2 = RefactoringUtil.getChainedConstructor(c2); if (cc1 == c2) return 1; if (cc2 == c1) return -1; if (cc1 == null) { return cc2 == null ? 0 : compare(c1, cc2); } else { return cc2 == null ? compare(cc1, c2) : compare(cc1, cc2); } } }); final ArrayList<PsiMethod> constrs = filterConstructorsIfFieldAlreadyAssigned(constructors, getField()); if (constrs.size() > 1) { final PsiMethodMember[] members = new PsiMethodMember[constrs.size()]; int i = 0; for (PsiMethod constructor : constrs) { members[i++] = new PsiMethodMember(constructor); } final List<PsiMethodMember> elements; if (ApplicationManager.getApplication().isUnitTestMode()) { elements = Arrays.asList(members); } else { final MemberChooser<PsiMethodMember> chooser = new MemberChooser<PsiMethodMember>(members, false, true, project); chooser.setTitle("Choose constructors to add parameter to"); chooser.show(); elements = chooser.getSelectedElements(); if (elements == null) return; } for (PsiMethodMember member : elements) { if (!addParameterToConstructor( project, file, editor, member.getElement(), new PsiField[] {getField()})) break; } } else if (!constrs.isEmpty()) { final Collection<SmartPsiElementPointer<PsiField>> fieldsToFix = getFieldsToFix(); try { final PsiMethod constructor = constrs.get(0); final LinkedHashSet<PsiField> fields = new LinkedHashSet<PsiField>(); getFieldsToFix().add(myField); for (SmartPsiElementPointer<PsiField> elementPointer : fieldsToFix) { final PsiField field = elementPointer.getElement(); if (field != null && isAvailable(field) && filterConstructorsIfFieldAlreadyAssigned(new PsiMethod[] {constructor}, field) .contains(constructor)) { fields.add(field); } } if (constrs.size() == constructors.length && fields.size() > 1 && !ApplicationManager.getApplication().isUnitTestMode()) { PsiFieldMember[] members = new PsiFieldMember[fields.size()]; int i = 0; for (PsiField field : fields) { members[i++] = new PsiFieldMember(field); } MemberChooser<PsiElementClassMember> chooser = new MemberChooser<PsiElementClassMember>(members, false, true, project); chooser.setTitle("Choose Fields to Generate Constructor Parameters for"); chooser.show(); if (chooser.getExitCode() != DialogWrapper.OK_EXIT_CODE) return; final List<PsiElementClassMember> selectedElements = chooser.getSelectedElements(); if (selectedElements == null) return; fields.clear(); for (PsiElementClassMember member : selectedElements) { fields.add((PsiField) member.getElement()); } } addParameterToConstructor( project, file, editor, constructor, constrs.size() == constructors.length ? fields.toArray(new PsiField[fields.size()]) : new PsiField[] {getField()}); } finally { fieldsToFix.clear(); } } }