Skip to content

Commit 1e0bf49

Browse files
committed
Backported generics/varargs signature refinements for scripting package
1 parent 0ea4f8e commit 1e0bf49

File tree

8 files changed

+117
-84
lines changed

8 files changed

+117
-84
lines changed

spring-context/src/main/java/org/springframework/scripting/ScriptFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,7 +49,7 @@ public interface ScriptFactory {
4949
* its Java interfaces (such as in the case of Groovy).
5050
* @return the interfaces for the script
5151
*/
52-
Class[] getScriptInterfaces();
52+
Class<?>[] getScriptInterfaces();
5353

5454
/**
5555
* Return whether the script requires a config interface to be
@@ -75,7 +75,7 @@ public interface ScriptFactory {
7575
* @throws IOException if script retrieval failed
7676
* @throws ScriptCompilationException if script compilation failed
7777
*/
78-
Object getScriptedObject(ScriptSource scriptSource, Class[] actualInterfaces)
78+
Object getScriptedObject(ScriptSource scriptSource, Class<?>... actualInterfaces)
7979
throws IOException, ScriptCompilationException;
8080

8181
/**
@@ -91,7 +91,7 @@ Object getScriptedObject(ScriptSource scriptSource, Class[] actualInterfaces)
9191
* @throws ScriptCompilationException if script compilation failed
9292
* @since 2.0.3
9393
*/
94-
Class getScriptedObjectType(ScriptSource scriptSource)
94+
Class<?> getScriptedObjectType(ScriptSource scriptSource)
9595
throws IOException, ScriptCompilationException;
9696

9797
/**

spring-context/src/main/java/org/springframework/scripting/bsh/BshScriptFactory.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,11 +45,11 @@ public class BshScriptFactory implements ScriptFactory, BeanClassLoaderAware {
4545

4646
private final String scriptSourceLocator;
4747

48-
private final Class[] scriptInterfaces;
48+
private final Class<?>[] scriptInterfaces;
4949

5050
private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
5151

52-
private Class scriptClass;
52+
private Class<?> scriptClass;
5353

5454
private final Object scriptClassMonitor = new Object();
5555

@@ -64,7 +64,9 @@ public class BshScriptFactory implements ScriptFactory, BeanClassLoaderAware {
6464
* Interpreted by the post-processor that actually creates the script.
6565
*/
6666
public BshScriptFactory(String scriptSourceLocator) {
67-
this(scriptSourceLocator, null);
67+
Assert.hasText(scriptSourceLocator, "'scriptSourceLocator' must not be empty");
68+
this.scriptSourceLocator = scriptSourceLocator;
69+
this.scriptInterfaces = null;
6870
}
6971

7072
/**
@@ -78,12 +80,13 @@ public BshScriptFactory(String scriptSourceLocator) {
7880
* @param scriptInterfaces the Java interfaces that the scripted object
7981
* is supposed to implement (may be {@code null})
8082
*/
81-
public BshScriptFactory(String scriptSourceLocator, Class[] scriptInterfaces) {
83+
public BshScriptFactory(String scriptSourceLocator, Class<?>... scriptInterfaces) {
8284
Assert.hasText(scriptSourceLocator, "'scriptSourceLocator' must not be empty");
8385
this.scriptSourceLocator = scriptSourceLocator;
8486
this.scriptInterfaces = scriptInterfaces;
8587
}
8688

89+
8790
public void setBeanClassLoader(ClassLoader classLoader) {
8891
this.beanClassLoader = classLoader;
8992
}
@@ -93,7 +96,7 @@ public String getScriptSourceLocator() {
9396
return this.scriptSourceLocator;
9497
}
9598

96-
public Class[] getScriptInterfaces() {
99+
public Class<?>[] getScriptInterfaces() {
97100
return this.scriptInterfaces;
98101
}
99102

@@ -108,11 +111,11 @@ public boolean requiresConfigInterface() {
108111
* Load and parse the BeanShell script via {@link BshScriptUtils}.
109112
* @see BshScriptUtils#createBshObject(String, Class[], ClassLoader)
110113
*/
111-
public Object getScriptedObject(ScriptSource scriptSource, Class[] actualInterfaces)
114+
public Object getScriptedObject(ScriptSource scriptSource, Class<?>... actualInterfaces)
112115
throws IOException, ScriptCompilationException {
113116

114117
try {
115-
Class clazz = null;
118+
Class<?> clazz;
116119

117120
synchronized (this.scriptClassMonitor) {
118121
boolean requiresScriptEvaluation = (this.wasModifiedForTypeCheck && this.scriptClass == null);
@@ -125,7 +128,7 @@ public Object getScriptedObject(ScriptSource scriptSource, Class[] actualInterfa
125128
if (result instanceof Class) {
126129
// A Class: We'll cache the Class here and create an instance
127130
// outside of the synchronized block.
128-
this.scriptClass = (Class) result;
131+
this.scriptClass = (Class<?>) result;
129132
}
130133
else {
131134
// Not a Class: OK, we'll simply create BeanShell objects
@@ -159,7 +162,7 @@ public Object getScriptedObject(ScriptSource scriptSource, Class[] actualInterfa
159162
}
160163
}
161164

162-
public Class getScriptedObjectType(ScriptSource scriptSource)
165+
public Class<?> getScriptedObjectType(ScriptSource scriptSource)
163166
throws IOException, ScriptCompilationException {
164167

165168
try {

spring-context/src/main/java/org/springframework/scripting/bsh/BshScriptUtils.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -66,7 +66,7 @@ public static Object createBshObject(String scriptSource) throws EvalError {
6666
* @throws EvalError in case of BeanShell parsing failure
6767
* @see #createBshObject(String, Class[], ClassLoader)
6868
*/
69-
public static Object createBshObject(String scriptSource, Class[] scriptInterfaces) throws EvalError {
69+
public static Object createBshObject(String scriptSource, Class<?>... scriptInterfaces) throws EvalError {
7070
return createBshObject(scriptSource, scriptInterfaces, ClassUtils.getDefaultClassLoader());
7171
}
7272

@@ -80,16 +80,16 @@ public static Object createBshObject(String scriptSource, Class[] scriptInterfac
8080
* @param scriptInterfaces the interfaces that the scripted Java object is
8181
* supposed to implement (may be {@code null} or empty if the script itself
8282
* declares a full class or returns an actual instance of the scripted object)
83-
* @param classLoader the ClassLoader to create the script proxy with
83+
* @param classLoader the ClassLoader to use for evaluating the script
8484
* @return the scripted Java object
8585
* @throws EvalError in case of BeanShell parsing failure
8686
*/
87-
public static Object createBshObject(String scriptSource, Class[] scriptInterfaces, ClassLoader classLoader)
87+
public static Object createBshObject(String scriptSource, Class<?>[] scriptInterfaces, ClassLoader classLoader)
8888
throws EvalError {
8989

9090
Object result = evaluateBshScript(scriptSource, scriptInterfaces, classLoader);
9191
if (result instanceof Class) {
92-
Class clazz = (Class) result;
92+
Class<?> clazz = (Class<?>) result;
9393
try {
9494
return clazz.newInstance();
9595
}
@@ -113,12 +113,12 @@ public static Object createBshObject(String scriptSource, Class[] scriptInterfac
113113
* @return the scripted Java class, or {@code null} if none could be determined
114114
* @throws EvalError in case of BeanShell parsing failure
115115
*/
116-
static Class determineBshObjectType(String scriptSource) throws EvalError {
116+
static Class<?> determineBshObjectType(String scriptSource) throws EvalError {
117117
Assert.hasText(scriptSource, "Script source must not be empty");
118118
Interpreter interpreter = new Interpreter();
119119
Object result = interpreter.eval(scriptSource);
120120
if (result instanceof Class) {
121-
return (Class) result;
121+
return (Class<?>) result;
122122
}
123123
else if (result != null) {
124124
return result.getClass();
@@ -139,11 +139,11 @@ else if (result != null) {
139139
* @param scriptInterfaces the interfaces that the scripted Java object is
140140
* supposed to implement (may be {@code null} or empty if the script itself
141141
* declares a full class or returns an actual instance of the scripted object)
142-
* @param classLoader the ClassLoader to create the script proxy with
142+
* @param classLoader the ClassLoader to use for evaluating the script
143143
* @return the scripted Java class or Java object
144144
* @throws EvalError in case of BeanShell parsing failure
145145
*/
146-
static Object evaluateBshScript(String scriptSource, Class[] scriptInterfaces, ClassLoader classLoader)
146+
static Object evaluateBshScript(String scriptSource, Class<?>[] scriptInterfaces, ClassLoader classLoader)
147147
throws EvalError {
148148

149149
Assert.hasText(scriptSource, "Script source must not be empty");

spring-context/src/main/java/org/springframework/scripting/groovy/GroovyScriptFactory.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -58,9 +58,9 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea
5858

5959
private GroovyClassLoader groovyClassLoader;
6060

61-
private Class scriptClass;
61+
private Class<?> scriptClass;
6262

63-
private Class scriptResultClass;
63+
private Class<?> scriptResultClass;
6464

6565
private CachedResultHolder cachedResult;
6666

@@ -131,7 +131,7 @@ public String getScriptSourceLocator() {
131131
* hence we don't need to explicitly expose interfaces here.
132132
* @return {@code null} always
133133
*/
134-
public Class[] getScriptInterfaces() {
134+
public Class<?>[] getScriptInterfaces() {
135135
return null;
136136
}
137137

@@ -148,11 +148,11 @@ public boolean requiresConfigInterface() {
148148
* Loads and parses the Groovy script via the GroovyClassLoader.
149149
* @see groovy.lang.GroovyClassLoader
150150
*/
151-
public Object getScriptedObject(ScriptSource scriptSource, Class[] actualInterfaces)
151+
public Object getScriptedObject(ScriptSource scriptSource, Class<?>... actualInterfaces)
152152
throws IOException, ScriptCompilationException {
153153

154154
try {
155-
Class scriptClassToExecute = null;
155+
Class<?> scriptClassToExecute;
156156

157157
synchronized (this.scriptClassMonitor) {
158158
this.wasModifiedForTypeCheck = false;
@@ -189,7 +189,7 @@ public Object getScriptedObject(ScriptSource scriptSource, Class[] actualInterfa
189189
}
190190
}
191191

192-
public Class getScriptedObjectType(ScriptSource scriptSource)
192+
public Class<?> getScriptedObjectType(ScriptSource scriptSource)
193193
throws IOException, ScriptCompilationException {
194194

195195
try {
@@ -233,7 +233,7 @@ public boolean requiresScriptedObjectRefresh(ScriptSource scriptSource) {
233233
* or the result of running the script instance)
234234
* @throws ScriptCompilationException in case of instantiation failure
235235
*/
236-
protected Object executeScript(ScriptSource scriptSource, Class scriptClass) throws ScriptCompilationException {
236+
protected Object executeScript(ScriptSource scriptSource, Class<?> scriptClass) throws ScriptCompilationException {
237237
try {
238238
GroovyObject goo = (GroovyObject) scriptClass.newInstance();
239239

spring-context/src/main/java/org/springframework/scripting/jruby/JRubyScriptFactory.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,7 +47,7 @@ public class JRubyScriptFactory implements ScriptFactory, BeanClassLoaderAware {
4747

4848
private final String scriptSourceLocator;
4949

50-
private final Class[] scriptInterfaces;
50+
private final Class<?>[] scriptInterfaces;
5151

5252
private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
5353

@@ -59,7 +59,7 @@ public class JRubyScriptFactory implements ScriptFactory, BeanClassLoaderAware {
5959
* @param scriptInterfaces the Java interfaces that the scripted object
6060
* is supposed to implement
6161
*/
62-
public JRubyScriptFactory(String scriptSourceLocator, Class[] scriptInterfaces) {
62+
public JRubyScriptFactory(String scriptSourceLocator, Class<?>... scriptInterfaces) {
6363
Assert.hasText(scriptSourceLocator, "'scriptSourceLocator' must not be empty");
6464
Assert.notEmpty(scriptInterfaces, "'scriptInterfaces' must not be empty");
6565
this.scriptSourceLocator = scriptSourceLocator;
@@ -76,7 +76,7 @@ public String getScriptSourceLocator() {
7676
return this.scriptSourceLocator;
7777
}
7878

79-
public Class[] getScriptInterfaces() {
79+
public Class<?>[] getScriptInterfaces() {
8080
return this.scriptInterfaces;
8181
}
8282

@@ -91,7 +91,7 @@ public boolean requiresConfigInterface() {
9191
* Load and parse the JRuby script via JRubyScriptUtils.
9292
* @see JRubyScriptUtils#createJRubyObject(String, Class[], ClassLoader)
9393
*/
94-
public Object getScriptedObject(ScriptSource scriptSource, Class[] actualInterfaces)
94+
public Object getScriptedObject(ScriptSource scriptSource, Class<?>... actualInterfaces)
9595
throws IOException, ScriptCompilationException {
9696
try {
9797
return JRubyScriptUtils.createJRubyObject(
@@ -108,7 +108,7 @@ public Object getScriptedObject(ScriptSource scriptSource, Class[] actualInterfa
108108
}
109109
}
110110

111-
public Class getScriptedObjectType(ScriptSource scriptSource)
111+
public Class<?> getScriptedObjectType(ScriptSource scriptSource)
112112
throws IOException, ScriptCompilationException {
113113

114114
return null;

spring-context/src/main/java/org/springframework/scripting/jruby/JRubyScriptUtils.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -63,7 +63,7 @@ public abstract class JRubyScriptUtils {
6363
* @throws JumpException in case of JRuby parsing failure
6464
* @see ClassUtils#getDefaultClassLoader()
6565
*/
66-
public static Object createJRubyObject(String scriptSource, Class[] interfaces) throws JumpException {
66+
public static Object createJRubyObject(String scriptSource, Class<?>... interfaces) throws JumpException {
6767
return createJRubyObject(scriptSource, interfaces, ClassUtils.getDefaultClassLoader());
6868
}
6969

@@ -76,7 +76,7 @@ public static Object createJRubyObject(String scriptSource, Class[] interfaces)
7676
* @throws JumpException in case of JRuby parsing failure
7777
*/
7878
@SuppressWarnings("deprecation")
79-
public static Object createJRubyObject(String scriptSource, Class[] interfaces, ClassLoader classLoader) {
79+
public static Object createJRubyObject(String scriptSource, Class<?>[] interfaces, ClassLoader classLoader) {
8080
Ruby ruby = initializeRuntime();
8181

8282
Node scriptRootNode = ruby.parseEval(scriptSource, "", null, 0);
@@ -132,16 +132,16 @@ private static ClassNode findClassNode(Node node) {
132132
}
133133
else if (child instanceof NewlineNode) {
134134
NewlineNode nn = (NewlineNode) child;
135-
Node found = findClassNode(nn.getNextNode());
136-
if (found instanceof ClassNode) {
137-
return (ClassNode) found;
135+
ClassNode found = findClassNode(nn.getNextNode());
136+
if (found != null) {
137+
return found;
138138
}
139139
}
140140
}
141141
for (Node child : children) {
142-
Node found = findClassNode(child);
143-
if (found instanceof ClassNode) {
144-
return (ClassNode) found;
142+
ClassNode found = findClassNode(child);
143+
if (found != null) {
144+
return found;
145145
}
146146
}
147147
return null;
@@ -207,16 +207,16 @@ private IRubyObject[] convertToRuby(Object[] javaArgs) {
207207
return rubyArgs;
208208
}
209209

210-
private Object convertFromRuby(IRubyObject rubyResult, Class returnType) {
210+
private Object convertFromRuby(IRubyObject rubyResult, Class<?> returnType) {
211211
Object result = JavaEmbedUtils.rubyToJava(this.ruby, rubyResult, returnType);
212212
if (result instanceof RubyArray && returnType.isArray()) {
213213
result = convertFromRubyArray(((RubyArray) result).toJavaArray(), returnType);
214214
}
215215
return result;
216216
}
217217

218-
private Object convertFromRubyArray(IRubyObject[] rubyArray, Class returnType) {
219-
Class targetType = returnType.getComponentType();
218+
private Object convertFromRubyArray(IRubyObject[] rubyArray, Class<?> returnType) {
219+
Class<?> targetType = returnType.getComponentType();
220220
Object javaArray = Array.newInstance(targetType, rubyArray.length);
221221
for (int i = 0; i < rubyArray.length; i++) {
222222
IRubyObject rubyObject = rubyArray[i];

0 commit comments

Comments
 (0)