Skip to content
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

Propose a fix to #1033 and add a new test for TemplateMatcher #1034

Merged
merged 11 commits into from
Dec 9, 2016
Merged
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
18 changes: 16 additions & 2 deletions src/main/java/spoon/template/TemplateMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package spoon.template;

import spoon.Launcher;
import spoon.SpoonException;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtFieldAccess;
import spoon.reflect.code.CtInvocation;
Expand Down Expand Up @@ -176,7 +177,7 @@ private CtElement checkListStatements(List<?> teList) {
* @return the matched elements
*/
public List<CtElement> find(final CtElement targetRoot) {
new CtScanner() {
CtScanner scanner = new CtScanner() {
@Override
public void scan(CtElement element) {
if (match(element, templateRoot)) {
Expand All @@ -185,7 +186,20 @@ public void scan(CtElement element) {
}
super.scan(element);
}
}.scan(targetRoot);
};

scanner.scan(templateRoot);
if (!finds.contains(templateRoot)) {
throw new SpoonException("TemplateMatcher was unable to find itself, it certainly indicates a bug. Please revise your template or report an issue.");
}
finds.clear();

scanner.scan(targetRoot);

// This case can occur when we are scanning the entire package for example see TemplateTest#testTemplateMatcherWithWholePackage
if (finds.contains(templateRoot)) {
finds.remove(templateRoot);
}

return finds;
}
Expand Down
50 changes: 49 additions & 1 deletion src/test/java/spoon/test/template/TemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import org.junit.Test;
import spoon.Launcher;
import spoon.compiler.SpoonResourceHelper;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtIf;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtTry;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtParameter;
Expand All @@ -18,18 +20,19 @@
import spoon.support.template.Parameters;
import spoon.template.Substitution;
import spoon.template.TemplateMatcher;
import spoon.test.template.testclasses.SecurityCheckerTemplate;

import java.io.File;
import java.io.Serializable;
import java.rmi.Remote;
import java.util.Date;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static spoon.testing.utils.ModelUtils.build;

public class TemplateTest {

Expand Down Expand Up @@ -277,4 +280,49 @@ public void testTemplateInterfaces() throws Exception {
assertTrue(superc.getSuperInterfaces().contains(factory.Type().createReference(Remote.class)));
}

@Test
public void testTemplateMatcherWithWholePackage() throws Exception {
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/template/testclasses/ContextHelper.java");
spoon.addInputResource("./src/test/java/spoon/test/template/testclasses/BServiceImpl.java");

spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/SecurityCheckerTemplate.java"));

spoon.buildModel();
Factory factory = spoon.getFactory();

CtClass<?> templateKlass = factory.Class().get(SecurityCheckerTemplate.class);
CtMethod templateMethod = (CtMethod) templateKlass.getElements(new NameFilter("matcher1")).get(0);
CtIf templateRoot = (CtIf) templateMethod.getBody().getStatement(0);
TemplateMatcher matcher = new TemplateMatcher(templateRoot);

List<CtElement> matches = matcher.find(factory.getModel().getRootPackage());

assertEquals(1, matches.size());

CtElement match = matches.get(0);

assertTrue("Match is not a if", match instanceof CtIf);

CtElement matchParent = match.getParent();

assertTrue("Match parent is not a block", matchParent instanceof CtBlock);

CtElement matchParentParent = matchParent.getParent();

assertTrue("Match grand parent is not a method", matchParentParent instanceof CtMethod);

CtMethod methodHello = (CtMethod)matchParentParent;

assertEquals("Match grand parent is not a method called hello", "hello", methodHello.getSimpleName());

CtElement methodParent = methodHello.getParent();

assertTrue("Parent of the method is not a class",methodParent instanceof CtClass);

CtClass bservice = (CtClass) methodParent;

assertEquals("Parent of the method is not a class called BServiceImpl", "BServiceImpl", bservice.getSimpleName());
}

}
30 changes: 30 additions & 0 deletions src/test/java/spoon/test/template/testclasses/BServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2006-2016 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.test.template.testclasses;

/**
* Created by urli on 08/12/2016.
*/
public class BServiceImpl {

private String bDao;

private ContextHelper contextHelper;

public void hello() {
if(!contextHelper.hasPermission("c")) {
throw new SecurityException();
}
bDao.toString();
}
}

22 changes: 22 additions & 0 deletions src/test/java/spoon/test/template/testclasses/ContextHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2006-2016 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.test.template.testclasses;

/**
* Created by urli on 08/12/2016.
*/
public class ContextHelper {

public boolean hasPermission(String value) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2006-2016 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.test.template.testclasses;

import spoon.reflect.code.CtLiteral;
import spoon.template.TemplateParameter;

/**
* Created by urli on 08/12/2016.
*/
public class SecurityCheckerTemplate {
public TemplateParameter<ContextHelper> _ctx_;
public CtLiteral<String> _p_;

public void matcher1() {
if (!_ctx_.S().hasPermission(_p_.S())) {
throw new SecurityException();
}
}
}