All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.qitsoft.qimono.internal.RegistryAnnotationAdapter Maven / Gradle / Ivy

Go to download

The framework library base on Mockito used to stub methods with private visibility, final methods or static methods. Also this library could be used to assign/retrieve values from private fields and to substitute date and time in tests.

The newest version!
/*
 * Copyright (C) 2011 QitSoft Inc.
 *
 * This file is part of Qimono.
 *
 * Qimono is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 */

package com.qitsoft.qimono.internal;

import java.util.ArrayList;
import java.util.List;
import org.objectweb.asm.AnnotationVisitor;

/**
 * The Annotation visitor which registers the instructions and after that
 * could be replayed.
 * 
 * @author Serghei Soloviov 
 */
public class RegistryAnnotationAdapter implements AnnotationVisitor {

    private static final int VISIT = 1;
    private static final int ENUM = 2;
    private static final int ANNOTATION = 3;
    private static final int ARRAY = 4;
    private static final int END = 5;

    private List registry = new ArrayList();

    /**
     * Default constructor.
     */
    public RegistryAnnotationAdapter() {
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void visit(String name, Object value) {
        registry.add(new Object[]{VISIT, name, value});
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void visitEnum(String name, String desc, String value) {
        registry.add(new Object[]{ENUM, name, desc, value});
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public AnnotationVisitor visitAnnotation(String name, String desc) {
        RegistryAnnotationAdapter ann = new RegistryAnnotationAdapter();
        registry.add(new Object[]{ANNOTATION, name, desc, ann});
        return ann;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public AnnotationVisitor visitArray(String name) {
        RegistryAnnotationAdapter ann = new RegistryAnnotationAdapter();
        registry.add(new Object[]{ARRAY, name, ann});
        return ann;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void visitEnd() {
        registry.add(new Object[]{END});
    }

    /**
     * Replays the annotation declarations registered before. You can invoke replay
     * for multiple times.
     * 
     * @param avp the next annotation visitor in the chain where the instructions
     *      will be replayed.
     */
    public void replay(AnnotationVisitor avp) {
        for(Object[] obj : registry) {
            switch((Integer)obj[0]) {
                case VISIT: avp.visit((String)obj[1], obj[2]); break;
                case ENUM: avp.visitEnum((String)obj[1], (String)obj[2], (String)obj[3]); break;
                case ANNOTATION:
                    AnnotationVisitor av_ann = avp.visitAnnotation((String)obj[1], (String)obj[2]);
                    ((RegistryAnnotationAdapter)obj[3]).replay(av_ann);
                    break;
                case ARRAY:
                    AnnotationVisitor av_array = avp.visitArray((String)obj[1]);
                    ((RegistryAnnotationAdapter)obj[2]).replay(av_array);
                    break;
                case END: avp.visitEnd(); break;
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy