How to use ModuleSystemFound method of org.mockito.internal.creation.bytebuddy.ModuleHandler class

Best Mockito code snippet using org.mockito.internal.creation.bytebuddy.ModuleHandler.ModuleSystemFound

Source:ModuleHandler.java Github

copy

Full Screen

...25 abstract Class<?> injectionBase(ClassLoader classLoader, String tyoeName);26 abstract void adjustModuleGraph(Class<?> source, Class<?> target, boolean export, boolean read);27 static ModuleHandler make(ByteBuddy byteBuddy, SubclassLoader loader, Random random) {28 try {29 return new ModuleSystemFound(byteBuddy, loader, random);30 } catch (Exception ignored) {31 return new NoModuleSystemFound();32 }33 }34 private static class ModuleSystemFound extends ModuleHandler {35 private final ByteBuddy byteBuddy;36 private final SubclassLoader loader;37 private final Random random;38 private final int injectonBaseSuffix;39 private final Method getModule,40 isOpen,41 isExported,42 isExportedUnqualified,43 canRead,44 addExports,45 addReads,46 addOpens,47 forName;48 private ModuleSystemFound(ByteBuddy byteBuddy, SubclassLoader loader, Random random)49 throws Exception {50 this.byteBuddy = byteBuddy;51 this.loader = loader;52 this.random = random;53 injectonBaseSuffix = Math.abs(random.nextInt());54 Class<?> moduleType = Class.forName("java.lang.Module");55 getModule = Class.class.getMethod("getModule");56 isOpen = moduleType.getMethod("isOpen", String.class, moduleType);57 isExported = moduleType.getMethod("isExported", String.class, moduleType);58 isExportedUnqualified = moduleType.getMethod("isExported", String.class);59 canRead = moduleType.getMethod("canRead", moduleType);60 addExports = moduleType.getMethod("addExports", String.class, moduleType);61 addReads = moduleType.getMethod("addReads", moduleType);62 addOpens = moduleType.getMethod("addOpens", String.class, moduleType);63 forName = Class.class.getMethod("forName", String.class);64 }65 @Override66 boolean isOpened(Class<?> source, Class<?> target) {67 if (source.getPackage() == null) {68 return true;69 }70 return (Boolean)71 invoke(72 isOpen,73 invoke(getModule, source),74 source.getPackage().getName(),75 invoke(getModule, target));76 }77 @Override78 boolean canRead(Class<?> source, Class<?> target) {79 return (Boolean) invoke(canRead, invoke(getModule, source), invoke(getModule, target));80 }81 @Override82 boolean isExported(Class<?> source) {83 if (source.getPackage() == null) {84 return true;85 }86 return (Boolean)87 invoke(88 isExportedUnqualified,89 invoke(getModule, source),90 source.getPackage().getName());91 }92 @Override93 boolean isExported(Class<?> source, Class<?> target) {94 if (source.getPackage() == null) {95 return true;96 }97 return (Boolean)98 invoke(99 isExported,100 invoke(getModule, source),101 source.getPackage().getName(),102 invoke(getModule, target));103 }104 @Override105 Class<?> injectionBase(ClassLoader classLoader, String typeName) {106 String packageName = typeName.substring(0, typeName.lastIndexOf('.'));107 if (classLoader == InjectionBase.class.getClassLoader()108 && InjectionBase.class.getPackage().getName().equals(packageName)) {109 return InjectionBase.class;110 } else {111 synchronized (this) {112 String name;113 int suffix = injectonBaseSuffix;114 do {115 name =116 packageName117 + "."118 + InjectionBase.class.getSimpleName()119 + "$"120 + suffix++;121 try {122 Class<?> type = Class.forName(name, false, classLoader);123 // The injected type must be defined in the class loader that is target124 // of the injection. Otherwise,125 // the class's unnamed module would differ from the intended module. To126 // avoid conflicts, we increment127 // the suffix until we hit a class with a known name and generate one if128 // it does not exist.129 if (type.getClassLoader() == classLoader) {130 return type;131 }132 } catch (ClassNotFoundException ignored) {133 break;134 }135 } while (true);136 return byteBuddy137 .subclass(Object.class, ConstructorStrategy.Default.NO_CONSTRUCTORS)138 .name(name)139 .make()140 .load(141 classLoader,142 loader.resolveStrategy(InjectionBase.class, classLoader, false))143 .getLoaded();144 }145 }146 }147 @Override148 void adjustModuleGraph(Class<?> source, Class<?> target, boolean export, boolean read) {149 boolean needsExport = export && !isExported(source, target);150 boolean needsRead = read && !canRead(source, target);151 if (!needsExport && !needsRead) {152 return;153 }154 ClassLoader classLoader = source.getClassLoader();155 if (classLoader == null) {156 throw new MockitoException(157 join(158 "Cannot adjust module graph for modules in the bootstrap loader",159 "",160 source161 + " is declared by the bootstrap loader and cannot be adjusted",162 "Requires package export to " + target + ": " + needsExport,163 "Requires adjusted reading of " + target + ": " + needsRead));164 }165 boolean targetVisible = classLoader == target.getClassLoader();166 while (!targetVisible && classLoader != null) {167 classLoader = classLoader.getParent();168 targetVisible = classLoader == target.getClassLoader();169 }170 MethodCall targetLookup;171 Implementation.Composable implementation;172 if (targetVisible) {173 targetLookup =174 MethodCall.invoke(getModule)175 .onMethodCall(MethodCall.invoke(forName).with(target.getName()));176 implementation = StubMethod.INSTANCE;177 } else {178 Class<?> intermediate;179 Field field;180 try {181 intermediate =182 byteBuddy183 .subclass(184 Object.class,185 ConstructorStrategy.Default.NO_CONSTRUCTORS)186 .name(187 String.format(188 "%s$%d",189 "org.mockito.codegen.MockitoTypeCarrier",190 Math.abs(random.nextInt())))191 .defineField(192 "mockitoType",193 Class.class,194 Visibility.PUBLIC,195 Ownership.STATIC)196 .make()197 .load(198 source.getClassLoader(),199 loader.resolveStrategy(200 source, source.getClassLoader(), false))201 .getLoaded();202 field = intermediate.getField("mockitoType");203 field.set(null, target);204 } catch (Exception e) {205 throw new MockitoException(206 join(207 "Could not create a carrier for making the Mockito type visible to "208 + source,209 "",210 "This is required to adjust the module graph to enable mock creation"),211 e);212 }213 targetLookup = MethodCall.invoke(getModule).onField(field);214 implementation =215 MethodCall.invoke(getModule)216 .onMethodCall(217 MethodCall.invoke(forName).with(intermediate.getName()));218 }219 MethodCall sourceLookup =220 MethodCall.invoke(getModule)221 .onMethodCall(MethodCall.invoke(forName).with(source.getName()));222 if (needsExport) {223 implementation =224 implementation.andThen(225 MethodCall.invoke(addExports)226 .onMethodCall(sourceLookup)227 .with(target.getPackage().getName())228 .withMethodCall(targetLookup));229 }230 if (needsRead) {231 implementation =232 implementation.andThen(233 MethodCall.invoke(addReads)234 .onMethodCall(sourceLookup)235 .withMethodCall(targetLookup));236 }237 try {238 Class.forName(239 byteBuddy240 .subclass(Object.class)241 .name(242 String.format(243 "%s$%s$%d",244 source.getName(),245 "MockitoModuleProbe",246 Math.abs(random.nextInt())))247 .invokable(isTypeInitializer())248 .intercept(implementation)249 .make()250 .load(251 source.getClassLoader(),252 loader.resolveStrategy(253 source, source.getClassLoader(), false))254 .getLoaded()255 .getName(),256 true,257 source.getClassLoader());258 } catch (Exception e) {259 throw new MockitoException(260 join(261 "Could not force module adjustment of the module of " + source,262 "",263 "This is required to adjust the module graph to enable mock creation"),264 e);265 }266 }267 private static Object invoke(Method method, Object target, Object... args) {268 try {269 return method.invoke(target, args);270 } catch (Exception e) {271 throw new MockitoException(272 join(273 "Could not invoke " + method + " using reflection",274 "",275 "Mockito attempted to interact with the Java module system but an unexpected method behavior was encountered"),276 e);277 }278 }279 }280 private static class NoModuleSystemFound extends ModuleHandler {281 @Override282 boolean isOpened(Class<?> source, Class<?> target) {283 return true;284 }285 @Override286 boolean canRead(Class<?> source, Class<?> target) {287 return true;288 }289 @Override290 boolean isExported(Class<?> source) {291 return true;292 }293 @Override294 boolean isExported(Class<?> source, Class<?> target) {...

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.

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in ModuleHandler

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful