Skip to content

Commit

Permalink
Use two different classes for comparators
Browse files Browse the repository at this point in the history
  • Loading branch information
surli committed Apr 5, 2018
1 parent 0f48d2f commit 1316155
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (C) 2006-2017 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and
* abiding by the rules of distribution of free software. You can use, modify
* and/or redistribute the software under the terms of the CeCILL-C license as
* circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package spoon.support.comparator.cu;

import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;

import java.util.Comparator;

public class FixedOrderBasedOnFileNameCompilationUnitComparator implements Comparator<CompilationUnitDeclaration> {
@Override
public int compare(CompilationUnitDeclaration o1, CompilationUnitDeclaration o2) {
String s1 = new String(o1.getFileName());
String s2 = new String(o2.getFileName());
return s1.compareTo(s2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,23 @@
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package spoon.support.compiler.jdt;
package spoon.support.comparator.cu;

import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
import spoon.Launcher;

import java.util.Comparator;
import java.util.Random;

public class CompilationUnitComparator implements Comparator<CompilationUnitDeclaration> {
public class RandomizeCompilationUnitOrderComparator implements Comparator<CompilationUnitDeclaration> {
private Random random;

@Override
public int compare(CompilationUnitDeclaration o1, CompilationUnitDeclaration o2) {
int seed = 0;
try {
if (System.getenv("SPOON_SEED_CU_COMPARATOR") != null) {
seed = Integer.parseInt(System.getenv("SPOON_SEED_CU_COMPARATOR"));
Launcher.LOGGER.warn("Seed for CU sorting set with: " + seed);
}
} catch (NumberFormatException | SecurityException e) {
Launcher.LOGGER.error("Error while parsing Spoon seed for CU sorting", e);
}
public RandomizeCompilationUnitOrderComparator(int seed) {
this.random = new Random(seed);
}

if (seed == 0) {
String s1 = new String(o1.getFileName());
String s2 = new String(o2.getFileName());
return s1.compareTo(s2);
} else {
Random random = new Random(seed);
int r = random.nextInt(3); // can be 0, 1 or 2
return r - 1; // can be -1, 0 or 1
}
}
@Override
public int compare(CompilationUnitDeclaration o1, CompilationUnitDeclaration o2) {
int r = this.random.nextInt(3); // can be 0, 1 or 2
return r - 1; // can be -1, 0 or 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import spoon.reflect.visitor.PrettyPrinter;
import spoon.reflect.visitor.Query;
import spoon.support.QueueProcessingManager;
import spoon.support.comparator.cu.FixedOrderBasedOnFileNameCompilationUnitComparator;
import spoon.support.comparator.cu.RandomizeCompilationUnitOrderComparator;
import spoon.support.compiler.VirtualFolder;

import java.io.ByteArrayInputStream;
Expand All @@ -61,6 +63,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -83,12 +86,32 @@ public class JDTBasedSpoonCompiler implements spoon.SpoonModelBuilder {
//The classpath used to build templates
protected String[] templateClasspath = new String[0];
protected List<CompilationUnitFilter> compilationUnitFilters = new ArrayList<>();
private Comparator<CompilationUnitDeclaration> cuComparator;

/**
* Default constructor
*/
public JDTBasedSpoonCompiler(Factory factory) {
this.factory = factory;
this.initializeCUCOmparator();
}

private void initializeCUCOmparator() {
int seed = 0;
try {
if (System.getenv("SPOON_SEED_CU_COMPARATOR") != null) {
seed = Integer.parseInt(System.getenv("SPOON_SEED_CU_COMPARATOR"));
Launcher.LOGGER.warn("Seed for CU sorting set with: " + seed);
}
} catch (NumberFormatException | SecurityException e) {
Launcher.LOGGER.error("Error while parsing Spoon seed for CU sorting", e);
}

if (seed != 0) {
this.cuComparator = new RandomizeCompilationUnitOrderComparator(seed);
} else {
this.cuComparator = new FixedOrderBasedOnFileNameCompilationUnitComparator();
}
}

@Override
Expand Down Expand Up @@ -428,10 +451,15 @@ protected CompilationUnitDeclaration[] buildUnits(JDTBuilder jdtBuilder, SpoonFo
return units;
}

private List<CompilationUnitDeclaration> orderCompilationUnits(CompilationUnitDeclaration[] units) {
List<CompilationUnitDeclaration> unitList = new ArrayList<>(Arrays.asList(units));
unitList.sort(this.cuComparator);
return unitList;
}

protected void buildModel(CompilationUnitDeclaration[] units) {
JDTTreeBuilder builder = new JDTTreeBuilder(factory);
List<CompilationUnitDeclaration> unitList = new ArrayList<>(Arrays.asList(units));
unitList.sort(new CompilationUnitComparator());
List<CompilationUnitDeclaration> unitList = this.orderCompilationUnits(units);

unitLoop:
for (CompilationUnitDeclaration unit : unitList) {
Expand Down

0 comments on commit 1316155

Please sign in to comment.