Java Code Examples for org.codehaus.groovy.ast.ClassNode#isScriptBody()

The following examples show how to use org.codehaus.groovy.ast.ClassNode#isScriptBody() . 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: TemplateASTTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void call(final SourceUnit source, final GeneratorContext context, final ClassNode classNode) throws CompilationFailedException {
    if (classNode.isScriptBody()) {
        classNode.setSuperClass(ClassHelper.make(config.getBaseTemplateClass()));
        createConstructor(classNode);
        transformRunMethod(classNode, source);
        VariableScopeVisitor visitor = new VariableScopeVisitor(source);
        visitor.visitClass(classNode);
    }
}
 
Example 2
Source File: SecureASTCustomizer.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected static List<MethodNode> filterMethods(ClassNode owner) {
    List<MethodNode> result = new LinkedList<>();
    List<MethodNode> methods = owner.getMethods();
    for (MethodNode method : methods) {
        if (method.getDeclaringClass() == owner && !method.isSynthetic()) {
            if ("main".equals(method.getName()) || "run".equals(method.getName()) && owner.isScriptBody()) continue;
            result.add(method);
        }
    }
    return result;
}
 
Example 3
Source File: BaseScriptASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void changeBaseScriptTypeFromPackageOrImport(final SourceUnit source, final AnnotatedNode parent, final AnnotationNode node) {
    Expression value = node.getMember("value");
    if (!(value instanceof ClassExpression)) {
        addError("Annotation " + MY_TYPE_NAME + " member 'value' should be a class literal.", value);
        return;
    }
    List<ClassNode> classes = source.getAST().getClasses();
    for (ClassNode classNode : classes) {
        if (classNode.isScriptBody()) {
            changeBaseScriptType(parent, classNode, value.getType());
        }
    }
}
 
Example 4
Source File: ASTTransformer.java    From pom-manipulation-ext with Apache License 2.0 5 votes vote down vote up
private void changeBaseScriptTypeFromPackageOrImport(final SourceUnit source, final AnnotatedNode parent, final AnnotationNode node) {
    Expression value = node.getMember("value");
    ClassNode scriptType;

    if (value == null) {
        if ( type == Type.MAVEN )
        {
            scriptType = MAVEN_BASE_SCRIPT_TYPE;
        }
        else
        {
            scriptType = GRADLE_BASE_SCRIPT_TYPE;
        }
    } else {
        if (!(value instanceof ClassExpression)) {
            addError( "Annotation " + getType() + " member 'value' should be a class literal.", value);
            return;
        }
        scriptType = value.getType();
    }
    List<ClassNode> classes = source.getAST().getClasses();
    for (ClassNode classNode : classes) {
        if (classNode.isScriptBody()) {
            changeBaseScriptType(parent, classNode, scriptType);
        }
    }
}
 
Example 5
Source File: BaseScriptASTTransformation.java    From groovy with Apache License 2.0 4 votes vote down vote up
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 6
Source File: ASTTransformer.java    From pom-manipulation-ext with Apache License 2.0 4 votes vote down vote up
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);
    }
}