001/*
002 *  Copyright 2013 Chris Pheby
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.jadira.reflection.cloning.implementor;
017
018import java.util.IdentityHashMap;
019
020import org.jadira.reflection.access.model.ClassModel;
021import org.jadira.reflection.access.model.FieldModel;
022import org.jadira.reflection.access.unsafe.UnsafeClassAccess;
023import org.jadira.reflection.access.unsafe.UnsafeOperations;
024import org.jadira.reflection.cloning.api.CloneDriver;
025import org.jadira.reflection.cloning.api.CloneStrategy;
026import org.objenesis.ObjenesisException;
027
028/**
029 * A CloneStrategy that uses sun.misc.Unsafe
030 */
031public class UnsafeCloneStrategy extends AbstractCloneStrategy implements CloneStrategy {
032
033    private static final UnsafeOperations UNSAFE_OPERATIONS = UnsafeOperations.getUnsafeOperations();
034    
035    @Override
036    public <T> T newInstance(Class<T> c) {
037        try {
038            return UNSAFE_OPERATIONS.allocateInstance(c);
039        } catch (IllegalStateException e) {
040            throw new ObjenesisException(e.getCause());
041        }
042    }
043
044    private static UnsafeCloneStrategy instance = new UnsafeCloneStrategy();
045
046    /**
047     * Returns a shared instance of UnsafeCloneStrategy
048     * @return The instance
049     */
050    public static UnsafeCloneStrategy getInstance() {
051        return instance;
052    }
053    
054    @Override
055    protected <W> ClassModel<W> getClassModel(Class<W> clazz) {
056        return UnsafeClassAccess.get(clazz).getClassModel();
057    }
058    
059    @Override
060    protected <T> void handleTransientField(T copy, FieldModel<T> f) {
061        
062        Class<?> type = f.getFieldClass();
063        if (type.isPrimitive()) {
064                if (java.lang.Boolean.TYPE == type) {
065                        f.getFieldAccess().putBooleanValue(copy, false);
066                } else if (java.lang.Byte.TYPE == type) {
067                        f.getFieldAccess().putByteValue(copy, (byte) 0);
068                } else if (java.lang.Character.TYPE == type) {
069                        f.getFieldAccess().putCharValue(copy, '\u0000');
070                } else if (java.lang.Short.TYPE == type) {
071                        f.getFieldAccess().putShortValue(copy, (short) 0);
072                } else if (java.lang.Integer.TYPE == type) {
073                        f.getFieldAccess().putIntValue(copy, 0);
074                } else if (java.lang.Long.TYPE == type) {
075                        f.getFieldAccess().putLongValue(copy, 0L);
076                } else if (java.lang.Float.TYPE == type) {
077                        f.getFieldAccess().putFloatValue(copy, 0.0f);
078                } else if (java.lang.Double.TYPE == type) {
079                        f.getFieldAccess().putDoubleValue(copy, 0.0d);
080                }
081        } else {
082                f.getFieldAccess().putValue(copy, null);
083        }
084    }
085
086    @Override
087    protected <T> void handleClonePrimitiveField(T obj, T copy, CloneDriver driver, FieldModel<T> f,
088            IdentityHashMap<Object, Object> referencesToReuse) {
089        
090        Class<?> type = f.getFieldClass();
091        if (type.isPrimitive()) {
092                if (java.lang.Boolean.TYPE == type) {
093                        f.getFieldAccess().putBooleanValue(copy, f.getFieldAccess().getBooleanValue(obj));
094                } else if (java.lang.Byte.TYPE == type) {
095                        f.getFieldAccess().putByteValue(copy, f.getFieldAccess().getByteValue(obj));
096                } else if (java.lang.Character.TYPE == type) {
097                        f.getFieldAccess().putCharValue(copy, f.getFieldAccess().getCharValue(obj));
098                } else if (java.lang.Short.TYPE == type) {
099                        f.getFieldAccess().putShortValue(copy, f.getFieldAccess().getShortValue(obj));
100                } else if (java.lang.Integer.TYPE == type) {
101                        f.getFieldAccess().putIntValue(copy, f.getFieldAccess().getIntValue(obj));
102                } else if (java.lang.Long.TYPE == type) {
103                        f.getFieldAccess().putLongValue(copy, f.getFieldAccess().getLongValue(obj));
104                } else if (java.lang.Float.TYPE == type) {
105                        f.getFieldAccess().putFloatValue(copy, f.getFieldAccess().getFloatValue(obj));
106                } else if (java.lang.Double.TYPE == type) {
107                        f.getFieldAccess().putDoubleValue(copy, f.getFieldAccess().getDoubleValue(obj));
108                }
109        } else {
110                f.getFieldAccess().putValue(copy, f.getFieldAccess().getValue(obj));
111        }
112    }
113
114    @Override
115    protected <T> Object getFieldValue(T obj, FieldModel<T> f) {
116        return f.getFieldAccess().getValue(obj);
117    }
118
119    @Override
120    protected <T> void putFieldValue(T obj, FieldModel<T> f, Object value) {
121        f.getFieldAccess().putValue(obj, value);
122    }
123}