How to use assureOpen method of org.mockito.internal.util.reflection.InstrumentationMemberAccessor class

Best Mockito code snippet using org.mockito.internal.util.reflection.InstrumentationMemberAccessor.assureOpen

Source:InstrumentationMemberAccessor.java Github

copy

Full Screen

...159 Object module =160 DISPATCHER.invokeWithArguments(161 getModule.bindTo(constructor.getDeclaringClass()));162 String packageName = constructor.getDeclaringClass().getPackage().getName();163 assureOpen(module, packageName);164 MethodHandle handle =165 ((MethodHandles.Lookup)166 DISPATCHER.invokeWithArguments(167 privateLookupIn,168 constructor.getDeclaringClass(),169 DISPATCHER.getLookup()))170 .unreflectConstructor(constructor);171 AtomicBoolean thrown = new AtomicBoolean();172 Object value =173 onConstruction.invoke(174 () -> {175 try {176 return DISPATCHER.invokeWithArguments(handle, arguments);177 } catch (Throwable throwable) {178 thrown.set(true);179 return throwable;180 }181 });182 if (thrown.get()) {183 throw new InvocationTargetException((Throwable) value);184 } else {185 return value;186 }187 } catch (InvocationTargetException e) {188 throw e;189 } catch (Throwable t) {190 throw new IllegalStateException(191 "Could not construct "192 + constructor193 + " with arguments "194 + Arrays.toString(arguments),195 t);196 }197 }198 @Override199 public Object invoke(Method method, Object target, Object... arguments)200 throws InvocationTargetException {201 assureArguments(202 method,203 Modifier.isStatic(method.getModifiers()) ? null : target,204 method.getDeclaringClass(),205 arguments,206 method.getParameterTypes());207 try {208 Object module =209 DISPATCHER.invokeWithArguments(getModule.bindTo(method.getDeclaringClass()));210 String packageName = method.getDeclaringClass().getPackage().getName();211 assureOpen(module, packageName);212 MethodHandle handle =213 ((MethodHandles.Lookup)214 DISPATCHER.invokeWithArguments(215 privateLookupIn,216 method.getDeclaringClass(),217 DISPATCHER.getLookup()))218 .unreflect(method);219 if (!Modifier.isStatic(method.getModifiers())) {220 handle = handle.bindTo(target);221 }222 try {223 return DISPATCHER.invokeWithArguments(handle, arguments);224 } catch (Throwable t) {225 throw new InvocationTargetException(t);226 }227 } catch (InvocationTargetException e) {228 throw e;229 } catch (Throwable t) {230 throw new IllegalStateException(231 "Could not invoke "232 + method233 + " on "234 + target235 + " with arguments "236 + Arrays.toString(arguments),237 t);238 }239 }240 @Override241 public Object get(Field field, Object target) {242 assureArguments(243 field,244 Modifier.isStatic(field.getModifiers()) ? null : target,245 field.getDeclaringClass(),246 new Object[0],247 new Class<?>[0]);248 try {249 Object module =250 DISPATCHER.invokeWithArguments(getModule.bindTo(field.getDeclaringClass()));251 String packageName = field.getDeclaringClass().getPackage().getName();252 assureOpen(module, packageName);253 MethodHandle handle =254 ((MethodHandles.Lookup)255 DISPATCHER.invokeWithArguments(256 privateLookupIn,257 field.getDeclaringClass(),258 DISPATCHER.getLookup()))259 .unreflectGetter(field);260 if (!Modifier.isStatic(field.getModifiers())) {261 handle = handle.bindTo(target);262 }263 return DISPATCHER.invokeWithArguments(handle);264 } catch (Throwable t) {265 throw new IllegalStateException("Could not read " + field + " on " + target, t);266 }267 }268 @Override269 public void set(Field field, Object target, Object value) throws IllegalAccessException {270 assureArguments(271 field,272 Modifier.isStatic(field.getModifiers()) ? null : target,273 field.getDeclaringClass(),274 new Object[] {value},275 new Class<?>[] {field.getType()});276 boolean illegalAccess = false;277 try {278 Object module =279 DISPATCHER.invokeWithArguments(getModule.bindTo(field.getDeclaringClass()));280 String packageName = field.getDeclaringClass().getPackage().getName();281 assureOpen(module, packageName);282 // Method handles do not allow setting final fields where setAccessible(true)283 // is required before unreflecting.284 boolean isFinal;285 if (Modifier.isFinal(field.getModifiers())) {286 isFinal = true;287 try {288 DISPATCHER.setAccessible(field, true);289 } catch (Throwable ignored) {290 illegalAccess =291 true; // To distinguish from propagated illegal access exception.292 throw new IllegalAccessException(293 "Could not make final field " + field + " accessible");294 }295 } else {296 isFinal = false;297 }298 try {299 MethodHandle handle =300 ((MethodHandles.Lookup)301 DISPATCHER.invokeWithArguments(302 privateLookupIn,303 field.getDeclaringClass(),304 DISPATCHER.getLookup()))305 .unreflectSetter(field);306 if (!Modifier.isStatic(field.getModifiers())) {307 handle = handle.bindTo(target);308 }309 DISPATCHER.invokeWithArguments(handle, value);310 } finally {311 if (isFinal) {312 DISPATCHER.setAccessible(field, false);313 }314 }315 } catch (Throwable t) {316 if (illegalAccess) {317 throw (IllegalAccessException) t;318 } else {319 throw new IllegalStateException("Could not read " + field + " on " + target, t);320 }321 }322 }323 private void assureOpen(Object module, String packageName) throws Throwable {324 // It would be more reliable to check if a module's package already is opened to325 // the dispatcher module from before. Unfortunately, there is no reliable check326 // for doing so since the isOpen(String, Module) method always returns true327 // if the second argument is an unnamed module. Therefore, for now, we need328 // to reopen packages even if they are already opened to the dispatcher module.329 if (!(Boolean) DISPATCHER.invokeWithArguments(isOpen, module, packageName)) {330 DISPATCHER.invokeWithArguments(331 redefineModule.bindTo(INSTRUMENTATION),332 module,333 Collections.emptySet(),334 Collections.emptyMap(),335 Collections.singletonMap(336 packageName, Collections.singleton(DISPATCHER.getModule())),337 Collections.emptySet(),...

Full Screen

Full Screen

assureOpen

Using AI Code Generation

copy

Full Screen

1public class MockitoAssureOpenTest {2 public static void main(String[] args) {3 InstrumentationMemberAccessor memberAccessor = new InstrumentationMemberAccessor();4 try {5 memberAccessor.assureOpen();6 } catch (Exception e) {7 e.printStackTrace();8 }9 }10}11 at org.mockito.internal.util.reflection.InstrumentationMemberAccessor.assureOpen(InstrumentationMemberAccessor.java:27)12 at MockitoAssureOpenTest.main(MockitoAssureOpenTest.java:8)

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful