org.jadira.reflection.hashcode.HashCodeBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cloning Show documentation
Show all versions of cloning Show documentation
Cloning for Jadira Framework
/*
* Copyright 2013 Christopher 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.hashcode;
import java.lang.reflect.Modifier;
import java.util.IdentityHashMap;
import org.jadira.reflection.access.api.ClassAccess;
import org.jadira.reflection.access.api.ClassAccessFactory;
import org.jadira.reflection.access.api.FieldAccess;
import org.jadira.reflection.access.api.MethodAccess;
import org.jadira.reflection.access.portable.PortableClassAccessFactory;
import org.jadira.reflection.access.unsafe.UnsafeClassAccessFactory;
import org.jadira.reflection.core.platform.FeatureDetection;
/**
* This class is based on the capabilities provided by Commons-Lang's
* HashCodeBuilder. It is not a complete "drop-in" replacement, but is near enough
* in functionality and in terms of its API that it should be quite
* straightforward to switch from one class to the other.
*
* HashCodeBuilder makes use of Jadira's reflection capabilities. This means, like
* Cloning there is a brief warm-up time entailed in preparing introspection
* when a class is first processed. If you are already performing cloning on a
* type you will not be impacted by this as any necessary warm-up will have
* already taken place.
*
* Once the initial warmup has been performed, you will, usually, find enhanced
* performance compared to standard reflection based approaches. The strategy to
* be used can be configured.
*
* Where objects that are reachable from the object being compared do not
* override hashCode(), the default behaviour is to rely on
* Object#hashCode(Object) which uses the object identity for those
* objects. This is also the behaviour of Commons Lang. You can modify this to
* reflect into any reachable object that does not override equals by enabling
* the defaultDeepReflect property.
*
* Transient fields are not compared by the current implementation of this
* class.
*/
public class HashCodeBuilder {
private static final ThreadLocal reflectionBuilder = new ThreadLocal() {
protected HashCodeBuilder initialValue() {
return new HashCodeBuilder(37, 17);
};
};
private IdentityHashMap