Package org.objectweb.speedo.generation.mivisitor

Source Code of org.objectweb.speedo.generation.mivisitor.PrimaryKeyFieldAdder$Loader

/**
* Copyright (C) 2001-2004 France Telecom R&D
*
* This library 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 2 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
package org.objectweb.speedo.generation.mivisitor;

import org.objectweb.speedo.lib.Personality;
import org.objectweb.speedo.metadata.SpeedoClass;
import org.objectweb.speedo.metadata.SpeedoField;
import org.objectweb.speedo.api.SpeedoException;
import org.objectweb.speedo.api.SpeedoProperties;
import org.objectweb.speedo.generation.enhancer.common.AbstractEnhancerComponent;
import org.objectweb.speedo.generation.enhancer.common.LoggedClassVisitor;
import org.objectweb.speedo.generation.enhancer.common.Util;
import org.objectweb.speedo.generation.api.SpeedoCompilerParameter;
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.CodeVisitor;
import org.objectweb.asm.Constants;
import org.objectweb.asm.Type;
import org.objectweb.asm.Attribute;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

/**
*
* @author S.Chassande-Barrioz
*/
public class PrimaryKeyFieldAdder extends AbstractMetaInfoVisitor {
    private Loader loader;

    public PrimaryKeyFieldAdder(Personality p) {
        super(p);
        loader = new Loader(p);
    }

    public PrimaryKeyFieldAdder(MetaInfoVisitor mim, Personality p) {
        super(mim, p);
        loader = new Loader(p);
    }

    private class Loader extends AbstractEnhancerComponent {

      public Loader(Personality p) {
        super(p);
      }
     
        public boolean init() throws SpeedoException {
            isSrcJar = false;
          return true;
        }

        public void process() throws SpeedoException {
            //nothing
        }
        public void setLogger(Logger logger) {
            this.logger = logger;
        }
    }

    protected String getLoggerName() {
        return SpeedoProperties.LOGGER_NAME + ".generation.enhancer.analyzer";
    }

    public void setLogger(Logger logger) {
        super.setLogger(logger);
        loader.setLogger(logger);
    }

    public void visitCompilerParameter(SpeedoCompilerParameter scp) throws SpeedoException {
        loader.setSpeedoCompilerParameter(scp);
        super.visitCompilerParameter(scp);
    }


    public void visitClass(SpeedoClass sc) throws SpeedoException {
        super.visitClass(sc);
        List pkfields = sc.getPKFields();
        if (sc.identity.objectidClass == null) {
            return;
        }
        logger.log(BasicLevel.DEBUG, "PrimaryKeyFieldAdder.visitClass(" + sc.getFQName() + ")");
        boolean isSrcJar = false;
        SpeedoCompilerParameter _scp = loader.getSpeedoCompilerParameter();
        String className = sc.identity.objectidClass;
        if (className.indexOf('.') == -1) {
            className = sc.moPackage.name + '.' + className;
        }
        UserIDClassAnalyzer uica = new UserIDClassAnalyzer(logger);
        loader.loadJavaClass(isSrcJar, className, _scp.output,false)
                .accept(uica, true);
        if (uica.fieldNames.isEmpty()) {
            throw new SpeedoException("The identifier class '"
                    + sc.identity.objectidClass + "' has no public field.");
        }
        if (sc.getSuperClassName() != null) {
      //do nothing, the primary key field are inherited from the ancestor
    } else if (pkfields.isEmpty()) {
            for(int i=0; i<uica.fieldNames.size(); i++) {
                SpeedoField sf = (SpeedoField) sc.fields.get(uica.fieldNames.get(i));
                if (sf == null) {
                    logger.log(BasicLevel.WARN, "The public field '"
                            + uica.fieldNames.get(i) + "' of the identifier class "
                            + className + "' does not match to any field in the persistent class '"
                            + sc.getFQName() +"'.");
                    continue;
                }
                if (!sf.type.equals(uica.fieldTypes.get(i))) {
                    throw new SpeedoException("The field '" + sf.name
                            + "' has not the same type in identifier class '"
                            + className + "' ("
                            + Util.type(Type.getType((String) uica.fieldTypes.get(i)))
                            + ") and in the persistent class '" + sc.getFQName()
                            + "' (" + Util.type(Type.getType(sf.type)) + ").");
                }
                sf.primaryKey = true;
            }
        } else {
            //check that the lists are the same
            for(int i=0; i<pkfields.size(); i++) {
                if (!uica.fieldNames.contains(((SpeedoField) pkfields.get(i)).name)) {
                    logger.log(BasicLevel.ERROR, "uica.fieldNames:\n" + uica.fieldNames);
                    logger.log(BasicLevel.ERROR, "pkfields:\n" + pkfields);
                    throw new SpeedoException("The field '" + pkfields.get(i)
                            + "' is marked primary-key=true whereas it is not "
                            + "defined in the identifier class '" + className
                            + "' associated to the persistent class '"
                            + sc.getFQName() + "'.");
                }
            }
        }
    }

    private static class UserIDClassAnalyzer  extends LoggedClassVisitor implements ClassVisitor {

        public List fieldNames;
        public List fieldTypes;

        public UserIDClassAnalyzer(Logger logger) {
            super(logger);
            fieldNames = new ArrayList();
            fieldTypes = new ArrayList();
        }

        public void visit(int version, int i, String s, String s1, String[] strings, String s2) {
        }

        public void visitInnerClass(String s, String s1, String s2, int i) {
        }

        public void visitField(final int access,
                           final String name,
                           final String desc,
                           final Object value,
                           final Attribute attrs) {
            if ((access & Constants.ACC_PUBLIC) != 0) {
                fieldNames.add(name);
                fieldTypes.add(desc);
            }
        }

        public CodeVisitor visitMethod(int i, String s, String s1, String[] strings, Attribute attrs) {
            return null;
        }

        public void visitAttribute(Attribute attribute) {
        }

        public void visitEnd() {
        }
    }
}
TOP

Related Classes of org.objectweb.speedo.generation.mivisitor.PrimaryKeyFieldAdder$Loader

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.