private void setRunning(MessageQueue mq) { Reflection.field("running").ofType(AtomicBoolean.class).in(mq).get().set(true); }
private JobHolder createNewJobHolderWithDelayUntil(Params params, long delayUntil) { JobHolder jobHolder = createNewJobHolder(params); getDelayUntilNsField(jobHolder).set(delayUntil); return jobHolder; }
T target = get(); DecoratorInvocationHandler<T> handler = new PostDecorator<T>(target, decorator); @SuppressWarnings("unchecked") T field = (T) Proxy.newProxyInstance(decorator.getClass().getClassLoader(), array(expectedType), handler); set(field); return DecoratedInvoker.newInvoker(target, decorator, expectedType, this, handler);
static Object getNestedField(String fieldName, Object target) { Field field = lookupInClassHierarchy(fieldName, typeOf(target)); return get(field, field.isAccessible(), target); }
private static <T> Invoker<T> createInvoker(String fieldName, Class<?> expectedType, Object target) { if (target == null) { throw new NullPointerException("Target should not be null"); } Field field = lookupInClassHierarchy(fieldName, typeOf(target)); verifyCorrectType(field, expectedType); return new Invoker<T>(target, field, expectedType); }
static <T> Invoker<T> newInvoker(String fieldName, Class<T> expectedType, Object target) { return createInvoker(fieldName, expectedType, target); }
T target = get(); DecoratorInvocationHandler<T> handler = new PreDecorator<T>(target, decorator); @SuppressWarnings("unchecked") T field = (T) Proxy.newProxyInstance(decorator.getClass().getClassLoader(), array(expectedType), handler); set(field); return DecoratedInvoker.newInvoker(target, decorator, expectedType, this, handler);
static <T> Invoker<T> newInvoker(String fieldName, TypeRef<T> expectedType, Object target) { return createInvoker(fieldName, expectedType.rawType(), target); }
public static JobHolder createNewJobHolder(Params params, Timer timer) { long delay = getDelayMsField(params).get(); long deadline = getDeadlineMsField(params).get(); boolean cancelOnDeadline = Boolean.TRUE.equals(getCancelOnDeadlineDeadlineField(params).get()); DummyJob job = new DummyJob(params); //noinspection WrongConstant return new JobHolder.Builder() .priority(params.getPriority()) .groupId(params.getGroupId()) .job(job) .id(job.getId()) .persistent(params.isPersistent()) .tags(job.getTags()) .createdNs(timer.nanoTime()) .deadline(deadline > 0 ? timer.nanoTime() + deadline * JobManager.NS_PER_MS : Params.FOREVER, cancelOnDeadline) .delayUntilNs(delay > 0 ? timer.nanoTime() + delay * JobManager.NS_PER_MS : JobManager.NOT_DELAYED_JOB_DELAY) .requiredNetworkType(getNetworkTypeField(params).get()) .runningSessionId(JobManager.NOT_RUNNING_SESSION_ID).build(); }
@Test public void testPriorityWithDelayedJobs() throws Exception { JobQueue jobQueue = createNewJobQueue(); JobHolder delayedPriority_5 = createNewJobHolder(new Params(5)); org.fest.reflect.field.Invoker<Long> delayUntilField = getDelayUntilNsField(delayedPriority_5); delayUntilField.set(mockTimer.nanoTime() - 1000); JobHolder delayedPriority_2 = createNewJobHolder(new Params(2)); delayUntilField = getDelayUntilNsField(delayedPriority_2); delayUntilField.set(mockTimer.nanoTime() - 500); JobHolder nonDelayedPriority_6 = createNewJobHolder(new Params(6)); JobHolder nonDelayedPriority_3 = createNewJobHolder(new Params(3)); JobHolder nonDelayedPriority_2 = createNewJobHolder(new Params(2)); jobQueue.insert(delayedPriority_5); jobQueue.insert(delayedPriority_2); jobQueue.insert(nonDelayedPriority_6); jobQueue.insert(nonDelayedPriority_2); jobQueue.insert(nonDelayedPriority_3); TestConstraint constraint = new TestConstraint(mockTimer); int lastPriority = Integer.MAX_VALUE; for (int i = 0; i < 5; i++) { JobHolder next = jobQueue.nextJobAndIncRunCount(constraint); assertThat("next job should not be null", next, notNullValue()); assertThat("next job's priority should be lower then previous for job " + i, next.getPriority() <= lastPriority, is(true)); lastPriority = next.getPriority(); } }
/** * Returns the value of the field managed by this class. * * @return the value of the field managed by this class. * @throws ReflectionError if the value of the field cannot be retrieved. */ public T get() { return Invoker.<T> get(field, accessible, target); }
@Provides public WebApplication webApp( @NotNull GuiceContainer guiceContainer ) { WebApplication copy = webApplicationReference; if ( copy == null ) { WebComponent component = Reflection.field( "webComponent" ).ofType( WebComponent.class ).in( guiceContainer ).get(); copy = Reflection.field( "application" ).ofType( WebApplication.class ).in( component ).get(); webApplicationReference = copy; } return copy; }
@Override @Nullable public Component focusOwner() { return field("focusOwner").ofType(Component.class).in(KeyboardFocusManager.class).get(); } }
@Nullable protected Object resolveId( @NotNull Object object ) { try { return Reflection.field( "id" ).ofType( Long.class ).in( object ).get(); } catch ( ReflectionError ignore ) { ignore.printStackTrace(); } return null; }
@Nullable protected Object resolveId( @Nonnull Object object ) { try { return Reflection.field( "id" ).ofType( Long.class ).in( object ).get(); } catch ( ReflectionError ignore ) { ignore.printStackTrace(); } return null; }
public JAnnotationFieldReference( @Nonnull JFieldVar ns ) { this.ns = ns; this.owner = Reflection.field( FIELD_OWNER ).ofType( JDefinedClass.class ).in( ns ).get(); }
@Nullable protected Object resolveId( @NotNull Object object ) { try { return Reflection.field( "id" ).ofType( Long.class ).in( object ).get(); } catch ( ReflectionError ignore ) { ignore.printStackTrace(); } return null; }
@Nullable protected Object resolveId( @NotNull Object object ) { try { return Reflection.field( "id" ).ofType( Long.class ).in( object ).get(); } catch ( ReflectionError ignore ) { ignore.printStackTrace(); } return null; }
@RunsInCurrentThread @Nonnull private static Container dockFor(final @Nonnull JToolBar toolBar) { try { return checkNotNull(field("dockingSource").ofType(Container.class).in(toolBar.getUI()).get()); } catch (RuntimeException e) { throw actionFailure("Unabled to determine dock for JToolBar"); } }
private static int modifiersFrom(@Nonnull String s) { if ("NO_MASK".equals(s)) { return NO_MASK; } try { Integer modifiers = field(s).ofType(int.class).in(InputEvent.class).get(); return checkNotNull(modifiers); } catch (ReflectionError e) { throw new ParsingException(concat("Unable to retrieve modifiers from text ", quote(s)), e.getCause()); } } }