alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (GenericsTestBase.java)

This example Groovy source code file (GenericsTestBase.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

classadapter, classreader, compilationunit, genericstester, genericstester, groovyclassloader, groovytestcase, hashmap, mycollector, mycollector, myloader, myloader, string, string, util

The Groovy GenericsTestBase.java source code

/*
 * Copyright 2003-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package gls.generics;

import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyClassLoader.InnerLoader;
import groovy.util.GroovyTestCase;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.control.CompilationUnit;
import org.codehaus.groovy.control.SourceUnit;
import org.objectweb.asm.*;

import java.util.HashMap;
import java.util.Map;

public class GenericsTestBase extends GroovyTestCase {
    MyLoader loader;
    HashMap signatures = new HashMap();
    
    private class MyLoader extends GroovyClassLoader{
        public MyLoader(ClassLoader classLoader) {
            super(classLoader);
        }

        protected ClassCollector createCollector(CompilationUnit unit,SourceUnit su) {
            return new MyCollector(new InnerLoader(this), unit, su);
        }
    }
    private class MyCollector extends GroovyClassLoader.ClassCollector{

        public MyCollector(InnerLoader myLoader, CompilationUnit unit, SourceUnit su) {
           super(myLoader,unit,su);
        }
        protected Class createClass(byte[] code, ClassNode classNode) {
            ClassReader cr = new ClassReader(code);
            GenericsTester classVisitor = new GenericsTester(new org.objectweb.asm.tree.ClassNode());
            cr.accept(classVisitor, ClassWriter.COMPUTE_MAXS);
            return super.createClass(code,classNode);
        }        
    }
    private class GenericsTester  extends ClassAdapter {
        public GenericsTester(ClassVisitor cv) {
            super(cv);
        }
        public void visit(int version, int access, String name,
                String signature, String superName, String[] interfaces) {
            if (signature!=null) signatures.put("class", signature);
        }
        public FieldVisitor visitField(int access, String name, String desc,
                String signature, Object value) {
            if (signature!=null) signatures.put(name,signature);
            return super.visitField(access, name, desc, signature, value);
        }
        public MethodVisitor visitMethod(int access, String name, String desc,
                String signature, String[] exceptions) {
            if (signature!=null) signatures.put(name+desc,signature);
            return super.visitMethod(access, name, desc, signature, exceptions);
        }

    }
    
    public void setUp(){
        loader = new MyLoader(this.getClass().getClassLoader());
    }
    
    public void createClassInfo(String script) {
        loader.parseClass(script);
    }
    
    public Map getSignatures() {
        return signatures;
    }
    
    protected void shouldNotCompile(String script) {
        try {
            loader.parseClass(script);
        } catch (CompilationFailedException cfe) {
            return;
        }
        throw new AssertionError("compilation of script '"+script+"' should have failed, but did not.");
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy GenericsTestBase.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.