How to use makeDeferConstructorNonPublic method of org.powermock.core.transformers.javassist.testclass.JavaAssistTestClassTransformer class

Best Powermock code snippet using org.powermock.core.transformers.javassist.testclass.JavaAssistTestClassTransformer.makeDeferConstructorNonPublic

Source:JavaAssistTestClassTransformer.java Github

copy

Full Screen

...54 55 if (isTestClass(clazz)) {56 removeTestAnnotationsForTestMethodsThatRunOnOtherClassLoader(clazz);57 addLifeCycleNotifications(clazz);58 makeDeferConstructorNonPublic(clazz);59 restoreOriginalConstructorsAccesses(clazz);60 61 } else if (isNestedWithinTestClass(clazz)) {62 makeDeferConstructorNonPublic(clazz);63 restoreOriginalConstructorsAccesses(clazz);64 }65 66 }67 68 private boolean isTestClass(CtClass clazz) {69 try {70 return Class.forName(clazz.getName(), false, getTestClass().getClassLoader())71 .isAssignableFrom(getTestClass());72 } catch (ClassNotFoundException ex) {73 return false;74 }75 }76 77 private boolean isNestedWithinTestClass(CtClass clazz) {78 String clazzName = clazz.getName();79 return clazzName.startsWith(getTestClass().getName())80 && '$' == clazzName.charAt(getTestClass().getName().length());81 }82 83 private Class<?> asOriginalClass(CtClass type) throws Exception {84 try {85 return type.isArray()86 ? Array.newInstance(asOriginalClass(type.getComponentType()), 0).getClass()87 : type.isPrimitive()88 ? Primitives.getClassFor((CtPrimitiveType) type)89 : Class.forName(type.getName(), true, getTestClass().getClassLoader());90 } catch (Exception ex) {91 throw new RuntimeException("Cannot resolve type: " + type, ex);92 }93 }94 95 private Class<?>[] asOriginalClassParams(CtClass[] parameterTypes)96 throws Exception {97 final Class<?>[] classParams = new Class[parameterTypes.length];98 for (int i = 0; i < classParams.length; ++i) {99 classParams[i] = asOriginalClass(parameterTypes[i]);100 }101 return classParams;102 }103 104 private void removeTestMethodAnnotationFrom(CtMethod m) {105 final AnnotationsAttribute attr = (AnnotationsAttribute)106 m.getMethodInfo().getAttribute(AnnotationsAttribute.visibleTag);107 javassist.bytecode.annotation.Annotation[] newAnnotations =108 new javassist.bytecode.annotation.Annotation[attr.numAnnotations() - 1];109 int i = -1;110 for (javassist.bytecode.annotation.Annotation a : attr.getAnnotations()) {111 if (a.getTypeName().equals(getTestMethodAnnotationType().getName())) {112 continue;113 }114 newAnnotations[++i] = a;115 }116 attr.setAnnotations(newAnnotations);117 }118 119 private void removeTestAnnotationsForTestMethodsThatRunOnOtherClassLoader(CtClass clazz)120 throws Exception {121 for (CtMethod m : clazz.getDeclaredMethods()) {122 if (m.hasAnnotation(getTestMethodAnnotationType()) && mustHaveTestAnnotationRemoved(m)) {123 removeTestMethodAnnotationFrom(m);124 }125 }126 }127 128 private void addLifeCycleNotifications(CtClass clazz) {129 try {130 addClassInitializerNotification(clazz);131 addConstructorNotification(clazz);132 } catch (CannotCompileException ex) {133 throw new Error("Powermock error: " + ex.getMessage(), ex);134 }135 }136 137 private void addClassInitializerNotification(CtClass clazz)138 throws CannotCompileException {139 if (null == clazz.getClassInitializer()) {140 clazz.makeClassInitializer();141 }142 clazz.getClassInitializer().insertBefore(143 GlobalNotificationBuildSupport.class.getName()144 + ".testClassInitiated(" + clazz.getName() + ".class);");145 }146 147 private static boolean hasSuperClass(CtClass clazz) {148 try {149 CtClass superClazz = clazz.getSuperclass();150 /*151 * Being extra careful here - and backup in case the152 * work-in-progress clazz doesn't cause NotFoundException ...153 */154 return null != superClazz155 && !"java.lang.Object".equals(superClazz.getName());156 } catch (NotFoundException noWasSuperClassFound) {157 return false;158 }159 }160 161 private void addConstructorNotification(final CtClass clazz)162 throws CannotCompileException {163 final String notificationCode =164 GlobalNotificationBuildSupport.class.getName()165 + ".testInstanceCreated(this);";166 final boolean asFinally = !hasSuperClass(clazz);167 for (final CtConstructor constr : clazz.getDeclaredConstructors()) {168 constr.insertAfter(169 notificationCode,170 asFinally/* unless there is a super-class, because of this171 * problem: https://community.jboss.org/thread/94194*/);172 }173 }174 175 private void restoreOriginalConstructorsAccesses(CtClass clazz) throws Exception {176 Class<?> originalClass = getTestClass().getName().equals(clazz.getName())177 ? getTestClass()178 : Class.forName(clazz.getName(), true, getTestClass().getClassLoader());179 for (final CtConstructor ctConstr : clazz.getConstructors()) {180 int ctModifiers = ctConstr.getModifiers();181 if (!Modifier.isPublic(ctModifiers)) {182 /* Probably a defer-constructor */183 continue;184 }185 int desiredAccessModifiers = originalClass.getDeclaredConstructor(186 asOriginalClassParams(ctConstr.getParameterTypes())).getModifiers();187 188 if (Modifier.isPrivate(desiredAccessModifiers)) {189 ctConstr.setModifiers(Modifier.setPrivate(ctModifiers));190 } else if (Modifier.isProtected(desiredAccessModifiers)) {191 ctConstr.setModifiers(Modifier.setProtected(ctModifiers));192 } else if (!Modifier.isPublic(desiredAccessModifiers)) {193 ctConstr.setModifiers(Modifier.setPackage(ctModifiers));194 } else {195 /* ctConstr remains public */196 }197 }198 }199 200 private void makeDeferConstructorNonPublic(final CtClass clazz) {201 for (final CtConstructor constr : clazz.getConstructors()) {202 try {203 for (CtClass paramType : constr.getParameterTypes()) {204 if (IndicateReloadClass.class.getName()205 .equals(paramType.getName())) {206 /* Found defer constructor ... */207 final int modifiers = constr.getModifiers();208 if (Modifier.isPublic(modifiers)) {209 constr.setModifiers(Modifier.setProtected(modifiers));210 }211 break;212 }213 }214 } catch (NotFoundException thereAreNoParameters) {...

Full Screen

Full Screen

makeDeferConstructorNonPublic

Using AI Code Generation

copy

Full Screen

1public class JavaAssistTestClassTransformerTest {2 private static final String TEST_CLASS_NAME = "org.powermock.core.transformers.javassist.testclass.JavaAssistTestClassTransformerTest$TestClass";3 private static final String TEST_CLASS_NAME_WITHOUT_PACKAGE = "JavaAssistTestClassTransformerTest$TestClass";4 private static final String TEST_CLASS_PACKAGE = "org.powermock.core.transformers.javassist.testclass";5 private static final String TEST_CLASS_FILE_NAME = TEST_CLASS_NAME_WITHOUT_PACKAGE + ".class";6 private static final String TEST_CLASS_FILE_NAME_WITHOUT_PACKAGE = TEST_CLASS_NAME_WITHOUT_PACKAGE + ".class";7 private static final String TEST_CLASS_FILE_NAME_WITHOUT_EXTENSION = TEST_CLASS_NAME_WITHOUT_PACKAGE;8 private static final String TEST_CLASS_FILE_NAME_WITHOUT_EXTENSION_AND_PACKAGE = TEST_CLASS_NAME_WITHOUT_PACKAGE;9 private static final String TEST_CLASS_FILE_NAME_WITHOUT_EXTENSION_AND_PACKAGE_AND_UNDERSCORE = TEST_CLASS_NAME_WITHOUT_PACKAGE;10 private static final String TEST_CLASS_FILE_NAME_WITHOUT_EXTENSION_AND_PACKAGE_AND_UNDERSCORE_AND_DOLLAR = TEST_CLASS_NAME_WITHOUT_PACKAGE;11 private static final String TEST_CLASS_NAME_WITHOUT_UNDERSCORE = "org.powermock.core.transformers.javassist.testclass.JavaAssistTestClassTransformerTestTestClass";12 private static final String TEST_CLASS_NAME_WITHOUT_UNDERSCORE_AND_PACKAGE = "JavaAssistTestClassTransformerTestTestClass";13 private static final String TEST_CLASS_FILE_NAME_WITHOUT_UNDERSCORE = TEST_CLASS_NAME_WITHOUT_UNDERSCORE + ".class";14 private static final String TEST_CLASS_FILE_NAME_WITHOUT_UNDERSCORE_AND_PACKAGE = TEST_CLASS_NAME_WITHOUT_UNDERSCORE_AND_PACKAGE + ".class";15 private static final String TEST_CLASS_FILE_NAME_WITHOUT_UNDERSCORE_AND_PACKAGE_AND_DOLLAR = TEST_CLASS_NAME_WITHOUT_UNDERSCORE_AND_PACKAGE + ".class";16 private static final String TEST_CLASS_FILE_NAME_WITHOUT_UNDERSCORE_AND_PACKAGE_AND_DOLLAR_AND_EXTENSION = TEST_CLASS_NAME_WITHOUT_UNDERSCORE_AND_PACKAGE;17 private static final String TEST_CLASS_FILE_NAME_WITHOUT_UNDERSCORE_AND_PACKAGE_AND_DOLLAR_AND_EXTENSION_AND_UNDERSCORE = TEST_CLASS_NAME_WITHOUT_UNDERSCORE_AND_PACKAGE;18 private static final String TEST_CLASS_NAME_WITHOUT_UNDERSCORE_AND_DOLLAR = "org.powermock.core.transformers.javassist.testclass.JavaAssistTestClassTransformerTestTestClass";

Full Screen

Full Screen

makeDeferConstructorNonPublic

Using AI Code Generation

copy

Full Screen

1import org.powermock.core.transformers.javassist.testclass.JavaAssistTestClassTransformer;2import org.powermock.reflect.Whitebox;3import org.powermock.reflect.exceptions.ConstructorNotFoundException;4import org.powermock.reflect.exceptions.TooManyConstructorsFoundException;5import org.powermock.reflect.exceptions.TooManyMethodsFoundException;6import org.powermock.reflect.exceptions.MethodNotFoundException;7import org.powermock.reflect.exceptions.FieldNotFoundException;8import org.powermock.reflect.exceptions.FieldNotWritableException;9import org.powermock.reflect.exceptions.FieldNotReadableException;10import org.powermock.reflect.exceptions.FieldAlreadyInitializedException;11import org.powermock.reflect.exceptions.FieldNotStaticException;12import org.powermock.reflect.exceptions.FieldNotFinalException;13import org.powermock.reflect.exceptions.FieldNotDeclaredException;14import org.powermock.reflect.exceptions.FieldAlreadyDeclaredException;15import org.powermock.reflect.exceptions.MethodNotDeclaredException;16import org.powermock.reflect.exceptions.MethodNotStaticException;17import org.powermock.reflect.exceptions.MethodNotFinalExcepti

Full Screen

Full Screen

makeDeferConstructorNonPublic

Using AI Code Generation

copy

Full Screen

1package org.powermock.core.transformers.javassist.testclass;2import org.junit.Test;3import org.junit.runner.RunWith;4import org.powermock.core.transformers.javassist.testclass.JavaAssistTestClassTransformerTest.TestClass;5import org.powermock.modules.junit4.PowerMockRunner;6import org.powermock.reflect.Whitebox;7import java.io.ByteArrayOutputStream;8import java.io.File;9import java.io.IOException;10import java.io.PrintStream;11import java.net.URL;12import java.net.URLClassLoader;13import java.nio.file.Files;14import java.nio.file.Path;15import java.nio.file.Paths;16import java.util.jar.JarEntry;17import java.util.jar.JarOutputStream;18import java.util.jar.Manifest;19import java.util.zip.ZipEntry;20import static org.junit.Assert.assertEquals;21import static org.junit.Assert.assertTrue;22@RunWith(PowerMockRunner.class)23public class JavaAssistTestClassTransformerTest {24 public void test() throws Exception {25 final String className = TestClass.class.getName();26 final String classFileName = className.replace('.', '/') + ".class";27 final Path tempDir = Files.createTempDirectory("temp");28 final Path jarPath = Paths.get(tempDir.toString(), "temp.jar");29 final Path classFilePath = Paths.get(tempDir.toString(), classFileName);30 final Path transformedClassFilePath = Paths.get(tempDir.toString(), "transformed", classFileName);31 Files.createDirectories(transformedClassFilePath.getParent());32 Files.copy(Paths.get("target", "test-classes", classFileName), classFilePath);33 JavaAssistTestClassTransformer transformer = new JavaAssistTestClassTransformer();34 transformer.makeDeferConstructorNonPublic(className, classFilePath.toString(), transformedClassFilePath.toString());35 createJar(jarPath, tempDir);36 final ClassLoader classLoader = new URLClassLoader(new URL[] { jarPath.toUri().toURL() });37 final Class<?> clazz = classLoader.loadClass(className);38 final ByteArrayOutputStream baos = new ByteArrayOutputStream();39 final PrintStream printStream = new PrintStream(baos);40 Whitebox.invokeMethod(clazz, "test", printStream);41 printStream.flush();42 assertEquals("TestClass.test()", baos.toString());43 }44 private static void createJar(Path jarPath, Path tempDir) throws IOException {

Full Screen

Full Screen

makeDeferConstructorNonPublic

Using AI Code Generation

copy

Full Screen

1public void testMethodWithComment() {2 assertTrue(true);3}4public class TestClassWithComment {5 public void testMethod() {6 assertTrue(true);7 }8}9public class TestClassWithComment {10 public void testMethod() {11 assertTrue(true);12 }13}14public void testMethodWithComment() {15 assertTrue(true);16}

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