Skip to content

Added logger for collecting preferences after refactorings were selected. Do not merge. #47

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
2 changes: 1 addition & 1 deletion src/main/java/org/ml_methods_group/algorithm/AKMeans.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected List<Refactoring> calculateRefactorings(ExecutionContext context, bool
.filter(e -> enableFieldRefactorings || !e.isField())
.map(e -> new Refactoring(e.getName(), dominant.getKey(),
getDensityBasedAccuracyRating(dominant.getValue(), community.size()) * ACCURACY,
e.isField()))
e.isField(), e.getElement()))
.forEach(refactorings::add);
}
return refactorings;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/ml_methods_group/algorithm/ARI.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private List<Refactoring> findRefactoring(Entity entity, List<Refactoring> accum
if (!targetClassName.equals(entity.getClassName())) {
accumulator.add(new Refactoring(entity.getName(), targetClassName,
AlgorithmsUtil.getGapBasedAccuracyRating(minDistance, difference) * ACCURACY,
entity.isField()));
entity.isField(), entity.getElement()));
}
return accumulator;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/ml_methods_group/algorithm/CCDA.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ protected List<Refactoring> calculateRefactorings(ExecutionContext context, bool
if (enableFieldRefactorings || !entry.getKey().isField()) {
return new Refactoring(entry.getKey().getName(), entry.getValue(),
AlgorithmsUtil.getDensityBasedAccuracyRating(dominant, size) * ACCURACY,
entry.getKey().isField());
entry.getKey().isField(), entry.getKey().getElement());
} else {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/ml_methods_group/algorithm/HAC.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected List<Refactoring> calculateRefactorings(ExecutionContext context, bool
if (enableFieldRefactorings || !entity.isField()) {
refactorings.add(new Refactoring(entity.getName(), className,
getDensityBasedAccuracyRating(dominantClass.getValue(), entitiesCount) * ACCURACY,
entity.isField()));
entity.isField(), entity.getElement()));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/ml_methods_group/algorithm/MRI.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected List<Refactoring> calculateRefactorings(ExecutionContext context, bool
processMethod(refactorings, currentEntity, nearestClass, accuracyRating);
} else {
refactorings.add(new Refactoring(currentEntity.getName(), nearestClass.getName(), accuracyRating,
currentEntity.isField()));
currentEntity.isField(), currentEntity.getElement()));
}
}

Expand Down Expand Up @@ -119,7 +119,7 @@ private Holder min(Holder first, Holder second) {
private void processMethod(List<Refactoring> refactorings, Entity method, ClassEntity target, double accuracy) {
if (method.isMovable()) {
final ClassEntity containingClass = classesByName.get(method.getClassName());
refactorings.add(new Refactoring(method.getName(), target.getName(), accuracy, false));
refactorings.add(new Refactoring(method.getName(), target.getName(), accuracy, false, method.getElement()));
containingClass.removeFromClass(method.getName());
target.addToClass(method.getName());
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/ml_methods_group/algorithm/Refactoring.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.ml_methods_group.algorithm;

import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;

public class Refactoring {
Expand All @@ -24,13 +25,27 @@ public class Refactoring {
private final double accuracy;
private final boolean isUnitField;

public PsiElement getElement() {
return element;
}

private PsiElement element;

public Refactoring(@NotNull String unit, @NotNull String target, double accuracy, boolean isUnitField) {
this.unit = unit;
this.target = target;
this.accuracy = accuracy;
this.isUnitField = isUnitField;
}

public Refactoring(@NotNull String unit, @NotNull String target, double accuracy, boolean isUnitField, PsiElement element) {
this.unit = unit;
this.target = target;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this(unit, target, .... )

this.accuracy = accuracy;
this.isUnitField = isUnitField;
this.element = element;
}

public String getUnit() {
return unit;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,23 @@ public abstract class Entity {

private final RelevantProperties relevantProperties;
private final String name;

public PsiElement getElement() {
return element;
}

private PsiElement element;
private double[] vector;
protected boolean isMovable = true;

public Entity(PsiElement element) {
this.element = element;
this.name = PsiSearchUtil.getHumanReadableName(element);
relevantProperties = new RelevantProperties();
}

protected Entity(Entity original) {
this.element = original.element;
relevantProperties = original.relevantProperties.copy();
name = original.name;
vector = Arrays.copyOf(original.vector, original.vector.length);
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/org/ml_methods_group/config/Logging.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

package org.ml_methods_group.config;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.*;

import java.io.IOException;

public class Logging {
public static Logger getLogger(Class<?> aClass) {
Expand All @@ -28,4 +27,11 @@ public static Logger getLogger(Class<?> aClass) {
logger.addAppender(new ConsoleAppender(new PatternLayout("%p [%c.%M] - %m%n")));
return logger;
}

public static Logger getRefactoringLogger(Class<?> aClass) throws IOException {
final Logger logger = Logger.getLogger(aClass.getName() + "-refactoring");
logger.setLevel(Level.INFO);
logger.addAppender(new FileAppender(new PatternLayout("%p [%c.%M] - %m%n"), "~/ArchitectureReloaded/log/refactorings.log", true));
return logger;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.Nls;
Expand All @@ -40,6 +41,11 @@ public class RefactorIntentionAction extends BaseIntentionAction {
this.refactoring = new Refactoring(unit, to, 0, false);
}

RefactorIntentionAction(String unit, String to, AnalysisScope scope, PsiElement element) {
this.scope = scope;
this.refactoring = new Refactoring(unit, to, 0, false, element);
}

@NotNull
@Override
public String getText() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private static void setAnnotations(@NotNull PsiElement element,
String.format("Can be moved to %s (%s)",
refactorings.get(name), algorithmName));

annotation.registerFix(new RefactorIntentionAction(name, refactorings.get(name), scope));
annotation.registerFix(new RefactorIntentionAction(name, refactorings.get(name), scope, element));
}
}

Expand Down
91 changes: 91 additions & 0 deletions src/main/java/org/ml_methods_group/ui/ClassRefactoringPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
package org.ml_methods_group.ui;

import com.intellij.analysis.AnalysisScope;
import com.intellij.psi.*;
import com.intellij.ui.ScrollPaneFactory;
import com.intellij.ui.TableSpeedSearch;
import com.intellij.ui.components.JBPanel;
import com.intellij.ui.table.JBTable;
import org.apache.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.ml_methods_group.algorithm.Refactoring;
import org.ml_methods_group.config.Logging;
import org.ml_methods_group.utils.ArchitectureReloadedBundle;
import org.ml_methods_group.utils.ExportResultsUtil;
import org.ml_methods_group.utils.PsiSearchUtil;
Expand All @@ -34,8 +37,12 @@
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;

import static javax.swing.ListSelectionModel.SINGLE_SELECTION;
Expand Down Expand Up @@ -65,6 +72,14 @@ class ClassRefactoringPanel extends JPanel {
private final Map<Refactoring, String> warnings;
private boolean isFieldDisabled;
private final List<Refactoring> refactorings;
private Logger logger;
{
try {
logger = Logging.getRefactoringLogger(ClassRefactoringPanel.class);
} catch (IOException e) {
e.printStackTrace();
}
}

ClassRefactoringPanel(List<Refactoring> refactorings, @NotNull AnalysisScope scope) {
this.scope = scope;
Expand Down Expand Up @@ -166,11 +181,87 @@ private JComponent createButtonsPanel() {
return panel;
}

private static int countLines(String str){
String[] lines = str.split("\r\n|\r|\n");
return lines.length;
}

private void refactorSelected() {
doRefactorButton.setEnabled(false);
selectAllButton.setEnabled(false);
table.setEnabled(false);
final List<Refactoring> refactorings = model.pullSelected();
for (Refactoring refactoring : refactorings) {
logger.info("-------");
logger.info(refactoring.toString());
logger.info("Is field - " + refactoring.isUnitField());
if (!refactoring.isUnitField()) {
PsiMethod psiMethod = (PsiMethod) refactoring.getElement();
String name = psiMethod.getName();
PsiStatement[] statements = Objects.requireNonNull(psiMethod.getBody()).getStatements();
int numberOfStatements = 0;
final int numberOfAsserts[] = new int[1];
final int numberOfLoops[] = new int[1];
final int numberOfLocalVariables[] = new int[1];
for (PsiStatement statement : statements){
statement.accept(new JavaRecursiveElementVisitor() {

@Override
public void visitLocalVariable(PsiLocalVariable variable) {
super.visitLocalVariable(variable);
numberOfLocalVariables[0]++;
}

@Override
public void visitDoWhileStatement(PsiDoWhileStatement statement) {
super.visitDoWhileStatement(statement);
numberOfLoops[0]++;
}

@Override
public void visitForStatement(PsiForStatement statement) {
super.visitForStatement(statement);
numberOfLoops[0]++;
}

@Override
public void visitForeachStatement(PsiForeachStatement statement) {
super.visitForeachStatement(statement);
numberOfLoops[0]++;
}

@Override
public void visitWhileStatement(PsiWhileStatement statement) {
super.visitWhileStatement(statement);
numberOfLoops[0]++;
}

@Override
public void visitAssertStatement(PsiAssertStatement statement) {
super.visitAssertStatement(statement);
numberOfAsserts[0]++;
}


});
numberOfStatements += countLines(statement.getText().replaceAll("(?m)^[ \t]*\r?\n", ""));
}
logger.info("Number of local variables = " + numberOfLocalVariables[0]);
logger.info("Number of loops = " + numberOfLoops[0]);
logger.info("Number of asserts = " + numberOfAsserts[0]);
logger.info("Number of lines = " + numberOfStatements);
logger.info("Is static = " + psiMethod.getModifierList().hasExplicitModifier("static"));
logger.info("Is private = " + psiMethod.getModifierList().hasExplicitModifier("private"));
logger.info("Number of parameters = " + psiMethod.getParameterList().getParametersCount());
logger.info("Return type = " + psiMethod.getReturnType());
logger.info("Is constructor = " + psiMethod.isConstructor());
logger.info("Throws an exception = " + (psiMethod.getThrowsList().getReferencedTypes().length != 0));
logger.info("Method's name is (" + name + ") length = " + name.length());
}
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
logger.info(dateFormat.format(date));
}
RefactoringUtil.moveRefactoring(refactorings, scope, model);
table.setEnabled(true);
doRefactorButton.setEnabled(true);
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/org/ml_methods_group/utils/RefactoringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@

import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -50,6 +52,7 @@ public final class RefactoringUtil {
private static class CachedMember {
public final PsiMember member;
public final String oldName;

public CachedMember(@NotNull PsiMember member, @NotNull String oldName) {
this.member = member;
this.oldName = oldName;
Expand Down Expand Up @@ -90,7 +93,7 @@ public static void moveRefactoring(@NotNull List<Refactoring> refactorings,
}

private static Set<String> moveMembersRefactoring(Collection<CachedMember> elements, PsiClass targetClass,
AnalysisScope scope) {
AnalysisScope scope) {
final Map<PsiClass, Set<CachedMember>> groupByCurrentClass = elements.stream()
.collect(groupingBy((CachedMember cm) -> cm.member.getContainingClass(), Collectors.toSet()));

Expand Down Expand Up @@ -276,13 +279,21 @@ public static List<Refactoring> combine(Collection<List<Refactoring>> refactorin
.collect(Collectors.toList());
}

public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Set<Object> seen = ConcurrentHashMap.newKeySet();
return t -> seen.add(keyExtractor.apply(t));
}

private static Refactoring combine(List<Refactoring> refactorings, String unit, int algorithmsCount) {
boolean isUnitField = refactorings.get(0).isUnitField();
final Map<String, Double> target = refactorings.stream()
.collect(Collectors.toMap(Refactoring::getTarget, RefactoringUtil::getSquaredAccuarcy, Double::sum));
final Map<String, PsiElement> element = refactorings.stream()
.filter(distinctByKey(Refactoring::getTarget))
.collect(Collectors.toMap(Refactoring::getTarget, Refactoring::getElement));
return target.entrySet().stream()
.max(Entry.comparingByValue())
.map(entry -> new Refactoring(unit, entry.getKey(), Math.sqrt(entry.getValue() / algorithmsCount), isUnitField))
.map(entry -> new Refactoring(unit, entry.getKey(), Math.sqrt(entry.getValue() / algorithmsCount), isUnitField, element.get(entry.getKey())))
.orElse(null);
}

Expand Down