Package org.objectweb.speedo.genclass.merger

Source Code of org.objectweb.speedo.genclass.merger.GenClassAdapter

/**
* Copyright (C) 2001-2005 France Telecom R&D
*/
package org.objectweb.speedo.genclass.merger;

import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.CodeVisitor;
import org.objectweb.asm.Constants;
import org.objectweb.speedo.generation.enhancer.common.CodeRenamer;
import org.objectweb.speedo.generation.enhancer.common.LoggedClassAdapter;
import org.objectweb.speedo.lib.Personality;
import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.Logger;

import java.util.Arrays;

/**
* Writes the non abstract methods and the fields of a class.
*/
public class GenClassAdapter extends LoggedClassAdapter {
    private final GCInfo gc;
    private boolean visitConstructor = true;
   
    public GenClassAdapter(ClassVisitor classVisitor, Logger logger, GCInfo gc, Personality p) {
        super(classVisitor, p, logger);
        this.gc = gc;
    }

    public boolean isVisitConstructor() {
        return visitConstructor;
    }

    public void setVisitConstructor(boolean v) {
        this.visitConstructor = v;
    }

    /**
     * Makes the classes no more abstract
     */
    public void visit(final int version, final int access,
            final String name,
            final String supername,
            final String[] interfaces,
            final String sourceFile) {
        if (logger.isLoggable(BasicLevel.DEBUG)) {
            StringBuffer sb = new StringBuffer("\tVisit class: ");
            sb.append(name).append("=>").append(gc.classToWrite);
            sb.append(" extends ").append(supername).append("=>").append(gc.getSuperName());
            if (interfaces != null && interfaces.length > 0) {
                sb.append(" implements ").append(Arrays.asList(interfaces));
            }
            logger.log(BasicLevel.DEBUG, sb.toString());
        }
        super.cv.visit(version, access - Constants.ACC_ABSTRACT,
                gc.classToWrite, gc.getSuperName(), interfaces, sourceFile);
    }

    /**
     * Add only non abstract method.
     */
    public CodeVisitor visitMethod(final int access,
            final String name,
            final String desc,
            final String[] exceptions,
            final Attribute attrs) {
       
        if ((access & Constants.ACC_ABSTRACT) == 0 //non abastract method
                && (visitConstructor || !"<init>".equals(name)) // permit to forget constructors
                ) {
                logger.log(BasicLevel.DEBUG, "\t\tVisit the method: " + name + desc);
                CodeVisitor mv = super.cv.visitMethod(access, name, desc, exceptions, attrs);
                mv = new CodeRenamer(mv, gc.firstClass, gc.classToWrite);
                mv = new CodeRenamer(mv, gc.secondClass, gc.classToWrite);
            return mv;
        } else {
            return null;
        }
    }
}
TOP

Related Classes of org.objectweb.speedo.genclass.merger.GenClassAdapter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.