Skip to content

Commit e13c02d

Browse files
authored
Merge pull request #125 from ml-in-programming/smartPsiElementPointer_integration
Migration to SmartPsiElementPointer and erasing name-based search and identification.
2 parents 0d57bcd + 8d8fff4 commit e13c02d

File tree

63 files changed

+451
-498
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+451
-498
lines changed

MetricsReloaded/src/test/java/com/sixrr/metrics/ui/SearchUtilTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020

2121
import java.util.Arrays;
2222
import java.util.List;
23-
import java.util.Set;
2423

25-
import static org.junit.Assert.*;
26-
import static com.sixrr.metrics.ui.SearchUtil.*;
24+
import static com.sixrr.metrics.ui.SearchUtil.tokenizeFilter;
25+
import static org.junit.Assert.assertEquals;
2726

2827
/**
2928
* @author Bas Leijdekkers

MetricsReloaded/utils/src/main/java/org/jetbrains/research/groups/ml_methods/utils/PSIUtil.java

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package org.jetbrains.research.groups.ml_methods.utils;
22

3-
import com.intellij.psi.PsiClass;
4-
import com.intellij.psi.PsiElement;
5-
import com.intellij.psi.PsiMethod;
3+
import com.intellij.openapi.fileTypes.StdFileTypes;
4+
import com.intellij.openapi.project.Project;
5+
import com.intellij.openapi.roots.ProjectRootManager;
6+
import com.intellij.openapi.roots.TestSourcesFilter;
7+
import com.intellij.openapi.vfs.VirtualFile;
8+
import com.intellij.psi.*;
69
import com.intellij.psi.util.PsiTreeUtil;
710
import org.jetbrains.annotations.NotNull;
11+
import org.jetbrains.annotations.Nullable;
812

9-
import java.util.HashSet;
10-
import java.util.Optional;
11-
import java.util.Set;
13+
import java.util.*;
14+
15+
import static com.sixrr.metrics.utils.MethodUtils.calculateSignature;
1216

1317
public final class PSIUtil {
1418
private PSIUtil() {
@@ -87,4 +91,44 @@ public static boolean isOverriding(PsiMethod method) {
8791

8892
return Optional.of(parent);
8993
}
94+
95+
public static String getHumanReadableName(@Nullable PsiElement element) {
96+
if (element instanceof PsiMethod) {
97+
return calculateSignature((PsiMethod) element);
98+
} else if (element instanceof PsiClass) {
99+
if (element instanceof PsiAnonymousClass) {
100+
return getHumanReadableName(((PsiAnonymousClass) element).getBaseClassReference().resolve());
101+
}
102+
return ((PsiClass) element).getQualifiedName();
103+
} else if (element instanceof PsiField) {
104+
final PsiMember field = (PsiMember) element;
105+
return getHumanReadableName(field.getContainingClass()) + "." + field.getName();
106+
}
107+
return "???";
108+
}
109+
110+
public static List<VirtualFile> getAllJavaFiles(Project project, boolean includeTestSources) {
111+
List<VirtualFile> javaVirtualFiles = new ArrayList<>();
112+
ProjectRootManager projectRootManager = ProjectRootManager.getInstance(project);
113+
projectRootManager.getFileIndex().iterateContent(virtualFile -> {
114+
if (isJavaFile(virtualFile)) {
115+
if (includeTestSources && TestSourcesFilter.isTestSources(virtualFile, project)) {
116+
javaVirtualFiles.add(virtualFile);
117+
}
118+
if (!TestSourcesFilter.isTestSources(virtualFile, project)) {
119+
javaVirtualFiles.add(virtualFile);
120+
}
121+
}
122+
return true;
123+
});
124+
return javaVirtualFiles;
125+
}
126+
127+
public static int getNumberOfJavaFiles(Project project, boolean includeTestSources) {
128+
return getAllJavaFiles(project, includeTestSources).size();
129+
}
130+
131+
public static boolean isJavaFile(VirtualFile virtualFile) {
132+
return virtualFile.getFileType().equals(StdFileTypes.JAVA);
133+
}
90134
}

algorithms-evaluation/src/main/java/org/jetbrains/research/groups/ml_methods/evaluation/AlgorithmEvaluator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import com.sixrr.metrics.profile.MetricsProfile;
66
import org.jetbrains.annotations.NotNull;
77
import org.jetbrains.research.groups.ml_methods.algorithm.Algorithm;
8+
import org.jetbrains.research.groups.ml_methods.algorithm.RefactoringExecutionContext;
89
import org.jetbrains.research.groups.ml_methods.refactoring.CalculatedRefactoring;
910
import org.jetbrains.research.groups.ml_methods.refactoring.MoveToClassRefactoring;
10-
import org.jetbrains.research.groups.ml_methods.algorithm.RefactoringExecutionContext;
1111
import org.jetbrains.research.groups.ml_methods.utils.MetricsProfilesUtil;
1212

1313
import java.util.Collections;

algorithms-evaluation/src/main/java/org/jetbrains/research/groups/ml_methods/evaluation/ProjectLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import com.intellij.analysis.AnalysisScope;
44
import com.intellij.openapi.project.Project;
55
import com.sixrr.metrics.utils.ProjectUtils;
6-
import org.jetbrains.research.groups.ml_methods.refactoring.MoveToClassRefactoring;
76
import org.jetbrains.research.groups.ml_methods.extraction.refactoring.RefactoringsLoader;
87
import org.jetbrains.research.groups.ml_methods.extraction.refactoring.readers.RefactoringsReaders;
8+
import org.jetbrains.research.groups.ml_methods.refactoring.MoveToClassRefactoring;
99

1010
import java.io.IOException;
1111
import java.nio.file.Path;

core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/ARI.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
import com.sixrr.stockmetrics.classMetrics.NumMethodsClassMetric;
77
import org.apache.log4j.Logger;
88
import org.jetbrains.annotations.NotNull;
9+
import org.jetbrains.research.groups.ml_methods.algorithm.AlgorithmsRepository.AlgorithmType;
910
import org.jetbrains.research.groups.ml_methods.algorithm.attributes.*;
1011
import org.jetbrains.research.groups.ml_methods.algorithm.distance.DistanceCalculator;
1112
import org.jetbrains.research.groups.ml_methods.algorithm.distance.RelevanceBasedDistanceCalculator;
13+
import org.jetbrains.research.groups.ml_methods.logging.Logging;
1214
import org.jetbrains.research.groups.ml_methods.refactoring.CalculatedRefactoring;
1315
import org.jetbrains.research.groups.ml_methods.refactoring.MoveFieldRefactoring;
1416
import org.jetbrains.research.groups.ml_methods.refactoring.MoveMethodRefactoring;
15-
import org.jetbrains.research.groups.ml_methods.logging.Logging;
1617
import org.jetbrains.research.groups.ml_methods.utils.AlgorithmsUtil;
1718

1819
import java.util.ArrayList;
@@ -27,7 +28,7 @@ public class ARI extends AbstractAlgorithm {
2728
private static final @NotNull DistanceCalculator distanceCalculator = RelevanceBasedDistanceCalculator.getInstance();
2829

2930
public ARI() {
30-
super("ARI", true);
31+
super(AlgorithmType.ARI, true);
3132
}
3233

3334
@Override

core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/AbstractAlgorithm.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
import org.apache.log4j.Logger;
88
import org.jetbrains.annotations.NotNull;
99
import org.jetbrains.annotations.Nullable;
10+
import org.jetbrains.research.groups.ml_methods.algorithm.AlgorithmsRepository.AlgorithmType;
1011
import org.jetbrains.research.groups.ml_methods.algorithm.attributes.AttributesStorage;
11-
import org.jetbrains.research.groups.ml_methods.refactoring.CalculatedRefactoring;
1212
import org.jetbrains.research.groups.ml_methods.logging.Logging;
13+
import org.jetbrains.research.groups.ml_methods.refactoring.CalculatedRefactoring;
1314

1415
import java.util.ArrayList;
1516
import java.util.List;
@@ -35,12 +36,21 @@
3536
public abstract class AbstractAlgorithm implements Algorithm {
3637
private static final Logger LOGGER = Logging.getLogger(AbstractAlgorithm.class);
3738

38-
private final String name;
39+
private final AlgorithmType algorithmType;
3940

4041
private final boolean enableParallelExecution;
4142

42-
public AbstractAlgorithm(String name, boolean enableParallelExecution) {
43-
this.name = name;
43+
/**
44+
* {@inheritDoc}
45+
*/
46+
@NotNull
47+
@Override
48+
public AlgorithmType getAlgorithmType() {
49+
return algorithmType;
50+
}
51+
52+
public AbstractAlgorithm(AlgorithmType algorithmType, boolean enableParallelExecution) {
53+
this.algorithmType = algorithmType;
4454
this.enableParallelExecution = enableParallelExecution;
4555
}
4656

@@ -49,7 +59,7 @@ public AbstractAlgorithm(String name, boolean enableParallelExecution) {
4959
*/
5060
@Override
5161
public @NotNull String getDescriptionString() {
52-
return name;
62+
return algorithmType.toString();
5363
}
5464

5565
/**
@@ -61,7 +71,7 @@ public AbstractAlgorithm(String name, boolean enableParallelExecution) {
6171
final @Nullable ExecutorService service,
6272
final boolean enableFieldRefactorings
6373
) {
64-
LOGGER.info(name + " started");
74+
LOGGER.info(algorithmType + " started");
6575
final long startTime = System.currentTimeMillis();
6676
final ProgressIndicator indicator;
6777

@@ -72,7 +82,7 @@ public AbstractAlgorithm(String name, boolean enableParallelExecution) {
7282
}
7383

7484
indicator.pushState();
75-
indicator.setText("Running " + name + "...");
85+
indicator.setText("Running " + algorithmType + "...");
7686
indicator.setFraction(0);
7787

7888
final ExecutionContext context = new ExecutionContext(
@@ -87,16 +97,16 @@ public AbstractAlgorithm(String name, boolean enableParallelExecution) {
8797
} catch (ProcessCanceledException e) {
8898
throw e;
8999
} catch (Exception e) {
90-
LOGGER.error(name + " finished with error: " + e);
91-
return new AlgorithmResult(name, e);
100+
LOGGER.error(algorithmType + " finished with error: " + e);
101+
return new AlgorithmResult(algorithmType, e);
92102
}
93103

94104
final long time = System.currentTimeMillis() - startTime;
95105
indicator.popState();
96106

97-
final AlgorithmResult result = new AlgorithmResult(refactorings, name, time, context.usedThreads);
107+
final AlgorithmResult result = new AlgorithmResult(refactorings, algorithmType, time, context.usedThreads);
98108

99-
LOGGER.info(name + " successfully finished");
109+
LOGGER.info(algorithmType + " successfully finished");
100110
LOGGER.info(result.getReport());
101111

102112
return result;

core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/Algorithm.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.util.List;
99
import java.util.concurrent.ExecutorService;
1010

11+
import static org.jetbrains.research.groups.ml_methods.algorithm.AlgorithmsRepository.AlgorithmType;
12+
1113
/**
1214
* An algorithm that analyses given {@link AttributesStorage} and produces refactoring suggestions
1315
* as an {@link AlgorithmResult}.
@@ -36,6 +38,11 @@ public interface Algorithm {
3638
*/
3739
@NotNull String getDescriptionString();
3840

41+
/**
42+
* Returns a type of algorithm. Should be used to identify algorithm and request it by enum type not by name.
43+
*/
44+
@NotNull AlgorithmType getAlgorithmType();
45+
3946
/**
4047
* Returns an array of metrics from which a features vectors for this particular algorithm
4148
* should be constructed.

core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/AlgorithmResult.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,31 @@
22

33
import org.jetbrains.annotations.NotNull;
44
import org.jetbrains.annotations.Nullable;
5+
import org.jetbrains.research.groups.ml_methods.algorithm.AlgorithmsRepository.AlgorithmType;
56
import org.jetbrains.research.groups.ml_methods.refactoring.CalculatedRefactoring;
67

78
import java.util.Collections;
89
import java.util.List;
910

1011
public class AlgorithmResult {
1112
private final List<CalculatedRefactoring> refactorings;
12-
private final String algorithmName;
13+
private final AlgorithmType algorithmType;
1314
private final long executionTime;
1415
private final int threadUsed;
1516
private final Exception exception;
1617

17-
AlgorithmResult(@NotNull List<CalculatedRefactoring> refactorings, String algorithmName, long executionTime,
18+
AlgorithmResult(@NotNull List<CalculatedRefactoring> refactorings, AlgorithmType algorithmType, long executionTime,
1819
int threadUsed) {
1920
this.refactorings = refactorings;
20-
this.algorithmName = algorithmName;
21+
this.algorithmType = algorithmType;
2122
this.executionTime = executionTime;
2223
this.threadUsed = threadUsed;
2324
this.exception = null;
2425
}
2526

26-
AlgorithmResult(String algorithmName, @NotNull Exception exception) {
27+
AlgorithmResult(AlgorithmType algorithmType, @NotNull Exception exception) {
2728
this.refactorings = Collections.emptyList();
28-
this.algorithmName = algorithmName;
29+
this.algorithmType = algorithmType;
2930
this.executionTime = 0;
3031
this.threadUsed = 0;
3132
this.exception = exception;
@@ -35,8 +36,8 @@ public List<CalculatedRefactoring> getRefactorings() {
3536
return Collections.unmodifiableList(refactorings);
3637
}
3738

38-
public String getAlgorithmName() {
39-
return algorithmName;
39+
public AlgorithmType getAlgorithmType() {
40+
return algorithmType;
4041
}
4142

4243
public long getExecutionTime() {
@@ -57,7 +58,7 @@ public boolean isSuccess() {
5758
}
5859

5960
public String getReport() {
60-
return "Results of " + algorithmName + " running" + System.lineSeparator() +
61+
return "Results of " + algorithmType + " running" + System.lineSeparator() +
6162
" Found " + refactorings.size() + " refactorings" + System.lineSeparator() +
6263
" Execution time: " + executionTime + System.lineSeparator() +
6364
" Threads used: " + threadUsed;

core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/AlgorithmsRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public static Optional<Algorithm> getAlgorithmByName(String algorithmName) {
1717
return ALGORITHMS.stream().filter(algorithm -> algorithm.getDescriptionString().equals(algorithmName)).findAny();
1818
}
1919

20+
public enum AlgorithmType {
21+
ARI, CCDA, HAC
22+
}
23+
2024
@Contract(pure = true)
2125
public static List<Algorithm> getAvailableAlgorithms() {
2226
return ALGORITHMS;

core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/CCDA.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
import com.sixrr.stockmetrics.classMetrics.NumMethodsClassMetric;
66
import org.apache.log4j.Logger;
77
import org.jetbrains.annotations.NotNull;
8+
import org.jetbrains.research.groups.ml_methods.algorithm.AlgorithmsRepository.AlgorithmType;
89
import org.jetbrains.research.groups.ml_methods.algorithm.attributes.AttributesStorage;
910
import org.jetbrains.research.groups.ml_methods.algorithm.attributes.ClassAttributes;
1011
import org.jetbrains.research.groups.ml_methods.algorithm.attributes.ClassInnerEntityAttributes;
1112
import org.jetbrains.research.groups.ml_methods.algorithm.entity.*;
13+
import org.jetbrains.research.groups.ml_methods.logging.Logging;
1214
import org.jetbrains.research.groups.ml_methods.refactoring.CalculatedRefactoring;
1315
import org.jetbrains.research.groups.ml_methods.refactoring.MoveFieldRefactoring;
1416
import org.jetbrains.research.groups.ml_methods.refactoring.MoveMethodRefactoring;
1517
import org.jetbrains.research.groups.ml_methods.refactoring.MoveToClassRefactoring;
16-
import org.jetbrains.research.groups.ml_methods.logging.Logging;
1718
import org.jetbrains.research.groups.ml_methods.utils.AlgorithmsUtil;
1819

1920
import java.util.*;
@@ -25,7 +26,7 @@ public class CCDA extends AbstractAlgorithm {
2526
private static final double ACCURACY = 1;
2627

2728
public CCDA() {
28-
super("CCDA", true);
29+
super(AlgorithmType.CCDA, true);
2930

3031
}
3132

0 commit comments

Comments
 (0)