use of spoon.SpoonException in project spoon by INRIA.
the class Parameters method getNamesToValues.
/**
* Gets the Map of names to template parameter value for all the template parameters of a given template type
* (including the ones defined by the super types).
*/
public static Map<String, Object> getNamesToValues(Template<?> template, CtClass<? extends Template<?>> templateType) {
// use linked hash map to assure same order of parameter names. There are cases during substitution of parameters when substitution order matters. E.g. SubstitutionVisitor#substituteName(...)
Map<String, Object> params = new LinkedHashMap<>();
try {
for (CtFieldReference<?> f : templateType.getAllFields()) {
if (isParameterSource(f)) {
String parameterName = getParameterName(f);
params.put(parameterName, getValue(template, parameterName, (Field) f.getActualField()));
}
}
} catch (Exception e) {
throw new SpoonException("Getting of template parameters failed", e);
}
return params;
}
use of spoon.SpoonException in project spoon by INRIA.
the class TemplateTest method substituteSubString.
@Test
public void substituteSubString() throws Exception {
// contract: the substitution of substrings works on named elements and references too
Launcher spoon = new Launcher();
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/SubStringTemplate.java"));
spoon.buildModel();
Factory factory = spoon.getFactory();
{
// contract: String value is substituted in substring of literal, named element and reference
final CtClass<?> result = (CtClass<?>) new SubStringTemplate("A").apply(factory.createClass());
assertEquals("java.lang.String m_A = \"A is here more times: A\";", result.getField("m_A").toString());
// contract: the parameter of type string replaces substring in method name
CtMethod<?> method1 = result.getMethodsByName("setA").get(0);
assertEquals("setA", method1.getSimpleName());
assertEquals("java.lang.String p_A", method1.getParameters().get(0).toString());
assertEquals("this.m_A = p_A", method1.getBody().getStatement(0).toString());
assertEquals("setA(\"The A is here too\")", result.getMethodsByName("m").get(0).getBody().getStatements().get(0).toString());
}
{
// contract: Type value name is substituted in substring of literal, named element and reference
final CtClass<?> result = (CtClass<?>) new SubStringTemplate(factory.Type().OBJECT.getTypeDeclaration()).apply(factory.createClass());
assertEquals("java.lang.String m_Object = \"Object is here more times: Object\";", result.getField("m_Object").toString());
// contract: the parameter of type string replaces substring in method name
CtMethod<?> method1 = result.getMethodsByName("setObject").get(0);
assertEquals("setObject", method1.getSimpleName());
assertEquals("java.lang.String p_Object", method1.getParameters().get(0).toString());
assertEquals("this.m_Object = p_Object", method1.getBody().getStatement(0).toString());
assertEquals("setObject(\"The Object is here too\")", result.getMethodsByName("m").get(0).getBody().getStatements().get(0).toString());
}
{
// contract: Type reference value name is substituted in substring of literal, named element and reference
final CtClass<?> result = (CtClass<?>) new SubStringTemplate(factory.Type().OBJECT).apply(factory.createClass());
assertEquals("java.lang.String m_Object = \"Object is here more times: Object\";", result.getField("m_Object").toString());
// contract: the parameter of type string replaces substring in method name
CtMethod<?> method1 = result.getMethodsByName("setObject").get(0);
assertEquals("setObject", method1.getSimpleName());
assertEquals("java.lang.String p_Object", method1.getParameters().get(0).toString());
assertEquals("this.m_Object = p_Object", method1.getBody().getStatement(0).toString());
assertEquals("setObject(\"The Object is here too\")", result.getMethodsByName("m").get(0).getBody().getStatements().get(0).toString());
}
{
// contract: String literal value name is substituted in substring of literal, named element and reference
final CtClass<?> result = (CtClass<?>) new SubStringTemplate(factory.createLiteral("Xxx")).apply(factory.createClass());
assertEquals("java.lang.String m_Xxx = \"Xxx is here more times: Xxx\";", result.getField("m_Xxx").toString());
// contract: the parameter of type string replaces substring in method name
CtMethod<?> method1 = result.getMethodsByName("setXxx").get(0);
assertEquals("setXxx", method1.getSimpleName());
assertEquals("java.lang.String p_Xxx", method1.getParameters().get(0).toString());
assertEquals("this.m_Xxx = p_Xxx", method1.getBody().getStatement(0).toString());
assertEquals("setXxx(\"The Xxx is here too\")", result.getMethodsByName("m").get(0).getBody().getStatements().get(0).toString());
}
{
// contract: The elements which cannot be converted to String should throw exception
SubStringTemplate template = new SubStringTemplate(factory.createSwitch());
try {
template.apply(factory.createClass());
fail();
} catch (SpoonException e) {
// OK
}
}
}
use of spoon.SpoonException in project spoon by INRIA.
the class MetamodelProperty method getItemValueType.
private static CtTypeReference<?> getItemValueType(CtTypeReference<?> valueType) {
ContainerKind valueContainerType = containerKindOf(valueType.getActualClass());
if (valueContainerType == ContainerKind.SINGLE) {
return null;
}
CtTypeReference<?> itemValueType;
if (valueContainerType == ContainerKind.MAP) {
if (String.class.getName().equals(valueType.getActualTypeArguments().get(0).getQualifiedName()) == false) {
throw new SpoonException("Unexpected container of type: " + valueType.toString());
}
itemValueType = valueType.getActualTypeArguments().get(1);
} else {
// List or Set
itemValueType = valueType.getActualTypeArguments().get(0);
}
if (itemValueType instanceof CtTypeParameterReference) {
itemValueType = ((CtTypeParameterReference) itemValueType).getBoundingType();
if (itemValueType == null) {
itemValueType = valueType.getFactory().Type().OBJECT;
}
}
return itemValueType;
}
use of spoon.SpoonException in project spoon by INRIA.
the class MetamodelProperty method getIdxOfBestMatchByReturnType.
private int getIdxOfBestMatchByReturnType(List<MMMethod> methods, MMMethodKind key) {
if (methods.size() > 2) {
throw new SpoonException("Resolving of more then 2 conflicting getters is not supported. There are: " + methods.toString());
}
// There is no input parameter. We are resolving getter field.
// choose the getter whose return value is a collection
// of second one
CtTypeReference<?> returnType1 = methods.get(0).getMethod().getType();
CtTypeReference<?> returnType2 = methods.get(1).getMethod().getType();
Factory f = returnType1.getFactory();
boolean is1Iterable = returnType1.isSubtypeOf(f.Type().ITERABLE);
boolean is2Iterable = returnType2.isSubtypeOf(f.Type().ITERABLE);
if (is1Iterable != is2Iterable) {
// they are not some. Only one of them is iterable
if (is1Iterable) {
if (isIterableOf(returnType1, returnType2)) {
// representation of 2nd method
return 0;
}
} else {
if (isIterableOf(returnType2, returnType1)) {
// representation of 1st method
return 1;
}
}
}
// else report ambiguity
return -1;
}
use of spoon.SpoonException in project spoon by INRIA.
the class SpoonMetaModel method initializeConcept.
/**
* is called once for each {@link MetamodelConcept}, to initialize it.
* @param type a class or inteface of the spoon model element
* @param mmConcept to be initialize {@link MetamodelConcept}
*/
private void initializeConcept(CtType<?> type, MetamodelConcept mmConcept) {
// it is not initialized yet. Do it now
if (type instanceof CtInterface<?>) {
CtInterface<?> iface = (CtInterface<?>) type;
mmConcept.setModelClass(getImplementationOfInterface(iface));
mmConcept.setModelInterface(iface);
} else if (type instanceof CtClass<?>) {
CtClass<?> clazz = (CtClass<?>) type;
mmConcept.setModelClass(clazz);
mmConcept.setModelInterface(getInterfaceOfImplementation(clazz));
} else {
throw new SpoonException("Unexpected spoon model type: " + type.getQualifiedName());
}
// add fields of class
if (mmConcept.getModelClass() != null) {
addFieldsOfType(mmConcept, mmConcept.getModelClass());
}
// add fields of interface
if (mmConcept.getModelInterface() != null) {
// add fields of interface too. They are not added by above call of addFieldsOfType, because the MetamodelConcept already exists in nameToConcept
addFieldsOfType(mmConcept, mmConcept.getModelInterface());
}
// initialize all fields
mmConcept.getRoleToProperty().forEach((role, mmField) -> {
// if there are more methods for the same field then choose the one which best matches the field type
mmField.sortByBestMatch();
// finally initialize value type of this field
mmField.setValueType(mmField.detectValueType());
});
}
Aggregations