Java Code Examples for spoon.reflect.cu.SourcePosition

The following examples show how to use spoon.reflect.cu.SourcePosition. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: nopol   Source File: DelegatingProcessor.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * @see spoon.processing.AbstractProcessor#isToBeProcessed(spoon.reflect.declaration.CtElement)
 */
@Override
public boolean isToBeProcessed(final CtStatement candidate) {
    boolean isPracticable = this.predicate.apply(candidate);
    if (isPracticable) {
        SourcePosition position = candidate.getPosition();
        if (position == null || position == SourcePosition.NOPOSITION) {
            return false;
        }
        boolean isSameFile = false;
        boolean isSameLine = position.getLine() == this.line;
        try {
            File f1 = position.getFile().getCanonicalFile().getAbsoluteFile();
            File f2 = file.getCanonicalFile();
            isSameFile = f1.getAbsolutePath().equals(f2.getAbsolutePath());
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
        isPracticable = this.process && isSameLine && isSameFile;
    }
    return isPracticable;
}
 
Example 2
Source Project: nopol   Source File: DumbFaultLocalizerImpl.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Map<SourceLocation, List<TestResult>> getTestListPerStatement() {
	SpoonedProject spooner = new SpoonedProject(nopolContext.getProjectSources(), nopolContext);
	final List<SourcePosition> l = new ArrayList<>();
	spooner.process(new AbstractProcessor<CtIf>(){
		@Override
		public void process(CtIf ctIf) {
			l.add(ctIf.getCondition().getPosition());
		}
	});

	Map<SourceLocation, List<TestResult>> countPerSourceLocation = new HashMap<>();

	List<TestResult> res = new ArrayList<>();

	for (String testClass : nopolContext.getProjectTests()) {
		try {
			URLClassLoader urlClassLoader = new URLClassLoader(nopolContext.getProjectClasspath(), this.getClass().getClassLoader());
			Class klass = urlClassLoader.loadClass(testClass);

			// does not work, see https://stackoverflow.com/a/29865611
			//for (FrameworkMethod desc : new BlockJUnit4ClassRunner(klass).getChildren()) {

			// so we get the methods ourselves
			// only support basic Junit4
			for (String m : getTestMethods(klass)) {
				res.add(new TestResultImpl(TestCase.from(m), false));
			}
	} catch (Exception e) {
			System.out.println(testClass);
		}
	}
	for(SourcePosition pos : l) {
		SourceLocation loc = new SourceLocation(pos.getCompilationUnit().getMainType().getQualifiedName(), pos.getLine());
		countPerSourceLocation.put(loc, Collections.unmodifiableList(res));
	}
	return countPerSourceLocation;
}
 
Example 3
Source Project: nopol   Source File: BeforeLocationFilter.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean matches(T element) {
    SourcePosition elementPosition = element.getPosition();
    if (elementPosition != null && elementPosition != SourcePosition.NOPOSITION && onTheSameFile(elementPosition)) {
        int distance = position().getLine() - elementPosition.getLine();
        if (distance >= 0) {
            return distance > 0 || position().getColumn() > elementPosition.getColumn();
        }
    }
    return false;
}
 
Example 4
Source Project: nopol   Source File: CodeSnippetFilter.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean matches(CtElement element) {
    SourcePosition position = element.getPosition();
    if (position != null && !(position instanceof NoSourcePosition)) {
        return FileLibrary.isSameFile(classSourceFile(), position.getFile()) && codeSnippet().equals(element.toString());
    }
    return false;
}
 
Example 5
Source Project: nopol   Source File: NoPol.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void runOnStatement(SourceLocation sourceLocation, List<TestResult> tests) {
	logger.debug("Analysing {} which is executed by {} tests", sourceLocation, tests.size());
	SpoonedClass spoonCl = spooner.forked(sourceLocation.getRootClassName());
	if (spoonCl == null || spoonCl.getSimpleType() == null) {
		logger.debug("cannot spoon "+sourceLocation.toString());
		return;
	}
	System.out.println(spoonCl.getSimpleType().hashCode());
	NopolProcessorBuilder builder = new NopolProcessorBuilder(spoonCl.getSimpleType().getPosition().getFile(), sourceLocation.getLineNumber(), nopolContext);

	// here, we only collect the processors to be applied later
	// this does not change the class itself
	spoonCl.process(builder);

	final List<NopolProcessor> nopolProcessors = builder.getNopolProcessors();
	for (NopolProcessor nopolProcessor : nopolProcessors) {
		logger.debug("looking with "+nopolProcessor.getClass().toString());

		SourcePosition position = nopolProcessor.getTarget().getPosition();
		sourceLocation.setSourceStart(position.getSourceStart());
		sourceLocation.setSourceEnd(position.getSourceEnd());

		List<Patch> patches = executeNopolProcessor(tests, sourceLocation, spoonCl, nopolProcessor);
		this.nopolResult.addPatches(patches);

		if (nopolContext.isOnlyOneSynthesisResult() && !patches.isEmpty()) {
			return;
		}
	}
}
 
Example 6
Source Project: nopol   Source File: NopolProcessorBuilder.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean isToBeProcessed(CtStatement candidate) {
    SourcePosition position = candidate.getPosition();
    if (position == null || position instanceof NoSourcePosition) {
        return false;
    }
    if (!new LineFilter().matches(candidate)) {
        return false;
    }
    boolean isSameFile = FileLibrary.isSameFile(file, position.getFile());
    boolean isSameLine = position.getLine() == this.line;
    return isSameLine && isSameFile && super.isToBeProcessed(candidate);
}
 
Example 7
Source Project: astor   Source File: SpoonElementPointer.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void process(CtElement element) {
	
	SourcePosition pos = element.getPosition();
	if (pos != null && pos.isValidPosition() && pos.getLine() == line) {
		inLine.add(element);
	}
}
 
Example 8
Source Project: astor   Source File: MutationSupporter.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Save on disk Source code file Should be configured before:
 * .getSpoonClassCompiler().updateOutput(srcOutput);
 * 
 * @param type
 * @return
 */
public void generateSourceCodeFromCtClass(CtType<?> type) {
	// WorkArround, for cloned
	SourcePosition sp = type.getPosition();
	type.setPosition(null);

	if (output == null || output.getJavaPrinter() == null) {
		throw new IllegalArgumentException("Spoon compiler must be initialized");
	}
	output.saveSourceCode((CtClass) type);
	// --
	// End Workarround
	type.setPosition(sp);

}
 
Example 9
Source Project: gumtree-spoon-ast-diff   Source File: TreeTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test_bug_Possition() throws Exception {
	AstComparator comparator = new AstComparator();
	File fl = new File("src/test/resources/examples/roots/test8/left_QuickNotepad_1.13.java");
	File fr = new File("src/test/resources/examples/roots/test8/right_QuickNotepad_1.14.java");

	CtType<?> astLeft = comparator.getCtType(fl);

	assertNotNull(astLeft);

	CtType<?> astRight = comparator.getCtType(fr);
	assertNotNull(astRight);

	Diff diffResult = comparator.compare(astLeft, astRight);
	List<Operation> rootOperations = diffResult.getRootOperations();
	getPaths(rootOperations);
	List<Operation> allOperations = diffResult.getAllOperations();
	getPaths(allOperations);

	assertEquals(1, rootOperations.size());

	SourcePosition position = rootOperations.get(0).getSrcNode().getPosition();
	assertTrue(position.getLine() > 0);
	assertEquals(113, position.getLine());

	assertTrue(!(position instanceof NoSourcePosition));

}
 
Example 10
Source Project: nopol   Source File: BeforeLocationFilter.java    License: GNU General Public License v2.0 4 votes vote down vote up
public BeforeLocationFilter(Class<T> theClass, SourcePosition position) {
    super(theClass, position);
}
 
Example 11
Source Project: nopol   Source File: LocationFilter.java    License: GNU General Public License v2.0 4 votes vote down vote up
public LocationFilter(Class<T> theClass, SourcePosition position) {
    super(theClass);
    this.position = position;
}
 
Example 12
Source Project: nopol   Source File: LocationFilter.java    License: GNU General Public License v2.0 4 votes vote down vote up
protected SourcePosition position() {
    return position;
}
 
Example 13
Source Project: nopol   Source File: LocationFilter.java    License: GNU General Public License v2.0 4 votes vote down vote up
public boolean onTheSameFile(SourcePosition otherPosition) {
    return FileLibrary.isSameFile(position().getFile(), otherPosition.getFile());
}
 
Example 14
Source Project: nopol   Source File: PatchGenerator.java    License: GNU General Public License v2.0 4 votes vote down vote up
private String getSubstring(String classContent, CtElement element) {
	SourcePosition position = element.getPosition();
	return classContent.substring(position.getSourceStart(), position.getSourceEnd() + 1);
}
 
Example 15
Source Project: nopol   Source File: While.java    License: GNU General Public License v2.0 4 votes vote down vote up
public SourcePosition position() {
    return astLoop().getPosition();
}
 
Example 16
Source Project: gumtree-spoon-ast-diff   Source File: TreeTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void test_bug_Possition_from_String() {
	String c1 = "" + "class X {\n" + "public void foo() {\n" + " int x = 0;\n" + "}" + "};";

	String c2 = "" + "class X {\n" + "public void foo() {\n" + " int x = 1;\n" + "}" + "};";

	AstComparator diff = new AstComparator();
	Diff editScript = diff.compare(c1, c2);
	assertTrue(editScript.getRootOperations().size() == 1);

	List<Operation> rootOperations = editScript.getRootOperations();

	assertEquals(1, rootOperations.size());

	SourcePosition position = rootOperations.get(0).getSrcNode().getPosition();
	assertTrue(!(position instanceof NoSourcePosition));

	assertTrue(position.getLine() > 0);
	assertEquals(3, position.getLine());

}