-
Notifications
You must be signed in to change notification settings - Fork 831
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #859 from daspilker/deprecation-ast
use AST transformation to log deprecation warnings
- Loading branch information
Showing
19 changed files
with
117 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
job-dsl-core/src/ast/groovy/javaposse/jobdsl/dsl/transform/ASTTransformationHelper.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package javaposse.jobdsl.dsl.transform | ||
|
||
import org.codehaus.groovy.ast.ClassNode | ||
import org.codehaus.groovy.ast.expr.VariableExpression | ||
import org.codehaus.groovy.control.SourceUnit | ||
import org.codehaus.groovy.syntax.CSTNode | ||
|
||
class ASTTransformationHelper { | ||
static VariableExpression getJobManagementVariable(SourceUnit sourceUnit, ClassNode clazz, CSTNode context) { | ||
if (!clazz.getField('jobManagement') && !clazz.getField('jm')) { | ||
sourceUnit.errorCollector.addError("no jobManagement field in $clazz", context, sourceUnit) | ||
} | ||
new VariableExpression(clazz.getField('jobManagement') ? 'jobManagement' : 'jm') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
.../src/ast/groovy/javaposse/jobdsl/dsl/transform/DeprecationWarningASTTransformation.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package javaposse.jobdsl.dsl.transform | ||
|
||
import org.codehaus.groovy.ast.ASTNode | ||
import org.codehaus.groovy.ast.AnnotationNode | ||
import org.codehaus.groovy.ast.ClassHelper | ||
import org.codehaus.groovy.ast.ClassNode | ||
import org.codehaus.groovy.ast.MethodNode | ||
import org.codehaus.groovy.ast.expr.ArgumentListExpression | ||
import org.codehaus.groovy.ast.expr.ConstantExpression | ||
import org.codehaus.groovy.ast.expr.MethodCallExpression | ||
import org.codehaus.groovy.ast.expr.VariableExpression | ||
import org.codehaus.groovy.ast.stmt.BlockStatement | ||
import org.codehaus.groovy.ast.stmt.ExpressionStatement | ||
import org.codehaus.groovy.control.CompilePhase | ||
import org.codehaus.groovy.control.SourceUnit | ||
import org.codehaus.groovy.syntax.Token | ||
import org.codehaus.groovy.transform.ASTTransformation | ||
import org.codehaus.groovy.transform.GroovyASTTransformation | ||
|
||
/** | ||
* Global AST transformation for logging deprecation warnings. | ||
* | ||
* Each method in a class implementing <code>javaposse.jobdsl.dsl.Context</code> annotated by {@link Deprecated} is | ||
* supplemented with a method call to <code>jobManagement.logDeprecationWarning()</code>. The method must have access to | ||
* a <code>jobManagement</code> field. | ||
*/ | ||
@GroovyASTTransformation(phase = CompilePhase.SEMANTIC_ANALYSIS) | ||
class DeprecationWarningASTTransformation implements ASTTransformation { | ||
private static final ClassNode DEPRECATED_ANNOTATION = ClassHelper.make('java.lang.Deprecated') | ||
private static final ClassNode CONTEXT_CLASS = ClassHelper.make('javaposse.jobdsl.dsl.Context') | ||
|
||
@Override | ||
void visit(ASTNode[] nodes, SourceUnit sourceUnit) { | ||
List<ClassNode> classes = sourceUnit.AST.classes.findAll { | ||
!it.interface && it.implementsInterface(CONTEXT_CLASS) | ||
} | ||
classes.methods.flatten().each { MethodNode method -> | ||
List<AnnotationNode> annotations = method.getAnnotations(DEPRECATED_ANNOTATION) | ||
if (annotations) { | ||
VariableExpression jobManagementVariable = ASTTransformationHelper.getJobManagementVariable( | ||
sourceUnit, | ||
method.declaringClass, | ||
Token.newString(annotations[0].text, annotations[0].lineNumber, annotations[0].columnNumber) | ||
) | ||
|
||
((BlockStatement) method.code).statements.add(0, new ExpressionStatement(new MethodCallExpression( | ||
jobManagementVariable, | ||
new ConstantExpression('logDeprecationWarning'), | ||
new ArgumentListExpression() | ||
))) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...-core/src/ast/resources/META-INF/services/org.codehaus.groovy.transform.ASTTransformation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
javaposse.jobdsl.dsl.transform.ContextASTTransformation | ||
javaposse.jobdsl.dsl.transform.PluginASTTransformation | ||
javaposse.jobdsl.dsl.transform.CoreVersionASTTransformation | ||
javaposse.jobdsl.dsl.transform.DeprecationWarningASTTransformation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.