From a3c8ffb236024468306f1a254989e83d9a054bfc Mon Sep 17 00:00:00 2001
From: Pavel Vojtechovsky
Date: Mon, 21 Nov 2016 21:17:58 +0100
Subject: [PATCH 1/4] refactor(isParameterSource): make code more readable
---
.../spoon/support/template/Parameters.java | 31 ++++++++++++-------
1 file changed, 20 insertions(+), 11 deletions(-)
diff --git a/src/main/java/spoon/support/template/Parameters.java b/src/main/java/spoon/support/template/Parameters.java
index 392eed04f81..edd41771e57 100644
--- a/src/main/java/spoon/support/template/Parameters.java
+++ b/src/main/java/spoon/support/template/Parameters.java
@@ -200,18 +200,27 @@ public static Collection 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
+ if (ref.getDeclaration().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 , 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;
}
/**
From 2c84242227534d040e48ae966097926d6d708e3b Mon Sep 17 00:00:00 2001
From: Pavel Vojtechovsky
Date: Mon, 21 Nov 2016 21:21:46 +0100
Subject: [PATCH 2/4] fix(isParameterSource): we must always have a source
---
.../java/spoon/support/template/Parameters.java | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/src/main/java/spoon/support/template/Parameters.java b/src/main/java/spoon/support/template/Parameters.java
index edd41771e57..4f3ba5008d0 100644
--- a/src/main/java/spoon/support/template/Parameters.java
+++ b/src/main/java/spoon/support/template/Parameters.java
@@ -201,12 +201,13 @@ public static Collection getNames(CtClass extends Template>> templat
*/
public static boolean isParameterSource(CtFieldReference> ref) {
CtField> field = ref.getDeclaration();
- if (field != null) {
- // we must have the source of this fieldref
- if (ref.getDeclaration().getAnnotation(Parameter.class) != null) {
- //it is the template field which represents template parameter, because of "Parameter" annotation
- return true;
- }
+ 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 , are not template parameters
From a259f7d4cd5e9e25fd1709920ccd0e439f37267d Mon Sep 17 00:00:00 2001
From: Pavel Vojtechovsky
Date: Mon, 21 Nov 2016 21:24:54 +0100
Subject: [PATCH 3/4] fix exception handling
Why we should hide this exception and let it continue?
---
src/main/java/spoon/support/template/Parameters.java | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/main/java/spoon/support/template/Parameters.java b/src/main/java/spoon/support/template/Parameters.java
index 4f3ba5008d0..6f85634661b 100644
--- a/src/main/java/spoon/support/template/Parameters.java
+++ b/src/main/java/spoon/support/template/Parameters.java
@@ -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;
@@ -191,7 +191,7 @@ public static Collection getNames(CtClass extends Template>> templat
}
}
} catch (Exception e) {
- Launcher.LOGGER.error(e.getMessage(), e);
+ throw new SpoonException("Getting of template parameters failed", e);
}
return params;
}
From e809fadacee4992391dfd10a035f0b731df7e780 Mon Sep 17 00:00:00 2001
From: Pavel Vojtechovsky
Date: Mon, 21 Nov 2016 21:31:10 +0100
Subject: [PATCH 4/4] refactor(getTemplateNameParameters) make it simpler
---
src/main/java/spoon/support/template/Parameters.java | 5 ++---
src/main/java/spoon/template/TemplateMatcher.java | 5 +----
2 files changed, 3 insertions(+), 7 deletions(-)
diff --git a/src/main/java/spoon/support/template/Parameters.java b/src/main/java/spoon/support/template/Parameters.java
index 6f85634661b..fdb14e13742 100644
--- a/src/main/java/spoon/support/template/Parameters.java
+++ b/src/main/java/spoon/support/template/Parameters.java
@@ -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;
@@ -182,8 +181,8 @@ 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 getNames(CtClass extends Template>> templateType) {
- Collection params = new ArrayList<>();
+ public static List getNames(CtClass extends Template>> templateType) {
+ List params = new ArrayList<>();
try {
for (CtFieldReference> f : templateType.getReference().getAllFields()) {
if (isParameterSource(f)) {
diff --git a/src/main/java/spoon/template/TemplateMatcher.java b/src/main/java/spoon/template/TemplateMatcher.java
index c8aa9fec19b..47add010da3 100644
--- a/src/main/java/spoon/template/TemplateMatcher.java
+++ b/src/main/java/spoon/template/TemplateMatcher.java
@@ -68,10 +68,7 @@ private List> getMethods(CtClass extends Template>> root) {
}
private List getTemplateNameParameters(CtClass extends Template>> templateType) {
- final List ts = new ArrayList<>();
- final Collection c = Parameters.getNames(templateType);
- ts.addAll(c);
- return ts;
+ return Parameters.getNames(templateType);
}
private List> getTemplateTypeParameters(final CtClass extends Template>> templateType) {