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

org.jadira.reflection.cloning.implementor.UnsafeCloneStrategy Maven / Gradle / Ivy

There is a newer version: 7.0.0.CR1
Show newest version
/*
 *  Copyright 2013 Chris Pheby
 *
 *  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 org.jadira.reflection.cloning.implementor;

import java.util.IdentityHashMap;

import org.jadira.reflection.access.model.ClassModel;
import org.jadira.reflection.access.model.FieldModel;
import org.jadira.reflection.access.unsafe.UnsafeClassAccess;
import org.jadira.reflection.access.unsafe.UnsafeOperations;
import org.jadira.reflection.cloning.api.CloneDriver;
import org.jadira.reflection.cloning.api.CloneStrategy;
import org.objenesis.ObjenesisException;

/**
 * A CloneStrategy that uses sun.misc.Unsafe
 */
public class UnsafeCloneStrategy extends AbstractCloneStrategy implements CloneStrategy {

    private static final UnsafeOperations UNSAFE_OPERATIONS = UnsafeOperations.getUnsafeOperations();
    
    @Override
    public  T newInstance(Class c) {
        try {
            return UNSAFE_OPERATIONS.allocateInstance(c);
        } catch (IllegalStateException e) {
            throw new ObjenesisException(e.getCause());
        }
    }

    private static UnsafeCloneStrategy instance = new UnsafeCloneStrategy();

    /**
     * Returns a shared instance of UnsafeCloneStrategy
     * @return The instance
     */
    public static UnsafeCloneStrategy getInstance() {
        return instance;
    }
    
    @Override
    protected  ClassModel getClassModel(Class clazz) {
        return UnsafeClassAccess.get(clazz).getClassModel();
    }
    
    @Override
    protected  void handleTransientField(T copy, FieldModel f) {
    	
    	Class type = f.getFieldClass();
        if (type.isPrimitive()) {
    		if (java.lang.Boolean.TYPE == type) {
    			f.getFieldAccess().putBooleanValue(copy, false);
    		} else if (java.lang.Byte.TYPE == type) {
    			f.getFieldAccess().putByteValue(copy, (byte) 0);
    		} else if (java.lang.Character.TYPE == type) {
    			f.getFieldAccess().putCharValue(copy, '\u0000');
    		} else if (java.lang.Short.TYPE == type) {
    			f.getFieldAccess().putShortValue(copy, (short) 0);
    		} else if (java.lang.Integer.TYPE == type) {
    			f.getFieldAccess().putIntValue(copy, 0);
    		} else if (java.lang.Long.TYPE == type) {
    			f.getFieldAccess().putLongValue(copy, 0L);
    		} else if (java.lang.Float.TYPE == type) {
    			f.getFieldAccess().putFloatValue(copy, 0.0f);
    		} else if (java.lang.Double.TYPE == type) {
    			f.getFieldAccess().putDoubleValue(copy, 0.0d);
    		}
        } else {
        	f.getFieldAccess().putValue(copy, null);
        }
    }

    @Override
    protected  void handleClonePrimitiveField(T obj, T copy, CloneDriver driver, FieldModel f,
            IdentityHashMap referencesToReuse) {
    	
    	Class type = f.getFieldClass();
        if (type.isPrimitive()) {
    		if (java.lang.Boolean.TYPE == type) {
    			f.getFieldAccess().putBooleanValue(copy, f.getFieldAccess().getBooleanValue(obj));
    		} else if (java.lang.Byte.TYPE == type) {
    			f.getFieldAccess().putByteValue(copy, f.getFieldAccess().getByteValue(obj));
    		} else if (java.lang.Character.TYPE == type) {
    			f.getFieldAccess().putCharValue(copy, f.getFieldAccess().getCharValue(obj));
    		} else if (java.lang.Short.TYPE == type) {
    			f.getFieldAccess().putShortValue(copy, f.getFieldAccess().getShortValue(obj));
    		} else if (java.lang.Integer.TYPE == type) {
    			f.getFieldAccess().putIntValue(copy, f.getFieldAccess().getIntValue(obj));
    		} else if (java.lang.Long.TYPE == type) {
    			f.getFieldAccess().putLongValue(copy, f.getFieldAccess().getLongValue(obj));
    		} else if (java.lang.Float.TYPE == type) {
    			f.getFieldAccess().putFloatValue(copy, f.getFieldAccess().getFloatValue(obj));
    		} else if (java.lang.Double.TYPE == type) {
    			f.getFieldAccess().putDoubleValue(copy, f.getFieldAccess().getDoubleValue(obj));
    		}
        } else {
        	f.getFieldAccess().putValue(copy, f.getFieldAccess().getValue(obj));
        }
    }

    @Override
    protected  Object getFieldValue(T obj, FieldModel f) {
        return f.getFieldAccess().getValue(obj);
    }

    @Override
    protected  void putFieldValue(T obj, FieldModel f, Object value) {
    	f.getFieldAccess().putValue(obj, value);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy