use of com.intellij.refactoring.util.VariableData in project intellij-community by JetBrains.
the class ExtractMethodSignatureSuggester method inlineSameArguments.
private void inlineSameArguments(PsiMethod method, List<PsiExpression> copies, InputVariables variables, List<Match> duplicates) {
final List<VariableData> variableDatum = variables.getInputVariables();
final Map<PsiVariable, PsiExpression> toInline = new HashMap<>();
final int strongParamsCound = method.getParameterList().getParametersCount();
for (int i = strongParamsCound; i < variableDatum.size(); i++) {
VariableData variableData = variableDatum.get(i);
final THashSet<PsiExpression> map = new THashSet<>(ourEquivalenceStrategy);
if (!collectParamValues(duplicates, variableData, map)) {
continue;
}
final PsiExpression currentExpression = copies.get(i - strongParamsCound);
map.add(currentExpression);
if (map.size() == 1) {
toInline.put(variableData.variable, currentExpression);
}
}
if (!toInline.isEmpty()) {
copies.removeAll(toInline.values());
inlineArgumentsInMethodBody(toInline);
removeRedundantParametersFromMethodSignature(toInline);
}
removeUnusedStongParams(strongParamsCound);
}
use of com.intellij.refactoring.util.VariableData in project intellij-community by JetBrains.
the class InputVariables method wrapInputVariables.
public ArrayList<VariableData> wrapInputVariables(final List<? extends PsiVariable> inputVariables) {
UniqueNameGenerator nameGenerator = new UniqueNameGenerator();
final ArrayList<VariableData> inputData = new ArrayList<>(inputVariables.size());
for (PsiVariable var : inputVariables) {
final String defaultName = getParameterName(var);
String name = nameGenerator.generateUniqueName(defaultName);
PsiType type = GenericsUtil.getVariableTypeByExpressionType(var.getType());
if (type instanceof PsiEllipsisType) {
type = ((PsiEllipsisType) type).toArrayType();
}
final Map<PsiCodeBlock, PsiType> casts = new HashMap<>();
for (PsiReference reference : ReferencesSearch.search(var, myScope)) {
final PsiElement element = reference.getElement();
final PsiElement parent = element.getParent();
final PsiCodeBlock block = PsiTreeUtil.getParentOfType(parent, PsiCodeBlock.class);
if (parent instanceof PsiTypeCastExpression) {
final PsiType currentType = casts.get(block);
final PsiType castType = ((PsiTypeCastExpression) parent).getType();
casts.put(block, casts.containsKey(block) && currentType == null ? null : getBroaderType(currentType, castType));
} else {
casts.put(block, null);
}
}
if (!casts.containsValue(null)) {
PsiType currentType = null;
for (PsiType psiType : casts.values()) {
currentType = getBroaderType(currentType, psiType);
if (currentType == null) {
break;
}
}
if (currentType != null) {
currentType = checkTopLevelInstanceOf(currentType);
if (currentType != null) {
type = currentType;
}
}
}
VariableData data = new VariableData(var, type);
data.name = name;
data.passAsParameter = true;
inputData.add(data);
if (myFoldingAvailable)
myFolding.isParameterFoldable(data, myScope, inputVariables, nameGenerator, defaultName);
}
if (myFoldingAvailable) {
final Set<VariableData> toDelete = new HashSet<>();
for (int i = inputData.size() - 1; i >= 0; i--) {
final VariableData data = inputData.get(i);
if (myFolding.isParameterSafeToDelete(data, myScope)) {
toDelete.add(data);
}
}
inputData.removeAll(toDelete);
}
if (myPassFields && myUsedInstanceFields != null) {
for (PsiField var : myUsedInstanceFields) {
final VariableData data = new VariableData(var, var.getType());
data.name = nameGenerator.generateUniqueName(getParameterName(var));
data.passAsParameter = true;
inputData.add(data);
}
}
return inputData;
}
use of com.intellij.refactoring.util.VariableData in project intellij-community by JetBrains.
the class InputVariables method annotateWithParameter.
public void annotateWithParameter(PsiJavaCodeReferenceElement reference) {
for (VariableData data : myInputVariables) {
final PsiElement element = reference.resolve();
if (data.variable.equals(element)) {
PsiType type = data.variable.getType();
final PsiMethodCallExpression methodCallExpression = PsiTreeUtil.getParentOfType(reference, PsiMethodCallExpression.class);
if (methodCallExpression != null) {
int idx = ArrayUtil.find(methodCallExpression.getArgumentList().getExpressions(), reference);
if (idx > -1) {
final PsiMethod psiMethod = methodCallExpression.resolveMethod();
if (psiMethod != null) {
final PsiParameter[] parameters = psiMethod.getParameterList().getParameters();
if (idx >= parameters.length) {
//vararg parameter
idx = parameters.length - 1;
if (idx >= 0) {
//incomplete code
type = parameters[idx].getType();
}
}
if (type instanceof PsiEllipsisType) {
type = ((PsiEllipsisType) type).getComponentType();
}
}
}
}
if (!myFoldingAvailable || !myFolding.annotateWithParameter(data, reference)) {
reference.putUserData(DuplicatesFinder.PARAMETER, Pair.create(data.variable, type));
}
}
}
}
use of com.intellij.refactoring.util.VariableData in project intellij-community by JetBrains.
the class MakeStaticUtil method collectVariableData.
public static boolean collectVariableData(PsiMember member, InternalUsageInfo[] internalUsages, ArrayList<VariableData> variableDatum) {
HashSet<PsiField> reported = new HashSet<>();
HashSet<PsiField> accessedForWriting = new HashSet<>();
boolean needClassParameter = false;
for (InternalUsageInfo usage : internalUsages) {
final PsiElement referencedElement = usage.getReferencedElement();
if (usage.isWriting()) {
accessedForWriting.add((PsiField) referencedElement);
needClassParameter = true;
} else if (referencedElement instanceof PsiField) {
PsiField field = (PsiField) referencedElement;
reported.add(field);
} else {
needClassParameter = true;
}
}
final ArrayList<PsiField> psiFields = new ArrayList<>(reported);
Collections.sort(psiFields, (psiField, psiField1) -> psiField.getName().compareTo(psiField1.getName()));
for (final PsiField field : psiFields) {
if (accessedForWriting.contains(field))
continue;
VariableData data = new VariableData(field);
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(member.getProject());
String name = field.getName();
name = codeStyleManager.variableNameToPropertyName(name, VariableKind.FIELD);
name = codeStyleManager.propertyNameToVariableName(name, VariableKind.PARAMETER);
name = RefactoringUtil.suggestUniqueVariableName(name, member, field);
data.name = name;
data.passAsParameter = true;
variableDatum.add(data);
}
return needClassParameter;
}
use of com.intellij.refactoring.util.VariableData in project intellij-community by JetBrains.
the class MakeClassStaticTest method performWithFields.
private void performWithFields() throws Exception {
configureByFile(TEST_ROOT + getTestName(false) + ".java");
PsiElement element = TargetElementUtil.findTargetElement(myEditor, TargetElementUtil.ELEMENT_NAME_ACCEPTED);
assertTrue(element instanceof PsiClass);
PsiClass aClass = (PsiClass) element;
final ArrayList<VariableData> parametersForFields = new ArrayList<>();
final boolean addClassParameter = MakeStaticUtil.buildVariableData(aClass, parametersForFields);
new MakeClassStaticProcessor(getProject(), aClass, new Settings(true, addClassParameter ? "anObject" : null, parametersForFields.toArray(new VariableData[parametersForFields.size()]))).run();
checkResultByFile(TEST_ROOT + getTestName(false) + "_after.java");
}
Aggregations