Java Code Examples for org.codehaus.groovy.ast.ClassNode#getDeclaredConstructor()
The following examples show how to use
org.codehaus.groovy.ast.ClassNode#getDeclaredConstructor() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NotYetImplementedASTTransformation.java From groovy with Apache License 2.0 | 6 votes |
public void visit(ASTNode[] nodes, SourceUnit source) { init(nodes, source); AnnotationNode anno = (AnnotationNode) nodes[0]; MethodNode methodNode = (MethodNode) nodes[1]; ClassNode exception = getMemberClassValue(anno, "exception"); if (exception == null) { exception = DEFAULT_THROW_TYPE; } ConstructorNode cons = exception.getDeclaredConstructor(new Parameter[]{new Parameter(ClassHelper.STRING_TYPE, "dummy")}); if (cons == null) { addError("Error during @NotYetImplemented processing: supplied exception " + exception.getNameWithoutPackage() + " doesn't have expected String constructor", methodNode); } if (methodNode.getCode() instanceof BlockStatement && !methodNode.getCode().isEmpty()) { // wrap code in try/catch with return for failure path followed by throws for success path TryCatchStatement tryCatchStatement = tryCatchS(methodNode.getCode()); tryCatchStatement.addCatch(catchS(param(CATCH_TYPE, "ignore"), ReturnStatement.RETURN_NULL_OR_VOID)); ThrowStatement throwStatement = throwS(ctorX(exception, args(constX("Method is marked with @NotYetImplemented but passes unexpectedly")))); methodNode.setCode(block(tryCatchStatement, throwStatement)); } }
Example 2
Source File: BaseScriptASTTransformation.java From groovy with Apache License 2.0 | 4 votes |
private void changeBaseScriptType(final AnnotatedNode parent, final ClassNode cNode, final ClassNode baseScriptType) { if (!cNode.isScriptBody()) { addError("Annotation " + MY_TYPE_NAME + " can only be used within a Script.", parent); return; } if (!baseScriptType.isScript()) { addError("Declared type " + baseScriptType + " does not extend groovy.lang.Script class!", parent); return; } cNode.setSuperClass(baseScriptType); // Method in base script that will contain the script body code. MethodNode runScriptMethod = ClassHelper.findSAM(baseScriptType); // If they want to use a name other than than "run", then make the change. if (isCustomScriptBodyMethod(runScriptMethod)) { MethodNode defaultMethod = cNode.getDeclaredMethod("run", Parameter.EMPTY_ARRAY); // GROOVY-6706: Sometimes an NPE is thrown here. // The reason is that our transform is getting called more than once sometimes. if (defaultMethod != null) { cNode.removeMethod(defaultMethod); MethodNode methodNode = new MethodNode(runScriptMethod.getName(), runScriptMethod.getModifiers() & ~ACC_ABSTRACT , runScriptMethod.getReturnType(), runScriptMethod.getParameters(), runScriptMethod.getExceptions() , defaultMethod.getCode()); // The AST node metadata has the flag that indicates that this method is a script body. // It may also be carrying data for other AST transforms. methodNode.copyNodeMetaData(defaultMethod); addGeneratedMethod(cNode, methodNode); } } // If the new script base class does not have a contextual constructor (g.l.Binding), then we won't either. // We have to do things this way (and rely on just default constructors) because the logic that generates // the constructors for our script class have already run. if (cNode.getSuperClass().getDeclaredConstructor(CONTEXT_CTOR_PARAMETERS) == null) { ConstructorNode orphanedConstructor = cNode.getDeclaredConstructor(CONTEXT_CTOR_PARAMETERS); cNode.removeConstructor(orphanedConstructor); } }
Example 3
Source File: ASTTransformer.java From pom-manipulation-ext with Apache License 2.0 | 4 votes |
private void changeBaseScriptType(final AnnotatedNode parent, final ClassNode cNode, final ClassNode baseScriptType) { if (!cNode.isScriptBody()) { addError( "Annotation " + getType() + " can only be used within a Script.", parent); return; } if (!baseScriptType.isScript()) { addError("Declared type " + baseScriptType + " does not extend groovy.lang.Script class!", parent); return; } List<AnnotationNode> annotations = parent.getAnnotations( DEPRECATED_COMMAND_TYPE ); if (cNode.getAnnotations( DEPRECATED_COMMAND_TYPE ).isEmpty()) { // #388 prevent "Duplicate annotation for class" AnnotationFormatError cNode.addAnnotations(annotations); } annotations = parent.getAnnotations( COMMAND_TYPE ); if (cNode.getAnnotations( COMMAND_TYPE ).isEmpty()) { // #388 prevent "Duplicate annotation for class" AnnotationFormatError cNode.addAnnotations(annotations); } cNode.setSuperClass(baseScriptType); // Method in base script that will contain the script body code. MethodNode runScriptMethod = ClassHelper.findSAM(baseScriptType); // If they want to use a name other than than "run", then make the change. if (isCustomScriptBodyMethod(runScriptMethod)) { MethodNode defaultMethod = cNode.getDeclaredMethod("run", Parameter.EMPTY_ARRAY); // GROOVY-6706: Sometimes an NPE is thrown here. // The reason is that our transform is getting called more than once sometimes. if (defaultMethod != null) { cNode.removeMethod(defaultMethod); MethodNode methodNode = new MethodNode(runScriptMethod.getName(), runScriptMethod.getModifiers() & ~ACC_ABSTRACT , runScriptMethod.getReturnType(), runScriptMethod.getParameters(), runScriptMethod.getExceptions() , defaultMethod.getCode()); // The AST node metadata has the flag that indicates that this method is a script body. // It may also be carrying data for other AST transforms. methodNode.copyNodeMetaData(defaultMethod); addGeneratedMethod(cNode, methodNode); } } // If the new script base class does not have a contextual constructor (g.l.Binding), then we won't either. // We have to do things this way (and rely on just default constructors) because the logic that generates // the constructors for our script class have already run. if (cNode.getSuperClass().getDeclaredConstructor(CONTEXT_CTOR_PARAMETERS) == null) { ConstructorNode orphanedConstructor = cNode.getDeclaredConstructor(CONTEXT_CTOR_PARAMETERS); cNode.removeConstructor(orphanedConstructor); } }