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

Template matcher improvements #996

Merged
merged 4 commits into from
Nov 21, 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
41 changes: 25 additions & 16 deletions src/main/java/spoon/support/template/Parameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package spoon.support.template;

import spoon.Launcher;
import spoon.SpoonException;
import spoon.reflect.code.CtArrayAccess;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtLiteral;
Expand All @@ -35,7 +35,6 @@
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -182,16 +181,16 @@ private static String getParameterName(CtFieldReference<?> f) {
* Gets the names of all the template parameters of a given template type
* (including the ones defined by the super types).
*/
public static Collection<String> getNames(CtClass<? extends Template<?>> templateType) {
Collection<String> params = new ArrayList<>();
public static List<String> getNames(CtClass<? extends Template<?>> templateType) {
List<String> params = new ArrayList<>();
try {
for (CtFieldReference<?> f : templateType.getReference().getAllFields()) {
if (isParameterSource(f)) {
params.add(getParameterName(f));
}
}
} catch (Exception e) {
Launcher.LOGGER.error(e.getMessage(), e);
throw new SpoonException("Getting of template parameters failed", e);
}
return params;
}
Expand All @@ -200,18 +199,28 @@ public static Collection<String> getNames(CtClass<? extends Template<?>> templat
* Tells if a given field is a template parameter.
*/
public static boolean isParameterSource(CtFieldReference<?> ref) {
try {
return (ref.getDeclaration() != null // we must have the source of
// this fieldref
&& ref.getDeclaration().getAnnotation(Parameter.class) != null) || (!((ref.getType() instanceof CtTypeParameterReference) || ref.getSimpleName().equals("this"))
&& getTemplateParameterType(ref.getFactory()).isSubtypeOf(ref.getType()));
} catch (RuntimeException e) {
// if (e.getCause() instanceof ClassNotFoundException) {
// return false;
// } else {
throw e;
// }
CtField<?> field = ref.getDeclaration();
if (field == null) {
// we must have the source of this fieldref, otherwise we cannot use it as template parameter
return false;
}
if (field.getAnnotation(Parameter.class) != null) {
//it is the template field which represents template parameter, because of "Parameter" annotation
return true;
}
if (ref.getType() instanceof CtTypeParameterReference) {
//the template fields, which are using generic type like <T>, are not template parameters
return false;
}
if (ref.getSimpleName().equals("this")) {
//the reference to this is not template parameter
return false;
}
if (getTemplateParameterType(ref.getFactory()).isSubtypeOf(ref.getType())) {
//the type of template field is or extends from class TemplateParameter.
return true;
}
return false;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/spoon/template/TemplateMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ private List<CtInvocation<?>> getMethods(CtClass<? extends Template<?>> root) {
}

private List<String> getTemplateNameParameters(CtClass<? extends Template<?>> templateType) {
final List<String> ts = new ArrayList<>();
final Collection<String> c = Parameters.getNames(templateType);
ts.addAll(c);
return ts;
return Parameters.getNames(templateType);
}

private List<CtTypeReference<?>> getTemplateTypeParameters(final CtClass<? extends Template<?>> templateType) {
Expand Down