How to use write method of org.powermock.classloading.DeepCloner class

Best Powermock code snippet using org.powermock.classloading.DeepCloner.write

Source:DeepCloner.java Github

copy

Full Screen

...160 ObjectInputStream ois = null;161 try {162 ByteArrayOutputStream bos = new ByteArrayOutputStream();163 oos = new ObjectOutputStream(bos);164 oos.writeObject(source);165 oos.flush();166 ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());167 ois = new ObjectInputStream(bin);168 return ois.readObject();169 } catch (Exception e) {170 throw new RuntimeException(e);171 } finally {172 close(oos);173 close(ois);174 }175 }176 private void close(Closeable closeable) {177 try {178 if (closeable != null) {179 closeable.close();180 }181 } catch (IOException e) {182 }183 }184 @SuppressWarnings({ "unchecked", "rawtypes" })185 private Object cloneEnum(ClassLoader targetCL, Object source) {186 Object target;187 final Class enumClassLoadedByTargetCL = ClassLoaderUtil.loadClass(getType(source), targetCL);188 target = getEnumValue(source, enumClassLoadedByTargetCL);189 return target;190 }191 @SuppressWarnings("unchecked")192 private <T> void cloneFields(ClassLoader targetCL, Class<T> targetClass, Object source, Object target,193 Map<Object, Object> referenceMap, boolean cloneStandardJavaTypes) {194 Class<?> currentTargetClass = targetClass;195 while (currentTargetClass != null) {196 for (Field field : currentTargetClass.getDeclaredFields()) {197 if (field.getAnnotation(doNotClone) != null) {198 continue;199 }200 field.setAccessible(true);201 try {202 final Field declaredField = Whitebox.getField(getType(source), field.getName());203 declaredField.setAccessible(true);204 final Object object = declaredField.get(source);205 final Object instantiatedValue;206 if (object == source) {207 instantiatedValue = target;208 } else if (referenceMap.containsKey(object)) {209 instantiatedValue = referenceMap.get(object);210 } else {211 if (object == null && !isIterable(object)) {212 instantiatedValue = object;213 } else {214 Class<Object> type = getType(object);215 if (type.getName().equals("void")) {216 type = Class.class.cast(Class.class);217 }218 final Class<Object> typeLoadedByCL = ClassLoaderUtil.loadClass(type, targetCL219 );220 if (type.isEnum()) {221 instantiatedValue = getEnumValue(object, typeLoadedByCL);222 } else {223 instantiatedValue = performClone(typeLoadedByCL, object, cloneStandardJavaTypes);224 }225 }226 }227 final boolean needsUnsafeWrite = field.isEnumConstant() || isStaticFinalModifier(field);228 if (needsUnsafeWrite) {229 UnsafeFieldWriter.write(field, target, instantiatedValue);230 } else {231 field.set(target, instantiatedValue);232 }233 } catch (RuntimeException e) {234 throw e;235 } catch (Exception e) {236 throw new RuntimeException(e);237 }238 }239 currentTargetClass = currentTargetClass.getSuperclass();240 }241 }242 private static <T> boolean isStandardJavaType(Class<T> targetClass) {243 return targetClass.getName().startsWith("java.");244 }245 private static boolean isStaticFinalModifier(final Field field) {246 final int modifiers = field.getModifiers();247 return Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers)248 || field.getDeclaringClass().equals(Character.class) && field.getName().equals("MIN_RADIX");249 }250 private static boolean isIterable(final Object object) {251 return object != null && isIterable(object.getClass());252 }253 private static boolean isIterable(final Class<?> cls) {254 return Iterable.class.isAssignableFrom(cls);255 }256 @SuppressWarnings({ "unchecked", "rawtypes" })257 private static Enum getEnumValue(final Object enumValueOfSourceClassloader,258 final Class<Object> enumTypeLoadedByTargetCL) {259 return Enum.valueOf((Class) enumTypeLoadedByTargetCL, ((Enum) enumValueOfSourceClassloader).toString());260 }261 private Object instantiateArray(ClassLoader targetCL, Class<?> arrayClass, Object objectToClone,262 boolean cloneStandardJavaTypes) {263 final int arrayLength = Array.getLength(objectToClone);264 final Object array = Array.newInstance(arrayClass.getComponentType(), arrayLength);265 for (int i = 0; i < arrayLength; i++) {266 final Object object = Array.get(objectToClone, i);267 final Object performClone;268 if (object == null) {269 performClone = null;270 } else {271 performClone = performClone(ClassLoaderUtil.loadClass(getType(object), targetCL),272 object, cloneStandardJavaTypes);273 }274 Array.set(array, i, performClone);275 }276 return array;277 }278 /**279 * Most of this code has been copied from the Sun14ReflectionProvider in the280 * XStream project. Some changes has been made, namely if the field is281 * static final then the {@link Unsafe#staticFieldOffset(Field)} method is282 * used instead of {@link Unsafe#objectFieldOffset(Field)}.283 * 284 * @author Joe Walnes285 * @author Brian Slesinsky286 * @author Johan Haleby287 */288 private static class UnsafeFieldWriter {289 private final static Unsafe unsafe;290 private final static Exception exception;291 static {292 Unsafe u = null;293 Exception ex = null;294 try {295 Class<?> objectStreamClass = Class.forName("sun.misc.Unsafe");296 Field unsafeField = objectStreamClass.getDeclaredField("theUnsafe");297 unsafeField.setAccessible(true);298 u = (Unsafe) unsafeField.get(null);299 } catch (Exception e) {300 ex = e;301 }302 exception = ex;303 unsafe = u;304 }305 public static void write(Field field, Object object, Object value) {306 if (exception != null) {307 throw new RuntimeException("Could not set field " + object.getClass() + "." + field.getName(),308 exception);309 }310 try {311 final long offset;312 if (DeepCloner.isStaticFinalModifier(field)) {313 offset = unsafe.staticFieldOffset(field);314 } else {315 offset = unsafe.objectFieldOffset(field);316 }317 Class<?> type = field.getType();318 if (type.isPrimitive()) {319 if (type.equals(Integer.TYPE)) {...

Full Screen

Full Screen

write

Using AI Code Generation

copy

Full Screen

1package org.powermock.classloading;2import java.io.IOException;3import java.io.ObjectOutputStream;4import java.io.OutputStream;5import java.io.Serializable;6public class DeepCloner {7 public static <T extends Serializable> T clone(T object) {8 try {9 return write(object);10 } catch (IOException e) {11 throw new RuntimeException(e);12 }13 }14 private static <T extends Serializable> T write(T object) throws IOException {15 OutputStream baos = new ByteArrayOutputStream();16 ObjectOutputStream oos = new ObjectOutputStream(baos);17 oos.writeObject(object);18 oos.close();19 return read(baos);20 }21 private static <T extends Serializable> T read(OutputStream baos) throws IOException {22 return null;23 }24}25package org.powermock.classloading;26import java.io.IOException;27import java.io.ObjectInputStream;28import java.io.OutputStream;29import java.io.Serializable;30public class DeepCloner {31 public static <T extends Serializable> T clone(T object) {32 try {33 return write(object);34 } catch (IOException e) {35 throw new RuntimeException(e);36 }37 }38 private static <T extends Serializable> T write(T object) throws IOException {39 OutputStream baos = new ByteArrayOutputStream();40 ObjectOutputStream oos = new ObjectOutputStream(baos);41 oos.writeObject(object);42 oos.close();43 return read(baos);44 }45 private static <T extends Serializable> T read(OutputStream baos) throws IOException {46 return null;47 }48}49package org.powermock.classloading;50import java.io.IOException;51import java.io.ObjectOutputStream;52import java.io.OutputStream;53import java.io.Serializable;54import org.powermock.api.easymock.PowerMock;55import org.powermock.api.easymock.annotation.Mock;56import org.powermock.api.easymock.annotation.MockNice;57import org.powermock.api.easymock.annotation.MockStrict;58import org.powermock.api.e

Full Screen

Full Screen

write

Using AI Code Generation

copy

Full Screen

1public class DeepCloneTest {2 public void testDeepClone() throws Exception {3 DeepCloneTest deepCloneTest = new DeepCloneTest();4 DeepCloneTest deepCloneTestClone = (DeepCloneTest) DeepCloner.write(deepCloneTest);5 Assert.assertNotEquals(deepCloneTest, deepCloneTestClone);6 }7}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful