本文整理汇总了Java中spoon.reflect.declaration.CtType类的典型用法代码示例。如果您正苦于以下问题:Java CtType类的具体用法?Java CtType怎么用?Java CtType使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CtType类属于spoon.reflect.declaration包,在下文中一共展示了CtType类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: instrumentAll
import spoon.reflect.declaration.CtType; //导入依赖的package包/类
private void instrumentAll(InputConfiguration configuration, CtType<?> testClass) {
final String classesDirectory = this.program.getProgramDir() + "/" + this.program.getClassesDir();
this.internalClassLoader = MemoryClassLoaderFactory.createMemoryClassLoader(testClass, configuration);
/* instrument all of them */
final Iterator<File> iterator = FileUtils.iterateFiles(new File(classesDirectory), new String[]{"class"}, true);
while (iterator.hasNext()) {
final File next = iterator.next();
final String fileName = next.getPath().substring(classesDirectory.length());
final String fullQualifiedName = fileName.replaceAll("/", ".").substring(0, fileName.length() - ".class".length());
try {
this.internalClassLoader.addDefinition(fullQualifiedName,
this.instrumenter.instrument(this.internalClassLoader.getResourceAsStream(fileName), fullQualifiedName));
} catch (IOException e) {
LOGGER.error("Encountered a problem while instrumenting " + fullQualifiedName);
throw new RuntimeException(e);
}
}
clearCache(this.internalClassLoader);
}
示例2: run
import spoon.reflect.declaration.CtType; //导入依赖的package包/类
public static TestListener run(InputConfiguration configuration,
URLClassLoader classLoader,
CtType<?> testClass,
Collection<String> testMethodNames,
RunListener... listeners) {
Logger.reset();
Logger.setLogDir(new File(configuration.getInputProgram().getProgramDir() + "/log"));
if (testClass.getModifiers().contains(ModifierKind.ABSTRACT)) {
final CtTypeReference<?> referenceToAbstractClass = testClass.getReference();
return testClass.getFactory().Class().getAll().stream()
.filter(ctType -> ctType.getSuperclass() != null)
.filter(ctType ->
ctType.getSuperclass().equals(referenceToAbstractClass)
)
.map(ctType -> run(configuration, classLoader, ctType, testMethodNames, listeners))
.reduce(new TestListener(), TestListener::aggregate);
}
return TestRunnerFactory.createRunner(testClass, classLoader).run(testClass.getQualifiedName(), testMethodNames, listeners);
}
示例3: getMethod
import spoon.reflect.declaration.CtType; //导入依赖的package包/类
public CtMethod getMethod(CtType<?> ctClass) {
if ("none".equals(this.simpleNameMethod)) {
return null;
} else {
if (this.testCase == null) {
List<CtMethod<?>> methodsByName = ctClass.getMethodsByName(this.simpleNameMethod);
if (methodsByName.isEmpty()) {
if (ctClass.getSuperclass() != null) {
return getMethod(ctClass.getSuperclass().getDeclaration());
} else {
return null;
}
}
this.testCase = methodsByName.get(0);
}
return this.testCase;
}
}
示例4: createAmplifiedTest
import spoon.reflect.declaration.CtType; //导入依赖的package包/类
public static CtType createAmplifiedTest(List<CtMethod<?>> ampTest, CtType<?> classTest) {
CtType amplifiedTest = classTest.clone();
final String amplifiedName = classTest.getSimpleName().startsWith("Test") ?
classTest.getSimpleName() + "Ampl" :
"Ampl" + classTest.getSimpleName();
amplifiedTest.setSimpleName(amplifiedName);
classTest.getMethods().stream().filter(AmplificationChecker::isTest).forEach(amplifiedTest::removeMethod);
ampTest.forEach(amplifiedTest::addMethod);
final CtTypeReference classTestReference = classTest.getReference();
amplifiedTest.getElements(new TypeFilter<CtTypeReference>(CtTypeReference.class) {
@Override
public boolean matches(CtTypeReference element) {
return element.equals(classTestReference) && super.matches(element);
}
}).forEach(ctTypeReference -> ctTypeReference.setSimpleName(amplifiedName));
classTest.getPackage().addType(amplifiedTest);
return amplifiedTest;
}
示例5: computeClassProvider
import spoon.reflect.declaration.CtType; //导入依赖的package包/类
public static Set<CtType> computeClassProvider(CtType testClass) {
List<CtType> types = Query.getElements(testClass.getParent(CtPackage.class), new TypeFilter(CtType.class));
types = types.stream()
.filter(Objects::nonNull)
.filter(type -> type.getPackage() != null)
.filter(type -> type.getPackage().getQualifiedName().equals(testClass.getPackage().getQualifiedName()))
.collect(Collectors.toList());
if (testClass.getParent(CtType.class) != null) {
types.add(testClass.getParent(CtType.class));
}
types.addAll(types.stream()
.flatMap(type -> getImport(type).stream())
.collect(Collectors.toSet()));
return new HashSet<>(types);
}
示例6: getImport
import spoon.reflect.declaration.CtType; //导入依赖的package包/类
public static Set<CtType> getImport(CtType type) {
if (!AmplificationHelper.importByClass.containsKey(type)) {
ImportScanner importScanner = new ImportScannerImpl();
try {
importScanner.computeImports(type);
Set<CtType> set = importScanner.getAllImports()
.stream()
.map(CtReference::getDeclaration)
.filter(Objects::nonNull)
.filter(ctElement -> ctElement instanceof CtType)
.map(ctElement -> (CtType) ctElement)
.collect(Collectors.toSet());
AmplificationHelper.importByClass.put(type, set);
} catch (Exception e) {
AmplificationHelper.importByClass.put(type, new HashSet<>(0));
}
}
return AmplificationHelper.importByClass.get(type);
}
示例7: compileAndRunTests
import spoon.reflect.declaration.CtType; //导入依赖的package包/类
private TestListener compileAndRunTests(CtType classTest, List<CtMethod<?>> currentTestList) {
CtType amplifiedTestClass = this.testSelector.buildClassForSelection(classTest, currentTestList);
final TestListener result = TestCompiler.compileAndRun(
amplifiedTestClass,
this.compiler,
currentTestList,
this.configuration
);
final long numberOfSubClasses = classTest.getFactory().Class().getAll().stream()
.filter(subClass -> classTest.getReference().equals(subClass.getSuperclass()))
.count();
if (result == null ||
!result.getFailingTests().isEmpty() ||
(!classTest.getModifiers().contains(ModifierKind.ABSTRACT) &&
result.getRunningTests().size() != currentTestList.size()) ||
(classTest.getModifiers().contains(ModifierKind.ABSTRACT) &&
numberOfSubClasses != result.getRunningTests().size())) {
return null;
} else {
LOGGER.info("update test testCriterion");
testSelector.update();
return result;
}
}
示例8: generateSingletonList
import spoon.reflect.declaration.CtType; //导入依赖的package包/类
@SuppressWarnings("unchecked")
static CtExpression<?> generateSingletonList(CtTypeReference type, String nameMethod, Class<?> typeOfCollection) {
final Factory factory = type.getFactory();
final CtType<?> collectionsType = factory.Type().get(Collections.class);
final CtTypeAccess<?> accessToCollections = factory.createTypeAccess(collectionsType.getReference());
final CtMethod<?> singletonListMethod = collectionsType.getMethodsByName(nameMethod).get(0);
final CtExecutableReference executableReference = factory.Core().createExecutableReference();
executableReference.setStatic(true);
executableReference.setSimpleName(singletonListMethod.getSimpleName());
executableReference.setDeclaringType(collectionsType.getReference());
executableReference.setType(factory.createCtTypeReference(typeOfCollection));
if (!type.getActualTypeArguments().isEmpty() &&
type.getActualTypeArguments().stream().allMatch(ValueCreatorHelper::canGenerateAValueForType)) {
executableReference.setParameters(type.getActualTypeArguments());
List<CtExpression<?>> parameters = type.getActualTypeArguments().stream()
.map(ValueCreator::generateRandomValue).collect(Collectors.toList());
return factory.createInvocation(accessToCollections, executableReference, parameters);
} else {
return factory.createInvocation(accessToCollections, executableReference,
factory.createConstructorCall(factory.Type().createReference(Object.class))
);
}
}
示例9: computeAmplifiedCoverage
import spoon.reflect.declaration.CtType; //导入依赖的package包/类
private Coverage computeAmplifiedCoverage() {
// computing the amplified test coverage
CtType<?> clone = this.currentClassTestToBeAmplified.clone();
clone.setParent(this.currentClassTestToBeAmplified.getParent());
this.currentClassTestToBeAmplified.getMethods().stream()
.filter(AmplificationChecker::isTest)
.forEach(clone::removeMethod);
this.selectedAmplifiedTest.forEach(clone::addMethod);
DSpotUtils.printJavaFileWithComment(clone, new File(DSpotCompiler.pathToTmpTestSources));
final String classesOfProject = program.getProgramDir() + program.getClassesDir() +
AmplificationHelper.PATH_SEPARATOR + program.getProgramDir() + program.getTestClassesDir();
final String classpath = AutomaticBuilderFactory.getAutomaticBuilder(this.configuration)
.buildClasspath(program.getProgramDir()) +
AmplificationHelper.PATH_SEPARATOR + classesOfProject;
return EntryPoint.runCoverageOnTestClasses(classpath, classesOfProject,
DSpotUtils.getAllTestClasses(configuration)
);
}
示例10: generateAsserts
import spoon.reflect.declaration.CtType; //导入依赖的package包/类
public List<CtMethod<?>> generateAsserts(CtType<?> testClass, List<CtMethod<?>> tests) throws IOException, ClassNotFoundException {
CtType cloneClass = testClass.clone();
cloneClass.setParent(testClass.getParent());
List<CtMethod<?>> testWithoutAssertions = tests.stream()
.map(this.assertionRemover::removeAssertion)
.collect(Collectors.toList());
testWithoutAssertions.forEach(cloneClass::addMethod);
MethodsAssertGenerator ags = new MethodsAssertGenerator(testClass, this.configuration, compiler);
final List<CtMethod<?>> amplifiedTestWithAssertion =
ags.generateAsserts(cloneClass, testWithoutAssertions);
if (amplifiedTestWithAssertion.isEmpty()) {
LOGGER.info("Could not generate any test with assertions");
} else {
LOGGER.info("{} new tests with assertions generated", amplifiedTestWithAssertion.size());
}
return amplifiedTestWithAssertion;
}
示例11: getPitTaskOptions
import spoon.reflect.declaration.CtType; //导入依赖的package包/类
private String getPitTaskOptions(CtType<?> testClass) {
return NEW_LINE + NEW_LINE + "pitest {" + NEW_LINE +
" " + OPT_TARGET_CLASSES + "['" + configuration.getProperty("filter") + "']" + NEW_LINE +
" " + OPT_WITH_HISTORY + "true" + NEW_LINE +
" " + OPT_VALUE_REPORT_DIR + NEW_LINE +
" " + OPT_VALUE_FORMAT + NEW_LINE +
(configuration.getProperty(PROPERTY_VALUE_TIMEOUT) != null ?
" " + PROPERTY_VALUE_TIMEOUT + " = " + configuration.getProperty(PROPERTY_VALUE_TIMEOUT) : "") + NEW_LINE +
(configuration.getProperty(PROPERTY_VALUE_JVM_ARGS) != null ?
" " + PROPERTY_VALUE_JVM_ARGS + " = " + configuration.getProperty(PROPERTY_VALUE_JVM_ARGS) : "") + NEW_LINE +
(testClass != null ? " " + OPT_TARGET_TESTS + "['" + ctTypeToFullQualifiedName(testClass) + "']": "") + NEW_LINE +
(configuration.getProperty(PROPERTY_ADDITIONAL_CP_ELEMENTS) != null ?
" " + OPT_ADDITIONAL_CP_ELEMENTS + "['" + configuration.getProperty(PROPERTY_ADDITIONAL_CP_ELEMENTS) + "']":"") + NEW_LINE +
(descartesMode ? " " + OPT_MUTATION_ENGINE + NEW_LINE + " " + getDescartesMutators() :
" " + OPT_MUTATORS + (evosuiteMode ?
VALUE_MUTATORS_EVOSUITE : VALUE_MUTATORS_ALL)) + NEW_LINE +
(configuration.getProperty(PROPERTY_EXCLUDED_CLASSES) != null ?
" " + OPT_EXCLUDED_CLASSES + "['" + configuration.getProperty(PROPERTY_EXCLUDED_CLASSES) + "']":"") + NEW_LINE +
"}" + NEW_LINE;
}
示例12: testPitDescartesMode
import spoon.reflect.declaration.CtType; //导入依赖的package包/类
@Test
public void testPitDescartesMode() throws Exception {
assertFalse(MavenPitCommandAndOptions.descartesMode);
MavenPitCommandAndOptions.descartesMode = true;
InputConfiguration configuration = new InputConfiguration("src/test/resources/descartes/descartes.properties");
DSpot dspot = new DSpot(configuration, 1,
new PitMutantScoreSelector("src/test/resources/descartes/mutations.csv"));
final CtClass<Object> originalTestClass = dspot.getInputProgram().getFactory().Class().get("fr.inria.stamp.mutationtest.test.TestCalculator");
assertEquals(2, originalTestClass.getMethods().size());
final CtType ctType = dspot.amplifyTest(
"fr.inria.stamp.mutationtest.test.TestCalculator",
Collections.singletonList("Integraltypestest")
);
// assertTrue(originalTestClass.getMethods().size() < ctType.getMethods().size()); // TODO
FileUtils.cleanDirectory(new File(configuration.getOutputDirectory()));
assertTrue(MavenPitCommandAndOptions.descartesMode);
}
示例13: test
import spoon.reflect.declaration.CtType; //导入依赖的package包/类
@Test
public void test() throws Exception {
try {
FileUtils.deleteDirectory(new File("target/trash"));
} catch (Exception ignored) {
//ignored
}
AmplificationHelper.setSeedRandom(23L);
InputConfiguration configuration = new InputConfiguration("src/test/resources/test-projects/test-projects.properties");
DSpot dspot = new DSpot(configuration, 1, Collections.singletonList(new StatementAdd()),
new TakeAllSelector());
assertEquals(6, dspot.getInputProgram().getFactory().Class().get("example.TestSuiteExample").getMethods().size());
final CtType<?> amplifiedTest = dspot.amplifyTest("example.TestSuiteExample", Collections.singletonList("test2"));
assertEquals(2, amplifiedTest.getMethods().size());
}
示例14: test
import spoon.reflect.declaration.CtType; //导入依赖的package包/类
@Test
public void test() throws Exception {
final String configurationPath = "src/test/resources/regression/test-projects_0/test-projects.properties";
final ChangeDetectorSelector changeDetectorSelector = new ChangeDetectorSelector();
final InputConfiguration configuration = new InputConfiguration(configurationPath);
final DSpot dSpot = new DSpot(configuration, 1,
Collections.singletonList(new StatementAdd()),
changeDetectorSelector);
assertEquals(6, dSpot.getInputProgram().getFactory().Type().get("example.TestSuiteExample").getMethods().size());
final CtType<?> ctType = dSpot.amplifyTest("example.TestSuiteExample").get(0); // TODO
assertFalse(ctType.getMethods().isEmpty());// TODO this is not deterministic.
// TODO We verify that DSpot has been able to detect the chagnes between the two version
// TODO at least with one amplified test, i.e. the list of method returned amplified test is not empty
}
示例15: test
import spoon.reflect.declaration.CtType; //导入依赖的package包/类
@Test
public void test() throws Exception {
/*
This selector aims at keeping amplified test that execute new lines in the source code.
*/
Utils.reset();
Utils.init("src/test/resources/test-projects/test-projects.properties");
EntryPoint.verbose = true;
final DSpot dspot = new DSpot(Utils.getInputConfiguration(),
1,
Arrays.asList(new Amplifier[]{new TestDataMutator(), new StatementAdd()}),
new CloverCoverageSelector()
);
final CtType ctType = dspot.amplifyTest(Utils.findClass("example.TestSuiteExample"),
Collections.singletonList(Utils.findMethod("example.TestSuiteExample", "test2"))
);
assertFalse(ctType.getMethodsByName("test2_literalMutationNumber9").isEmpty());
EntryPoint.verbose = false;
}