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: refactor SubstitutionVisitor. Unify conversion of template parameters #1350

Closed
Closed
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
45 changes: 37 additions & 8 deletions src/main/java/spoon/support/template/Parameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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 +70,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 +80,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 +102,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 +204,25 @@ 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;
}

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