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

WiP: feature Substitution#insertType #1357

Closed
wants to merge 8 commits into from
Closed
68 changes: 60 additions & 8 deletions src/main/java/spoon/support/template/Parameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import spoon.reflect.code.CtLiteral;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtTypeMember;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtFieldReference;
Expand All @@ -36,6 +37,7 @@
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -69,9 +71,8 @@ public static Integer getIndex(CtExpression<?> e) {
* Gets a template field parameter value.
*/
public static Object getValue(Template<?> template, String parameterName, Integer index) {
Object tparamValue = null;
Field rtField = null;
try {
Field rtField = null;
for (Field f : RtHelper.getAllFields(template.getClass())) {
if (isParameterSource(f)) {
if (parameterName.equals(getParameterName(f))) {
Expand All @@ -80,6 +81,20 @@ public static Object getValue(Template<?> template, String parameterName, Intege
}
}
}
} catch (Exception e) {
throw new UndefinedParameterException(e);
}
Object tparamValue = getValue(template, parameterName, rtField);
if (rtField.getType().isArray() && (index != null)) {
tparamValue = ((Object[]) tparamValue)[index];
}
return tparamValue;
}
private static Object getValue(Template<?> template, String parameterName, Field rtField) {
if (rtField == null) {
throw new UndefinedParameterException();
}
try {
if (Modifier.isFinal(rtField.getModifiers())) {
Map<String, Object> m = finals.get(template);
if (m == null) {
Expand All @@ -88,14 +103,10 @@ public static Object getValue(Template<?> template, String parameterName, Intege
return m.get(parameterName);
}
rtField.setAccessible(true);
tparamValue = rtField.get(template);
if (rtField.getType().isArray() && (index != null)) {
tparamValue = ((Object[]) tparamValue)[index];
}
return rtField.get(template);
} catch (Exception e) {
throw new UndefinedParameterException();
throw new UndefinedParameterException(e);
}
return tparamValue;
}

static Map<Template<?>, Map<String, Object>> finals = new HashMap<>();
Expand Down Expand Up @@ -194,6 +205,47 @@ public static List<String> getNames(CtClass<? extends Template<?>> templateType)
}
return params;
}
/**
* 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;
}

/**
* Gets the Map of names to template parameter values for all the template parameters of a given template type
* + adds mapping of template model reference to target type as parameter too
* @param f
* the factory
* @param targetType
* the target type of the substitution (can be null), which will be done with result parameters
* @param template
* the template that holds the parameter values
*/
public static Map<String, Object> getTemplateParametersAsMap(Factory f, CtType<?> targetType, Template<?> template) {
Map<String, Object> params = new HashMap<>(Parameters.getNamesToValues(template, (CtClass) f.Class().get(template.getClass())));
if (targetType != null) {
/*
* there is required to replace all template model references by target type reference.
* Handle that request as template parameter too
*/
params.put(template.getClass().getSimpleName(), targetType.getReference());
}
return params;
}

/**
* Tells if a given field is a template parameter.
Expand Down
Loading