spoon.reflect.code.CtComment Java Examples

The following examples show how to use spoon.reflect.code.CtComment. 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 File: DiffCasesTest.java    From coming with MIT License 6 votes vote down vote up
@Test
public void testDiff1Comment() throws Exception {
	File s = getFile("diffcases/diffcomment1/1205753_EmbedPooledConnection_0_s.java");
	File t = getFile("diffcases/diffcomment1/1205753_EmbedPooledConnection_0_t.java");
	FineGrainDifftAnalyzer r = new FineGrainDifftAnalyzer();
	boolean includeComments = true;
	Diff diffOut = r.getDiff(s, t, includeComments);
	System.out.println("Output: " + diffOut);
	Assert.assertEquals(1, diffOut.getRootOperations().size());
	Operation op = diffOut.getRootOperations().get(0);
	Assert.assertTrue(op.getSrcNode().getComments().size() > 0);

	List<Operation> allop = diffOut.getAllOperations();
	boolean hasComment = false;
	for (Operation operation : allop) {
		hasComment = hasComment || (operation.getSrcNode() instanceof CtComment);
	}
	assertTrue(hasComment);

}
 
Example #2
Source File: DiffCasesTest.java    From coming with MIT License 6 votes vote down vote up
@Test
public void testDiff2Comment() throws Exception {
	File s = getFile("diffcases/diffcomment2/1205753_EmbedPooledConnection_0_s.java");
	File t = getFile("diffcases/diffcomment2/1205753_EmbedPooledConnection_0_t.java");
	FineGrainDifftAnalyzer r = new FineGrainDifftAnalyzer();
	boolean includeComments = true;
	Diff diffOut = r.getDiff(s, t, includeComments);
	System.out.println("Output: " + diffOut);
	Assert.assertEquals(1, diffOut.getRootOperations().size());

	List<Operation> allop = diffOut.getAllOperations();
	boolean hasComment = false;
	for (Operation operation : allop) {
		hasComment = hasComment || (operation.getSrcNode() instanceof CtComment);
	}
	assertTrue(hasComment);

}
 
Example #3
Source File: DiffCasesTest.java    From coming with MIT License 6 votes vote down vote up
@Test
public void testDiff3Comment() throws Exception {
	File s = getFile("diffcases/diffcomment3/RectangularCholeskyDecomposition_s.java");
	File t = getFile("diffcases/diffcomment3/RectangularCholeskyDecomposition_t.java");
	FineGrainDifftAnalyzer r = new FineGrainDifftAnalyzer();
	boolean includeComments = true;
	Diff diffOut = r.getDiff(s, t, includeComments);
	System.out.println("Output: " + diffOut);
	Assert.assertEquals(1, diffOut.getRootOperations().size());
	Operation op = diffOut.getRootOperations().get(0);
	Assert.assertTrue(op.getSrcNode().getComments().size() > 0);

	assertFalse(op.getSrcNode() instanceof CtComment);

	List<Operation> allop = diffOut.getAllOperations();
	boolean hasComment = false;
	for (Operation operation : allop) {
		if ((operation.getSrcNode() instanceof CtComment)) {
			hasComment = true;
			System.out.println(operation.getSrcNode());
		}
	}
	assertTrue(hasComment);

}
 
Example #4
Source File: AstComparatorTest.java    From gumtree-spoon-ast-diff with Apache License 2.0 6 votes vote down vote up
@Test
public void testDiff1Comment() throws Exception {
	File s = new File("src/test/resources/examples/diffcomment1/1205753_EmbedPooledConnection_0_s.java");
	File t = new File("src/test/resources/examples/diffcomment1/1205753_EmbedPooledConnection_0_t.java");
	boolean includeComments = true;
	AstComparator r = new AstComparator(includeComments);
	Diff diffOut = r.compare(s, t);
	System.out.println("Output: " + diffOut);
	Assert.assertEquals(1, diffOut.getRootOperations().size());
	Operation op = diffOut.getRootOperations().get(0);
	Assert.assertTrue(op.getSrcNode().getComments().size() > 0);

	List<Operation> allop = diffOut.getAllOperations();
	boolean hasComment = false;
	for (Operation operation : allop) {
		hasComment = hasComment || (operation.getSrcNode() instanceof CtComment);
	}
	assertTrue(hasComment);

}
 
Example #5
Source File: AstComparatorTest.java    From gumtree-spoon-ast-diff with Apache License 2.0 6 votes vote down vote up
@Test
public void testDiff2Comment() throws Exception {
	File s = new File("src/test/resources/examples/diffcomment2/1205753_EmbedPooledConnection_0_s.java");
	File t = new File("src/test/resources/examples/diffcomment2/1205753_EmbedPooledConnection_0_t.java");
	boolean includeComments = true;
	AstComparator r = new AstComparator(includeComments);

	Diff diffOut = r.compare(s, t);
	System.out.println("Output: " + diffOut);
	Assert.assertEquals(1, diffOut.getRootOperations().size());
	Operation op = diffOut.getRootOperations().get(0);
	// Assert.assertTrue(op.getSrcNode().getComments().size() > 0);

	List<Operation> allop = diffOut.getAllOperations();
	boolean hasComment = false;
	for (Operation operation : allop) {
		hasComment = hasComment || (operation.getSrcNode() instanceof CtComment);
	}
	assertTrue(hasComment);

}
 
Example #6
Source File: AstComparatorTest.java    From gumtree-spoon-ast-diff with Apache License 2.0 6 votes vote down vote up
@Test
public void testDiff3Comment() throws Exception {
	File s = new File("src/test/resources/examples/diffcomment3/RectangularCholeskyDecomposition_s.java");
	File t = new File("src/test/resources/examples/diffcomment3/RectangularCholeskyDecomposition_t.java");
	boolean includeComments = true;
	AstComparator r = new AstComparator(includeComments);

	Diff diffOut = r.compare(s, t);
	System.out.println("Output: " + diffOut);
	Assert.assertEquals(1, diffOut.getRootOperations().size());
	Operation op = diffOut.getRootOperations().get(0);
	Assert.assertTrue(op.getSrcNode().getComments().size() > 0);

	assertFalse(op.getSrcNode() instanceof CtComment);

	List<Operation> allop = diffOut.getAllOperations();
	boolean hasComment = false;
	for (Operation operation : allop) {
		if ((operation.getSrcNode() instanceof CtComment)) {
			hasComment = true;
			System.out.println(operation.getSrcNode());
		}
	}
	assertTrue(hasComment);

}
 
Example #7
Source File: APICheckingProcessor.java    From spoon-examples with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void process(CtMethod method) {
    final Factory factory = method.getFactory();

    CtBlock methodBody = method.getBody();

    List<CtComment> bodyComments = new ArrayList<>();

    ArrayList<CtStatement> ctStatements = new ArrayList<>(methodBody.getStatements());
    for (CtStatement ctStatement : ctStatements) {
        String statement = ctStatement.toString();
        bodyComments.add(factory.createInlineComment(statement));
        methodBody.removeStatement(ctStatement);
    }

    CtClass<? extends Throwable> myExceptionClass = factory.Class().get(EXCEPTION_FQN);
    CtConstructorCall<? extends Throwable> myNewException = factory.createConstructorCall(myExceptionClass.getReference());

    CtThrow throwMyException = factory.createThrow();
    throwMyException.setThrownExpression(myNewException);
    methodBody.addStatement(throwMyException);

    bodyComments.add(factory.createInlineComment("FIXME: The private API type should never be return in a public API."));

    for (CtComment bodyComment : bodyComments) {
        throwMyException.addComment(bodyComment);
    }
}
 
Example #8
Source File: MainComingTest.java    From coming with MIT License 5 votes vote down vote up
@Test
public void testIncludeCommentMain() throws Exception {

	File left = getFile("diffcases/diffcomment1/1205753_EmbedPooledConnection_0_s.java");
	File right = getFile("diffcases/diffcomment1/1205753_EmbedPooledConnection_0_t.java");

	ComingMain cm = new ComingMain();
	// With comments
	FinalResult result = cm.run(new String[] { "-location",
			left.getAbsolutePath() + File.pathSeparator + right.getAbsolutePath(), "-input", "filespair",
			"-entitytype", "BinaryOperator", "-action", "INS", "-processcomments", "true" });

	assertNotNull(result);

	assertNotNull(result.values().size() > 0);

	RevisionResult rr = (RevisionResult) result.values().stream().findFirst().get();
	DiffResult<IRevision, Diff> diff = (DiffResult) rr.get(FineGrainDifftAnalyzer.class.getSimpleName());

	Assert.assertTrue(diff.getAll().size() > 0);

	Diff diffOut = diff.getAll().get(0);

	Assert.assertEquals(1, diffOut.getRootOperations().size());
	Operation op = diffOut.getRootOperations().get(0);
	Assert.assertTrue(op.getSrcNode().getComments().size() > 0);

	List<Operation> allop = diffOut.getAllOperations();
	boolean hasComment = false;
	for (Operation operation : allop) {
		hasComment = hasComment || (operation.getSrcNode() instanceof CtComment);
	}
	assertTrue(hasComment);

}
 
Example #9
Source File: MainComingTest.java    From coming with MIT License 5 votes vote down vote up
@Test
public void testNotIncludeCommentMain() throws Exception {

	File left = getFile("diffcases/diffcomment1/1205753_EmbedPooledConnection_0_s.java");
	File right = getFile("diffcases/diffcomment1/1205753_EmbedPooledConnection_0_t.java");

	ComingMain cm = new ComingMain();
	// With comments
	FinalResult result = cm.run(new String[] { "-location",
			left.getAbsolutePath() + File.pathSeparator + right.getAbsolutePath(), "-input", "filespair",
			"-entitytype", "BinaryOperator", "-action", "INS", "-processcomments", "false" });

	assertNotNull(result);

	assertNotNull(result.values().size() > 0);

	RevisionResult rr = (RevisionResult) result.values().stream().findFirst().get();
	DiffResult<IRevision, Diff> diff = (DiffResult) rr.get(FineGrainDifftAnalyzer.class.getSimpleName());

	Assert.assertTrue(diff.getAll().size() > 0);

	Diff diffOut = diff.getAll().get(0);

	Operation op = diffOut.getRootOperations().get(0);
	Assert.assertTrue(op.getSrcNode().getComments().isEmpty());

	List<Operation> allop = diffOut.getAllOperations();
	boolean hasComment = false;
	for (Operation operation : allop) {
		hasComment = hasComment || (operation.getSrcNode() instanceof CtComment);
	}
	assertFalse(hasComment);

}
 
Example #10
Source File: BigTransfoScenarioTest.java    From spoon-examples with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("all")
@Test
public void main() {
    MavenLauncher launcher = new MavenLauncher(
            "./src/test/resources/project/",
            MavenLauncher.SOURCE_TYPE.APP_SOURCE);

    CtModel model = launcher.buildModel();
    List<CtMethod> methodList = model.
            filterChildren(new NamedElementFilter<CtPackage>(CtPackage.class, "ow2con")).
            filterChildren(new NamedElementFilter<CtPackage>(CtPackage.class, "publicapi")).
            filterChildren(new TypeFilter<CtMethod>(CtMethod.class)).
            filterChildren(new Filter<CtMethod>() {
                @Override
                public boolean matches(CtMethod element) {
                    boolean isPublic = element.isPublic();
                    CtTypeReference returnType = element.getType();
                    String privateApiPackage = "ow2con.privateapi";
                    boolean isTypeFromPrivateApi = returnType.getQualifiedName().contains(privateApiPackage);
                    return isPublic && isTypeFromPrivateApi;
                }
            }).list();

    Factory factory = launcher.getFactory();
    CtClass<? extends Throwable> exceptionClass = factory.createClass("ow2con.PrivateAPIException");
    CtConstructorCall<? extends Throwable> exceptionInstance = factory.createConstructorCall(exceptionClass.getReference());

    for (CtMethod method : methodList) {
        CtBlock methodBody = method.getBody();
        List<CtComment> bodyComments = new ArrayList<>();

        ArrayList<CtStatement> ctStatements = new ArrayList<>(methodBody.getStatements());

        for (CtStatement ctStatement : ctStatements) {
            String statement = ctStatement.toString();
            CtComment statementAsComment = factory.createInlineComment(statement);
            bodyComments.add(statementAsComment);
            methodBody.removeStatement(ctStatement);
        }

        CtThrow throwMyException = factory.createThrow();
        CtConstructorCall<? extends Throwable> constructorCall = exceptionInstance.clone();
        throwMyException.setThrownExpression(constructorCall);
        methodBody.addStatement(throwMyException);

        bodyComments.add(
                factory.createInlineComment(
                "FIXME: The private API type should never be return in a public API."
                )
        );

        for (CtComment bodyComment : bodyComments) {
            throwMyException.addComment(bodyComment);
        }
    }

    Environment environment = launcher.getEnvironment();
    environment.setCommentEnabled(true);
    environment.setAutoImports(true);
    // the transformation must produce compilable code
    environment.setShouldCompile(true);
    launcher.prettyprint();

    // look in folder spooned/ow2con/publicapi/ the transformed code
}
 
Example #11
Source File: SecAnalysis.java    From coming with MIT License 4 votes vote down vote up
public static boolean newCommentInPrevious(CtComment comment, Diff diff) {

		boolean existInsert = false;
		boolean existDetete = false;

		for (Operation op : diff.getAllOperations()) {

			if (op.getNode() == comment && op instanceof InsertOperation)
				existInsert = true;

			if (op instanceof DeleteOperation && op.getNode() instanceof CtComment
					&& (op.getNode() == comment || op.getSrcNode().toString().equals(comment.toString())))
				existDetete = true;

		}

		return existInsert && !existDetete;
	}
 
Example #12
Source File: LabelFinder.java    From gumtree-spoon-ast-diff with Apache License 2.0 4 votes vote down vote up
@Override
public void visitCtComment(CtComment comment) {
	label = comment.getContent();
}
 
Example #13
Source File: SecAnalysis.java    From coming with MIT License 3 votes vote down vote up
public static String getOpInComment(CtComment comment, Diff diff) {

		for (Operation op : diff.getAllOperations()) {

			if (op.getNode() == comment) {
				return op.getAction().getName();

			}

		}

		return "unchanged";
	}