Skip to content

Migration to unique strings which describe psi elements #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void postInterfaceMetric(@NotNull Metric metric, @NotNull PsiClass anInte
@Override
public void postMethodMetric(@NotNull Metric metric, @NotNull PsiMethod method, double value) {
final MetricsResult results = getResultsForCategory(MetricCategory.Method);
final String signature = MethodUtils.calculateSignature(method);
final String signature = MethodUtils.calculateUniqueSignature(method);
results.postValue(metric, signature, value);
results.setElementForMeasuredObject(signature, method);
}
Expand Down Expand Up @@ -170,7 +170,7 @@ public void postInterfaceMetric(@NotNull Metric metric, @NotNull PsiClass anInte
public void postMethodMetric(@NotNull Metric metric, @NotNull PsiMethod method,
double numerator, double denominator) {
final MetricsResult results = getResultsForCategory(MetricCategory.Method);
final String signature = MethodUtils.calculateSignature(method);
final String signature = MethodUtils.calculateUniqueSignature(method);
results.postValue(metric, signature, numerator, denominator);
results.setElementForMeasuredObject(signature, method);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void visitMethodCallExpression(PsiMethodCallExpression callExpression) {
super.visitMethodCallExpression(callExpression);
final PsiMethod method = callExpression.resolveMethod();
if (method != null) {
final String signature = MethodUtils.calculateSignature(method);
final String signature = MethodUtils.calculateUniqueSignature(method);
registerOperator(signature);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public void testComplexReturnType() {

public void testTwoGenericTypes() {
MetricsResultsHolderTestImpl metricResults = runMetricOnTestCase(METRIC);
assertEquals(1.0, metricResults.getMethodMetric(METRIC, "A.method(T,R,Collection)"));
assertEquals(1.0, metricResults.getMethodMetric(METRIC, "A.method(T,R,java.util.Collection)"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.Objects;
import java.util.stream.Collectors;

import static com.sixrr.metrics.utils.MethodUtils.calculateSignature;
import static com.sixrr.metrics.utils.MethodUtils.calculateUniqueSignature;
import static junit.framework.TestCase.fail;

public class MetricsResultsHolderTestImpl implements MetricsResultsHolder {
Expand Down Expand Up @@ -135,7 +135,7 @@ private PsiClass findClassByQualifiedName(Metric metric, String classQualifiedNa

private PsiMethod findMethodBySignature(Metric metric, String methodSignature) {
List<PsiMethod> methods = methodMetrics.get(metric).keySet().stream().
filter(method -> calculateSignature(method).equals(methodSignature)).
filter(method -> calculateUniqueSignature(method).equals(methodSignature)).
collect(Collectors.toList());

if (methods.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static int parametersCount(PsiMethod method) {
return method.getParameterList().getParametersCount();
}

public static String calculateSignature(PsiMethod method) {
private static String calculateSignature(PsiMethod method, boolean isCanonical) {
final PsiClass containingClass = method.getContainingClass();
final String className;
if (containingClass != null) {
Expand All @@ -122,13 +122,22 @@ public static String calculateSignature(PsiMethod method) {
out.append(',');
}
final PsiType parameterType = parameters[i].getType();
final String parameterTypeText = parameterType.getPresentableText();
final String parameterTypeText = isCanonical ?
parameterType.getCanonicalText() : parameterType.getPresentableText();
out.append(parameterTypeText);
}
out.append(')');
return out.toString();
}

public static String calculateUniqueSignature(PsiMethod method) {
return calculateSignature(method, true);
}

public static String calculateHumanReadableSignature(PsiMethod method) {
return calculateSignature(method, false);
}

public static boolean isGetter(final PsiMethod method) {
final PsiType returnType = method.getReturnType();
return !PsiType.VOID.equals(returnType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

import java.util.*;

import static com.sixrr.metrics.utils.MethodUtils.calculateSignature;
import static com.sixrr.metrics.utils.MethodUtils.calculateHumanReadableSignature;
import static com.sixrr.metrics.utils.MethodUtils.calculateUniqueSignature;

public final class PSIUtil {
private PSIUtil() {
Expand Down Expand Up @@ -93,16 +94,25 @@ public static boolean isOverriding(PsiMethod method) {
}

public static String getHumanReadableName(@Nullable PsiElement element) {
return getPsiElementName(element, false);
}

public static String getUniqueName(@Nullable PsiElement element) {
return getPsiElementName(element, true);
}

private static String getPsiElementName(@Nullable PsiElement element, boolean isUnique) {
if (element instanceof PsiMethod) {
return calculateSignature((PsiMethod) element);
return isUnique ?
calculateUniqueSignature((PsiMethod) element) : calculateHumanReadableSignature((PsiMethod) element);
} else if (element instanceof PsiClass) {
if (element instanceof PsiAnonymousClass) {
return getHumanReadableName(((PsiAnonymousClass) element).getBaseClassReference().resolve());
return getPsiElementName(((PsiAnonymousClass) element).getBaseClassReference().resolve(), isUnique);
}
return ((PsiClass) element).getQualifiedName();
} else if (element instanceof PsiField) {
final PsiMember field = (PsiMember) element;
return getHumanReadableName(field.getContainingClass()) + "." + field.getName();
return getPsiElementName(field.getContainingClass(), isUnique) + "." + field.getName();
}
return "???";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

import java.util.*;

import static org.jetbrains.research.groups.ml_methods.utils.PSIUtil.getHumanReadableName;
import static org.jetbrains.research.groups.ml_methods.utils.PSIUtil.getUniqueName;


/**
* Extracts every {@link CodeEntity} from {@link AnalysisScope} according to a {@link FinderStrategy} that is uses.
Expand Down Expand Up @@ -157,7 +158,7 @@ public void visitFile(PsiFile file) {
@Override
public void visitClass(PsiClass aClass) {
indicator.checkCanceled();
classForName.put(getHumanReadableName(aClass), aClass);
classForName.put(getUniqueName(aClass), aClass);
if (!strategy.acceptClass(aClass)) {
return;
}
Expand Down Expand Up @@ -237,7 +238,7 @@ private boolean isProperty(PsiClass aClass, PsiMember member) {

@Contract("null -> false")
private boolean isClassInProject(final @Nullable PsiClass aClass) {
return aClass != null && classForName.containsKey(getHumanReadableName(aClass));
return aClass != null && classForName.containsKey(getUniqueName(aClass));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import java.util.Objects;

import static org.jetbrains.research.groups.ml_methods.utils.PSIUtil.getHumanReadableName;
import static org.jetbrains.research.groups.ml_methods.utils.PSIUtil.getUniqueName;

public class FieldEntity extends ClassInnerEntity {
private final @NotNull PsiField psiField;
Expand All @@ -31,7 +31,7 @@ public FieldEntity(
@Override
public @NotNull String getIdentifier() {
return ApplicationManager.getApplication().runReadAction(
(Computable<String>) () -> getHumanReadableName(psiField.getContainingClass()) + "." + psiField.getName()
(Computable<String>) () -> getUniqueName(psiField.getContainingClass()) + "." + psiField.getName()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public MethodEntity(
@Override
public @NotNull String getIdentifier() {
return ApplicationManager.getApplication().runReadAction(
(Computable<String>) () -> MethodUtils.calculateSignature(psiMethod)
(Computable<String>) () -> MethodUtils.calculateUniqueSignature(psiMethod)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.Map.Entry;
import java.util.stream.Collectors;

import static org.jetbrains.research.groups.ml_methods.utils.PSIUtil.getHumanReadableName;
import static org.jetbrains.research.groups.ml_methods.utils.PSIUtil.getUniqueName;

public final class RefactoringUtil {
private static Logger LOG = Logging.getLogger(RefactoringUtil.class);
Expand Down Expand Up @@ -52,9 +52,9 @@ public static List<CalculatedRefactoring> intersect(Collection<List<CalculatedRe
return refactorings.stream()
.flatMap(List::stream)
.collect(Collectors.groupingBy(refactoring ->
getHumanReadableName(refactoring.getRefactoring().getEntity()) +
getUniqueName(refactoring.getRefactoring().getEntity()) +
"&" +
getHumanReadableName(refactoring.getRefactoring().getTargetClass()),
getUniqueName(refactoring.getRefactoring().getTargetClass()),
Collectors.toList()))
.values().stream()
.filter(collection -> collection.size() == refactorings.size())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static com.intellij.testFramework.UsefulTestCase.assertOneOf;
import static junit.framework.TestCase.assertTrue;
import static org.jetbrains.research.groups.ml_methods.utils.PSIUtil.getHumanReadableName;
import static org.jetbrains.research.groups.ml_methods.utils.PSIUtil.getUniqueName;
import static org.junit.Assert.assertEquals;

class TestCasesCheckers {
Expand All @@ -23,8 +24,8 @@ class TestCasesCheckers {

private static Map<String, String> toMap(List<CalculatedRefactoring> refactorings) {
return refactorings.stream().collect(Collectors.toMap(it ->
getHumanReadableName(it.getRefactoring().getEntity()),
it -> getHumanReadableName(it.getRefactoring().getTargetClass())));
getUniqueName(it.getRefactoring().getEntity()),
it -> getUniqueName(it.getRefactoring().getTargetClass())));
}

@NotNull
Expand Down Expand Up @@ -206,7 +207,7 @@ void checkMovieRentalStoreWithFeatureEnvy(@NotNull RefactoringExecutionContext c

final Map<String, String> refactorings = toMap(context.getResultForType(algorithmType).getRefactorings());
final Map<String, String> expected = new HashMap<>();
expected.put(getPackageName() + ".Customer.getMovie(Movie)", getPackageName() + ".Rental");
expected.put(getPackageName() + ".Customer.getMovie(" + getPackageName() + ".Movie)", getPackageName() + ".Rental");
assertEquals(expected, refactorings);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public JMoveRefactoringTextRepresentation(MoveMethodRefactoring refactoring) {

@Override
public boolean isOfGivenMethod(PsiMethod method) {
String methodsSignature = MethodUtils.calculateSignature(method);
String methodsSignature = MethodUtils.calculateHumanReadableSignature(method);
String methodsSignatureWithoutParams = methodsSignature.split("\\(")[0];
String refactoringSignature = getMethodsSignature();
return refactoringSignature.equals(methodsSignature) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ private void setMethod(@NotNull PsiMethod method) {
this.method = method;

if (oldMethod != null) {
String oldSignature = MethodUtils.calculateSignature(oldMethod);
String newSignature = MethodUtils.calculateSignature(method);
String oldSignature = MethodUtils.calculateHumanReadableSignature(oldMethod);
String newSignature = MethodUtils.calculateHumanReadableSignature(method);

throw new IllegalStateException("Refactorings set is ambiguous. Candidates: " +
oldSignature + ", " + newSignature);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ private void runTestCase(final @NotNull InfoValidator validator) throws Exceptio
assertSameElements(
repository.getMethods()
.stream()
.map(MethodUtils::calculateSignature)
.map(MethodUtils::calculateUniqueSignature)
.collect(Collectors.toList()),
validator.methodNames()
);

for (PsiMethod method : repository.getMethods()) {
String methodName = MethodUtils.calculateSignature(method);
String methodName = MethodUtils.calculateUniqueSignature(method);

MethodInfo info = repository.getMethodInfo(method).orElseThrow(
() -> new IllegalStateException(
Expand All @@ -124,39 +124,39 @@ private void runTestCase(final @NotNull InfoValidator validator) throws Exceptio
assertSameElements(
info.getSameInstanceCallers()
.stream()
.map(MethodUtils::calculateSignature)
.map(MethodUtils::calculateUniqueSignature)
.collect(Collectors.toList()),
validator.getSameObjectCallers(methodName)
);

assertSameElements(
info.getAnotherInstanceCallers()
.stream()
.map(MethodUtils::calculateSignature)
.map(MethodUtils::calculateUniqueSignature)
.collect(Collectors.toList()),
validator.getAnotherObjectCallers(methodName)
);

assertSameElements(
info.getSameInstanceTargets()
.stream()
.map(MethodUtils::calculateSignature)
.map(MethodUtils::calculateUniqueSignature)
.collect(Collectors.toList()),
validator.getSameObjectTargets(methodName)
);

assertSameElements(
info.getAnotherInstanceTargets()
.stream()
.map(MethodUtils::calculateSignature)
.map(MethodUtils::calculateUniqueSignature)
.collect(Collectors.toList()),
validator.getAnotherObjectTargets(methodName)
);

assertSameElements(
info.getAccessedFields()
.stream()
.map(PSIUtil::getHumanReadableName)
.map(PSIUtil::getUniqueName)
.collect(Collectors.toList()),
validator.getAccessedFields(methodName)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public MoveMethodRefactoringFeatures(
);

methodMetricsValues = extractMetricsResultsFor(
MethodUtils.calculateSignature(refactoring.getMethod()),
MethodUtils.calculateUniqueSignature(refactoring.getMethod()),
resultsForMethods
);
}
Expand Down