Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
f1f36cd0
Commit
f1f36cd0
authored
Mar 30, 2014
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract closure grabber into AstUtils
parent
b71f0793
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
29 deletions
+58
-29
AstUtils.java
.../java/org/springframework/boot/cli/compiler/AstUtils.java
+57
-0
GroovyBeansTransformation.java
...ramework/boot/cli/compiler/GroovyBeansTransformation.java
+1
-29
No files found.
spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/AstUtils.java
View file @
f1f36cd0
...
...
@@ -16,6 +16,7 @@
package
org
.
springframework
.
boot
.
cli
.
compiler
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.HashSet
;
import
java.util.Set
;
...
...
@@ -25,6 +26,14 @@ import org.codehaus.groovy.ast.AnnotationNode;
import
org.codehaus.groovy.ast.ClassNode
;
import
org.codehaus.groovy.ast.FieldNode
;
import
org.codehaus.groovy.ast.MethodNode
;
import
org.codehaus.groovy.ast.expr.ArgumentListExpression
;
import
org.codehaus.groovy.ast.expr.ClosureExpression
;
import
org.codehaus.groovy.ast.expr.ConstantExpression
;
import
org.codehaus.groovy.ast.expr.Expression
;
import
org.codehaus.groovy.ast.expr.MethodCallExpression
;
import
org.codehaus.groovy.ast.stmt.BlockStatement
;
import
org.codehaus.groovy.ast.stmt.ExpressionStatement
;
import
org.codehaus.groovy.ast.stmt.Statement
;
/**
* General purpose AST utilities.
...
...
@@ -101,4 +110,52 @@ public abstract class AstUtils {
return
false
;
}
public
static
boolean
hasAtLeastOneInterface
(
ClassNode
classNode
,
String
...
types
)
{
Set
<
String
>
typesSet
=
new
HashSet
<
String
>(
Arrays
.
asList
(
types
));
for
(
ClassNode
inter
:
classNode
.
getInterfaces
())
{
if
(
typesSet
.
contains
(
inter
.
getName
()))
{
return
true
;
}
}
return
false
;
}
/**
* Extract a top-level <code>name</code> closure from inside this block if there is
* one. Removes it from the block at the same time.
* @param block a block statement (class definition)
* @return a beans Closure if one can be found, null otherwise
*/
public
static
ClosureExpression
getClosure
(
BlockStatement
block
,
String
name
,
boolean
remove
)
{
for
(
Statement
statement
:
new
ArrayList
<
Statement
>(
block
.
getStatements
()))
{
if
(
statement
instanceof
ExpressionStatement
)
{
Expression
expression
=
((
ExpressionStatement
)
statement
).
getExpression
();
if
(
expression
instanceof
MethodCallExpression
)
{
MethodCallExpression
call
=
(
MethodCallExpression
)
expression
;
Expression
methodCall
=
call
.
getMethod
();
if
(
methodCall
instanceof
ConstantExpression
)
{
ConstantExpression
method
=
(
ConstantExpression
)
methodCall
;
if
(
name
.
equals
(
method
.
getValue
()))
{
ArgumentListExpression
arguments
=
(
ArgumentListExpression
)
call
.
getArguments
();
if
(
remove
)
{
block
.
getStatements
().
remove
(
statement
);
}
ClosureExpression
closure
=
(
ClosureExpression
)
arguments
.
getExpression
(
0
);
return
closure
;
}
}
}
}
}
return
null
;
}
public
static
ClosureExpression
getClosure
(
BlockStatement
block
,
String
name
)
{
return
getClosure
(
block
,
name
,
false
);
}
}
spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyBeansTransformation.java
View file @
f1f36cd0
...
...
@@ -25,14 +25,8 @@ import org.codehaus.groovy.ast.ClassHelper;
import
org.codehaus.groovy.ast.ClassNode
;
import
org.codehaus.groovy.ast.ModuleNode
;
import
org.codehaus.groovy.ast.PropertyNode
;
import
org.codehaus.groovy.ast.expr.ArgumentListExpression
;
import
org.codehaus.groovy.ast.expr.ClosureExpression
;
import
org.codehaus.groovy.ast.expr.ConstantExpression
;
import
org.codehaus.groovy.ast.expr.Expression
;
import
org.codehaus.groovy.ast.expr.MethodCallExpression
;
import
org.codehaus.groovy.ast.stmt.BlockStatement
;
import
org.codehaus.groovy.ast.stmt.ExpressionStatement
;
import
org.codehaus.groovy.ast.stmt.Statement
;
import
org.codehaus.groovy.control.SourceUnit
;
import
org.codehaus.groovy.transform.ASTTransformation
;
...
...
@@ -105,29 +99,7 @@ public class GroovyBeansTransformation implements ASTTransformation {
* @return a beans Closure if one can be found, null otherwise
*/
private
ClosureExpression
beans
(
BlockStatement
block
)
{
for
(
Statement
statement
:
new
ArrayList
<
Statement
>(
block
.
getStatements
()))
{
if
(
statement
instanceof
ExpressionStatement
)
{
Expression
expression
=
((
ExpressionStatement
)
statement
)
.
getExpression
();
if
(
expression
instanceof
MethodCallExpression
)
{
MethodCallExpression
call
=
(
MethodCallExpression
)
expression
;
Expression
methodCall
=
call
.
getMethod
();
if
(
methodCall
instanceof
ConstantExpression
)
{
ConstantExpression
method
=
(
ConstantExpression
)
methodCall
;
if
(
BEANS
.
equals
(
method
.
getValue
()))
{
ArgumentListExpression
arguments
=
(
ArgumentListExpression
)
call
.
getArguments
();
block
.
getStatements
().
remove
(
statement
);
ClosureExpression
closure
=
(
ClosureExpression
)
arguments
.
getExpression
(
0
);
return
closure
;
}
}
}
}
}
return
null
;
return
AstUtils
.
getClosure
(
block
,
BEANS
,
true
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment