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

fix: Pattern can match a Type #2555

Merged
merged 2 commits into from
Sep 27, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
import spoon.pattern.internal.node.RootNode;
import spoon.pattern.internal.node.StringNode;
import spoon.pattern.internal.parameter.AbstractParameterInfo;
import spoon.pattern.internal.parameter.ComputedParameterInfo;
import spoon.pattern.internal.parameter.ListParameterInfo;
import spoon.pattern.internal.parameter.MapParameterInfo;
import spoon.pattern.internal.parameter.ParameterInfo;
import spoon.pattern.internal.parameter.SimpleNameOfTypeReferenceParameterComputer;
import spoon.reflect.code.CtArrayAccess;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtExpression;
Expand Down Expand Up @@ -232,7 +234,9 @@ public PatternParameterConfigurator byType(CtTypeReference<?> type) {
CtType<?> type2 = queryModel().filterChildren((CtType<?> t) -> t.getQualifiedName().equals(typeQName)).first();
if (type2 != null) {
//Substitute name of template too
addSubstitutionRequest(pi, type2, CtRole.NAME);
ComputedParameterInfo piName = new ComputedParameterInfo(SimpleNameOfTypeReferenceParameterComputer.INSTANCE, pi);
piName.setParameterValueType(String.class);
addSubstitutionRequest(piName, type2, CtRole.NAME);
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright (C) 2006-2018 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.pattern.internal.parameter;

import java.util.List;
import java.util.function.Function;

import spoon.SpoonException;
import spoon.pattern.internal.ResultHolder;
import spoon.reflect.factory.Factory;
import spoon.support.util.ImmutableMap;

/**
* Represents a parameter which is related to a value of another parameter.
* For example parameter which represents CtTypeReference has a value `abc.sample.AType`
* And computed parameter which represents name of type referenced by CtTypeReference
* has a computed String value `AType`
*/
public class ComputedParameterInfo extends AbstractParameterInfo {

private final ParameterComputer computer;

public ComputedParameterInfo(ParameterComputer computer, ParameterInfo next) {
super(next);
this.computer = computer;
}

@Override
protected String getPlainName() {
return getWrappedName(getContainerName());
}

@Override
protected String getWrappedName(String containerName) {
return containerName + "$" + computer.getName();
}

@Override
protected Object addValueAs(Object container, Function<Object, Object> merger) {
//do not try to match on computed value
return container;
}

@Override
protected List<Object> getEmptyContainer() {
throw new SpoonException("ComputedParameterInfo#getEmptyContainer should not be used");
}
@Override
public <T> void getValueAs(Factory factory, ResultHolder<T> result, ImmutableMap parameters) {
ResultHolder<?> inputHolder = computer.createInputHolder();
super.getValueAs(factory, inputHolder, parameters);
computer.computeValue((ResultHolder) result, inputHolder);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (C) 2006-2018 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.pattern.internal.parameter;

import spoon.pattern.internal.ResultHolder;

/**
* Computes a value of {@link ComputedParameterInfo}
* I - type of input value
* O - type of computed value
*/
public interface ParameterComputer {

/**
* @return user friendly name of this computer
*/
String getName();

/**
* @return holder for input value
*/
ResultHolder<?> createInputHolder();

/**
* @param outputHolder holds result of computation
* @param inputHolder holds input of computation
*/
void computeValue(ResultHolder<Object> outputHolder, ResultHolder<?> inputHolder);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright (C) 2006-2018 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.pattern.internal.parameter;

import spoon.pattern.internal.ResultHolder;
import spoon.reflect.reference.CtTypeReference;

/**
* a {@link ParameterComputer} which computes simpleName of {@link CtTypeReference}
*/
public class SimpleNameOfTypeReferenceParameterComputer implements ParameterComputer {

public static final SimpleNameOfTypeReferenceParameterComputer INSTANCE = new SimpleNameOfTypeReferenceParameterComputer();

@Override
public String getName() {
return "simpleName";
}

@Override
public ResultHolder<?> createInputHolder() {
return new ResultHolder.Single<>(CtTypeReference.class);
}

@Override
public void computeValue(ResultHolder<Object> outputHolder, ResultHolder<?> inputHolder) {
String name = null;
CtTypeReference<?> typeRef = ((ResultHolder.Single<CtTypeReference<?>>) inputHolder).getResult();
if (typeRef != null) {
name = typeRef.getSimpleName();
}
outputHolder.addResult(name);
}
}
32 changes: 32 additions & 0 deletions src/test/java/spoon/test/template/PatternTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,38 @@ public void testExtensionDecoupledSubstitutionVisitor() {
assertTrue(aTry.getBody().getStatements().size() > 1);
}

@Test
public void testMatchType() {
//contract: one can match a type
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] {"--output-type", "nooutput" });
launcher.addInputResource("./src/test/java/spoon/test/template/testclasses/logger/Logger.java");

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

//create a template class
final CtClass<?> aTemplateType = launcher.getFactory().Class().create("a.template.Clazz");
//create a pattern which should match that class
Pattern pattern = PatternBuilder.create(aTemplateType)
.configurePatternParameters(pb -> {
pb.parameter("members").byRole(CtRole.TYPE_MEMBER, e -> e == aTemplateType);
pb.parameter("modifiers").byRole(CtRole.MODIFIER, e -> e == aTemplateType);
}).build();

final CtClass<?> aTargetType = launcher.getFactory().Class().get(Logger.class);
List<Match> matches = pattern.getMatches(aTargetType);
assertEquals(1, matches.size());
Match match = matches.get(0);
assertSame(aTargetType, match.getMatchingElement());
List<CtTypeMember> expectedTypeMembers = aTargetType.getTypeMembers();
List<CtTypeMember> typeMembers = (List<CtTypeMember>) match.getParameters().getValue("members");
assertEquals(expectedTypeMembers.size(), typeMembers.size());
for (int i = 0; i < expectedTypeMembers.size(); i++) {
assertSame(expectedTypeMembers.get(i), typeMembers.get(i));
}
}

private Map<String, Object> getMap(Match match, String name) {
Object v = match.getParametersMap().get(name);
assertNotNull(v);
Expand Down