Java Code Examples for org.apache.commons.lang3.reflect.FieldUtils#readDeclaredField()

The following examples show how to use org.apache.commons.lang3.reflect.FieldUtils#readDeclaredField() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PrebidServerAdapterTest.java    From prebid-mobile-android with Apache License 2.0 6 votes vote down vote up
private JSONObject getPostDataHelper(AdType adType, @Nullable Map<String, Set<String>> contextDataDictionary, @Nullable Set<String> contextKeywordsSet, @Nullable AdSize minSizePerc, @Nullable VideoBaseAdUnit.Parameters parameters) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {

        PrebidMobile.setApplicationContext(activity.getApplicationContext());

        server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBid()));

        DemandAdapter.DemandAdapterListener mockListener = mock(DemandAdapter.DemandAdapterListener.class);
        PrebidServerAdapter adapter = new PrebidServerAdapter();
        HashSet<AdSize> sizes = new HashSet<>();
        sizes.add(new AdSize(300, 250));

        RequestParams requestParams = new RequestParams("67890", adType, sizes, contextDataDictionary, contextKeywordsSet, minSizePerc, parameters);
        String uuid = UUID.randomUUID().toString();
        adapter.requestDemand(requestParams, mockListener, uuid);
        @SuppressWarnings("unchecked")
        ArrayList<PrebidServerAdapter.ServerConnector> connectors = (ArrayList<PrebidServerAdapter.ServerConnector>) FieldUtils.readDeclaredField(adapter, "serverConnectors", true);
        PrebidServerAdapter.ServerConnector connector = connectors.get(0);

        JSONObject postData = (JSONObject) MethodUtils.invokeMethod(connector, true, "getPostData");
        return postData;
    }
 
Example 2
Source File: ResultCodeTest.java    From prebid-mobile-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testTimeOut() throws Exception {
    PrebidMobile.setPrebidServerHost(Host.APPNEXUS);
    PrebidMobile.setTimeoutMillis(30);
    PrebidMobile.setPrebidServerAccountId("b7adad2c-e042-4126-8ca1-b3caac7d3e5c");
    PrebidMobile.setShareGeoLocation(true);
    PrebidMobile.setApplicationContext(activity.getApplicationContext());
    DemandAdapter.DemandAdapterListener mockListener = mock(DemandAdapter.DemandAdapterListener.class);
    PrebidServerAdapter adapter = new PrebidServerAdapter();
    HashSet<AdSize> sizes = new HashSet<>();
    sizes.add(new AdSize(0, 250));
    RequestParams requestParams = new RequestParams("e2edc23f-0b3b-4203-81b5-7cc97132f418", AdType.BANNER, sizes);
    String uuid = UUID.randomUUID().toString();
    adapter.requestDemand(requestParams, mockListener, uuid);

    @SuppressWarnings("unchecked")
    ArrayList<PrebidServerAdapter.ServerConnector> connectors = (ArrayList<PrebidServerAdapter.ServerConnector>) FieldUtils.readDeclaredField(adapter, "serverConnectors", true);
    PrebidServerAdapter.ServerConnector connector = connectors.get(0);
    PrebidServerAdapter.ServerConnector.TimeoutCountDownTimer timeoutCountDownTimer = (PrebidServerAdapter.ServerConnector.TimeoutCountDownTimer) FieldUtils.readDeclaredField(connector, "timeoutCountDownTimer", true);
    shadowOf(timeoutCountDownTimer).invokeFinish();

    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onDemandFailed(ResultCode.TIMEOUT, uuid);
}
 
Example 3
Source File: LocalMessageCache.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private ReadWriteLock getLockInProcessQueue(ProcessQueue pq) {
    try {
        return (ReadWriteLock) FieldUtils.readDeclaredField(pq, "lockTreeMap", true);
    } catch (IllegalAccessException e) {
        return null;
    }
}
 
Example 4
Source File: PrebidServerAdapterTest.java    From prebid-mobile-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testRewardedVideoAdUnit() throws Exception {
    //given
    PrebidMobile.setPrebidServerAccountId("12345");

    //when
    PrebidMobile.setApplicationContext(activity.getApplicationContext());
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBid()));
    DemandAdapter.DemandAdapterListener mockListener = mock(DemandAdapter.DemandAdapterListener.class);
    PrebidServerAdapter adapter = new PrebidServerAdapter();
    HashSet<AdSize> sizes = new HashSet<>();
    sizes.add(new AdSize(500, 700));

    RequestParams requestParams = new RequestParams("67890", AdType.REWARDED_VIDEO, sizes);
    String uuid = UUID.randomUUID().toString();
    adapter.requestDemand(requestParams, mockListener, uuid);
    @SuppressWarnings("unchecked")
    ArrayList<PrebidServerAdapter.ServerConnector> connectors = (ArrayList<PrebidServerAdapter.ServerConnector>) FieldUtils.readDeclaredField(adapter, "serverConnectors", true);
    PrebidServerAdapter.ServerConnector connector = connectors.get(0);

    JSONObject postData = (JSONObject) MethodUtils.invokeMethod(connector, true, "getPostData");

    JSONObject video = postData.getJSONArray("imp").getJSONObject(0).getJSONObject("video");

    JSONObject cache = postData.getJSONObject("ext").getJSONObject("prebid").getJSONObject("cache");
    JSONObject vastxml = cache.getJSONObject("vastxml");

    int instl = postData.getJSONArray("imp").getJSONObject(0).getInt("instl");

    int isRewarded = postData.getJSONArray("imp").getJSONObject(0).getJSONObject("ext").getJSONObject("prebid").getInt("is_rewarded_inventory");

    //then
    assertEquals(5, video.getInt("placement"));
    assertEquals(1, video.getInt("linearity"));
    assertEquals(1, instl);
    assertEquals(1, isRewarded);
    assertNotNull(vastxml);
}
 
Example 5
Source File: PlainPermissionManagerTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void cleanAuthenticationInfoTest() throws IllegalAccessException {
    // PlainPermissionManager.addPlainAccessResource(plainAccessResource);
    Map<String, List<PlainAccessResource>> plainAccessResourceMap = (Map<String, List<PlainAccessResource>>) FieldUtils.readDeclaredField(plainPermissionManager, "plainAccessResourceMap", true);
    Assert.assertFalse(plainAccessResourceMap.isEmpty());

    plainPermissionManager.clearPermissionInfo();
    plainAccessResourceMap = (Map<String, List<PlainAccessResource>>) FieldUtils.readDeclaredField(plainPermissionManager, "plainAccessResourceMap", true);
    Assert.assertTrue(plainAccessResourceMap.isEmpty());
    // RemoveDataVersionFromYamlFile("src/test/resources/conf/plain_acl.yml");
}
 
Example 6
Source File: ContentMergePlugin.java    From dolphin with Apache License 2.0 5 votes vote down vote up
@Override
public boolean sqlMapGenerated(GeneratedXmlFile sqlMap, IntrospectedTable introspectedTable) {
  SqlMapGeneratorConfiguration smgc = context.getSqlMapGeneratorConfiguration();
  try {
    Document document = (Document) FieldUtils.readDeclaredField(sqlMap, "document", true);
    File targetFile = getTargetFile(smgc.getTargetPackage(), sqlMap.getFileName());
    if (!targetFile.exists()) { // 第一次生成直接使用当前生成的文件
      return true;
    }
    visitAndMerge(document, targetFile);
  } catch (ShellException | IOException | IllegalAccessException | DocumentException e) {
    e.printStackTrace();
  }
  return true;
}
 
Example 7
Source File: PrebidServerAdapterTest.java    From prebid-mobile-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testPostDataWithStoredResponses() throws Exception {
    //given
    PrebidMobile.setStoredAuctionResponse("111122223333");
    PrebidMobile.addStoredBidResponse("appnexus", "221144");
    PrebidMobile.addStoredBidResponse("rubicon", "221155");

    //when
    PrebidMobile.setApplicationContext(activity.getApplicationContext());
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBid()));
    DemandAdapter.DemandAdapterListener mockListener = mock(DemandAdapter.DemandAdapterListener.class);
    PrebidServerAdapter adapter = new PrebidServerAdapter();
    HashSet<AdSize> sizes = new HashSet<>();
    sizes.add(new AdSize(500, 700));

    RequestParams requestParams = new RequestParams("67890", AdType.INTERSTITIAL, sizes);
    String uuid = UUID.randomUUID().toString();
    adapter.requestDemand(requestParams, mockListener, uuid);
    @SuppressWarnings("unchecked")
    ArrayList<PrebidServerAdapter.ServerConnector> connectors = (ArrayList<PrebidServerAdapter.ServerConnector>) FieldUtils.readDeclaredField(adapter, "serverConnectors", true);
    PrebidServerAdapter.ServerConnector connector = connectors.get(0);

    JSONObject postData = (JSONObject) MethodUtils.invokeMethod(connector, true, "getPostData");
    JSONObject prebid = postData.getJSONArray("imp").getJSONObject(0).getJSONObject("ext").getJSONObject("prebid");
    JSONObject storedauctionresponse = prebid.getJSONObject("storedauctionresponse");
    JSONArray storedbidresponse = prebid.getJSONArray("storedbidresponse");
    JSONObject storedbidresponse1 = storedbidresponse.getJSONObject(0);
    JSONObject storedbidresponse2 = storedbidresponse.getJSONObject(1);

    //then
    assertEquals("111122223333", storedauctionresponse.getString("id"));

    Assert.assertEquals(2, storedbidresponse.length());
    assertEquals("appnexus", storedbidresponse1.getString("bidder"));
    assertEquals("221144", storedbidresponse1.getString("id"));
    assertEquals("rubicon", storedbidresponse2.getString("bidder"));
    assertEquals("221155", storedbidresponse2.getString("id"));

}
 
Example 8
Source File: LocalMessageCache.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private ReadWriteLock getLockInProcessQueue(ProcessQueue pq) {
    try {
        return (ReadWriteLock) FieldUtils.readDeclaredField(pq, "lockTreeMap", true);
    } catch (IllegalAccessException e) {
        return null;
    }
}
 
Example 9
Source File: XMLMergePlugin.java    From dolphin with Apache License 2.0 5 votes vote down vote up
@Override
public boolean sqlMapGenerated(GeneratedXmlFile sqlMap, IntrospectedTable introspectedTable) {
  SqlMapGeneratorConfiguration smgc = context.getSqlMapGeneratorConfiguration();
  try {
    Document document = (Document) FieldUtils.readDeclaredField(sqlMap, "document", true);
    File targetFile = getTargetFile(smgc.getTargetPackage(), sqlMap.getFileName());
    if (!targetFile.exists()) { // 第一次生成直接使用当前生成的文件
      return true;
    }
    visitAndMerge(document, targetFile);
  } catch (ShellException | IOException | IllegalAccessException | DocumentException e) {
    e.printStackTrace();
  }
  return true;
}
 
Example 10
Source File: LocalMessageCache.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private ReadWriteLock getLockInProcessQueue(ProcessQueue pq) {
    try {
        return (ReadWriteLock) FieldUtils.readDeclaredField(pq, "lockTreeMap", true);
    } catch (IllegalAccessException e) {
        return null;
    }
}
 
Example 11
Source File: PrebidServerAdapterTest.java    From prebid-mobile-android with Apache License 2.0 4 votes vote down vote up
@Test
public void testTargetingParamsInPostData() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBid()));
    HttpUrl hostUrl = server.url("/");
    Host.CUSTOM.setHostUrl(hostUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    PrebidMobile.setPrebidServerAccountId("12345");
    PrebidMobile.setShareGeoLocation(true);
    PrebidMobile.setApplicationContext(activity.getApplicationContext());
    TargetingParams.setYearOfBirth(1989);
    TargetingParams.setGender(TargetingParams.GENDER.FEMALE);
    TargetingParams.setBundleName("org.prebid.mobile");
    TargetingParams.setDomain("prebid.org");
    TargetingParams.setStoreUrl("store://app");
    TargetingParams.setSubjectToGDPR(true);
    TargetingParams.setSubjectToCOPPA(true);
    TargetingParams.setGDPRConsentString("testGDPR");
    DemandAdapter.DemandAdapterListener mockListener = mock(DemandAdapter.DemandAdapterListener.class);
    PrebidServerAdapter adapter = new PrebidServerAdapter();
    HashSet<AdSize> sizes = new HashSet<>();
    sizes.add(new AdSize(320, 50));
    RequestParams requestParams = new RequestParams("67890", AdType.BANNER, sizes);
    String uuid = UUID.randomUUID().toString();
    adapter.requestDemand(requestParams, mockListener, uuid);
    @SuppressWarnings("unchecked")
    ArrayList<PrebidServerAdapter.ServerConnector> connectors = (ArrayList<PrebidServerAdapter.ServerConnector>) FieldUtils.readDeclaredField(adapter, "serverConnectors", true);
    PrebidServerAdapter.ServerConnector connector = connectors.get(0);
    assertEquals(uuid, connector.getAuctionId());
    JSONObject postData = (JSONObject) MethodUtils.invokeMethod(connector, true, "getPostData");
    assertEquals(8, postData.length());
    assertTrue(postData.has("id"));
    assertTrue(postData.has("source"));
    assertTrue(postData.has("imp"));
    assertTrue(postData.has("device"));
    assertTrue(postData.has("app"));
    assertTrue(postData.has("user"));
    assertTrue(postData.has("regs"));
    assertTrue(postData.has("ext"));
    JSONObject imp = postData.getJSONArray("imp").getJSONObject(0);
    assertEquals("PrebidMobile", imp.getString("id"));
    assertEquals(1, imp.getInt("secure"));
    assertEquals(1, imp.getJSONObject("banner").getJSONArray("format").length());
    assertEquals(320, imp.getJSONObject("banner").getJSONArray("format").getJSONObject(0).getInt("w"));
    assertEquals(50, imp.getJSONObject("banner").getJSONArray("format").getJSONObject(0).getInt("h"));
    assertEquals(67890, imp.getJSONObject("ext").getJSONObject("prebid").getJSONObject("storedrequest").getInt("id"));
    JSONObject device = postData.getJSONObject("device");
    assertEquals(PrebidServerSettings.deviceMake, device.getString("make"));
    assertEquals(PrebidServerSettings.deviceModel, device.getString("model"));
    assertEquals(0, device.getInt("lmt"));
    assertEquals(PrebidServerSettings.os, device.getString("os"));
    assertEquals(Locale.getDefault().getLanguage(), device.getString("language"));
    assertEquals(String.valueOf(BaseSetup.testSDK), device.getString("osv"));
    assertEquals(320, device.getInt("w"));
    assertEquals(0, device.getInt("h"));
    assertEquals(1, device.getInt("pxratio"));
    assertEquals(2, device.getInt("connectiontype"));
    JSONObject app = postData.getJSONObject("app");
    assertEquals("org.prebid.mobile", app.getString("bundle"));
    assertEquals("prebid.org", app.getString("domain"));
    assertEquals("store://app", app.getString("storeurl"));
    assertEquals("12345", app.getJSONObject("publisher").getString("id"));
    assertEquals("prebid-mobile", app.getJSONObject("ext").getJSONObject("prebid").getString("source"));
    assertEquals(PrebidServerSettings.sdk_version, app.getJSONObject("ext").getJSONObject("prebid").getString("version"));
    JSONObject user = postData.getJSONObject("user");
    assertEquals(3, user.length());
    assertEquals(1989, user.getInt("yob"));
    assertEquals("F", user.getString("gender"));
    assertEquals("testGDPR", user.getJSONObject("ext").getString("consent"));
    JSONObject regs = postData.getJSONObject("regs");
    assertEquals(1, regs.getInt("coppa"));
    assertEquals(1, regs.getJSONObject("ext").getInt("gdpr"));
    JSONObject ext = postData.getJSONObject("ext");
    assertTrue(ext.getJSONObject("prebid").has("cache"));
    assertTrue(ext.getJSONObject("prebid").getJSONObject("cache").has("bids"));
    assertEquals(0, ext.getJSONObject("prebid").getJSONObject("cache").getJSONObject("bids").length());
    assertEquals("12345", ext.getJSONObject("prebid").getJSONObject("storedrequest").getString("id"));
    assertTrue(ext.getJSONObject("prebid").has("targeting"));
}
 
Example 12
Source File: HttpWebConnection.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T> T getField(final Object target, final String fieldName) throws IllegalAccessException {
    return (T) FieldUtils.readDeclaredField(target, fieldName, true);
}
 
Example 13
Source File: UnsafeUtils.java    From confucius-commons with Apache License 2.0 3 votes vote down vote up
/**
 * 获取对象数组字段中目标索引的<code>double</code>值
 *
 * @param object
 *         对象
 * @param fieldName
 *         字段名称
 * @param index
 *         元素索引
 * @return 目标索引的值
 */
public static double getDoubleFromArrayVolatile(Object object, String fieldName, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException, IllegalAccessException {
    Object array = FieldUtils.readDeclaredField(object, fieldName, true);
    assertArrayIndex(array, index);
    long offset = doubleArrayIndexOffset(index);
    return unsafe.getDoubleVolatile(array, offset);
}
 
Example 14
Source File: UnsafeUtils.java    From confucius-commons with Apache License 2.0 3 votes vote down vote up
/**
 * 给指定对象的字段设置给定的<code>short</code>值
 *
 * @param object
 *         对象
 * @param fieldName
 *         字段名称
 * @param index
 *         元素索引
 * @param value
 *         <code>short</code>值
 * @throws IllegalArgumentException
 *         参考{@link ReflectionUtils#assertArrayType(Object)}
 * @throws ArrayIndexOutOfBoundsException
 *         当<code>index</code>小于0,或者大于或等于数组长度
 */
public static void putShortIntoArrayVolatile(Object object, String fieldName, int index, short value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException, IllegalAccessException {
    Object array = FieldUtils.readDeclaredField(object, fieldName, true);
    assertArrayIndex(array, index);
    long offset = shortArrayIndexOffset(index);
    unsafe.putShortVolatile(array, offset, value);
}
 
Example 15
Source File: UnsafeUtils.java    From confucius-commons with Apache License 2.0 3 votes vote down vote up
/**
 * 获取对象数组字段中目标索引的<code>java.lang.Object</code>值
 *
 * @param object
 *         对象
 * @param fieldName
 *         字段名称
 * @param index
 *         元素索引
 * @return 目标索引的值
 * @throws IllegalArgumentException
 *         参考{@link ReflectionUtils#assertArrayType(Object)}
 * @throws ArrayIndexOutOfBoundsException
 *         当<code>index</code>小于0,或者大于或等于数组长度
 */
public static Object getObjectFromArrayVolatile(Object object, String fieldName, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException, IllegalAccessException {
    Object array = FieldUtils.readDeclaredField(object, fieldName, true);
    assertArrayIndex(array, index);
    long offset = objectArrayIndexOffset(index);
    return unsafe.getObjectVolatile(array, offset);
}
 
Example 16
Source File: UnsafeUtils.java    From confucius-commons with Apache License 2.0 3 votes vote down vote up
/**
 * 给指定对象的字段设置给定的<code>int</code>值(顺序写入)
 *
 * @param object
 *         对象
 * @param fieldName
 *         字段名称
 * @param index
 *         元素索引
 * @param value
 *         <code>int</code>值
 * @throws IllegalArgumentException
 *         参考{@link ReflectionUtils#assertArrayType(Object)}
 * @throws ArrayIndexOutOfBoundsException
 *         当<code>index</code>小于0,或者大于或等于数组长度
 */
public static void putOrderedIntIntoArray(Object object, String fieldName, int index, int value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException, IllegalAccessException {
    Object array = FieldUtils.readDeclaredField(object, fieldName, true);
    assertArrayIndex(array, index);
    long offset = intArrayIndexOffset(index);
    unsafe.putOrderedInt(array, offset, value);
}
 
Example 17
Source File: UnsafeUtils.java    From confucius-commons with Apache License 2.0 3 votes vote down vote up
/**
 * 给指定对象的字段设置给定的<code>boolean</code>值
 *
 * @param object
 *         对象
 * @param fieldName
 *         字段名称
 * @param index
 *         元素索引
 * @param value
 *         <code>boolean</code>值
 * @throws IllegalArgumentException
 *         参考{@link ReflectionUtils#assertArrayType(Object)}
 * @throws ArrayIndexOutOfBoundsException
 *         当<code>index</code>小于0,或者大于或等于数组长度
 */
public static void putBooleanIntoArrayVolatile(Object object, String fieldName, int index, boolean value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException, IllegalAccessException {
    Object array = FieldUtils.readDeclaredField(object, fieldName, true);
    assertArrayIndex(array, index);
    long offset = booleanArrayIndexOffset(index);
    unsafe.putBooleanVolatile(array, offset, value);
}
 
Example 18
Source File: UnsafeUtils.java    From confucius-commons with Apache License 2.0 3 votes vote down vote up
/**
 * 获取对象数组字段中目标索引的<code>short</code>值
 *
 * @param object
 *         对象
 * @param fieldName
 *         字段名称
 * @param index
 *         元素索引
 * @return 目标索引的值
 */
public static short getShortFromArrayVolatile(Object object, String fieldName, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException, IllegalAccessException {
    Object array = FieldUtils.readDeclaredField(object, fieldName, true);
    assertArrayIndex(array, index);
    long offset = shortArrayIndexOffset(index);
    return unsafe.getShortVolatile(array, offset);
}
 
Example 19
Source File: UnsafeUtils.java    From confucius-commons with Apache License 2.0 3 votes vote down vote up
/**
 * 获取对象数组字段中目标索引的<code>byte</code>值
 *
 * @param object
 *         对象
 * @param fieldName
 *         字段名称
 * @param index
 *         元素索引
 * @return 目标索引的值
 */
public static byte getByteFromArrayVolatile(Object object, String fieldName, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException, IllegalAccessException {
    Object array = FieldUtils.readDeclaredField(object, fieldName, true);
    assertArrayIndex(array, index);
    long offset = byteArrayIndexOffset(index);
    return unsafe.getByteVolatile(array, offset);
}
 
Example 20
Source File: UnsafeUtils.java    From confucius-commons with Apache License 2.0 3 votes vote down vote up
/**
 * 获取对象数组字段中目标索引的<code>float</code>值
 *
 * @param object
 *         对象
 * @param fieldName
 *         字段名称
 * @param index
 *         元素索引
 * @return 目标索引的值
 */
public static float getFloatFromArrayVolatile(Object object, String fieldName, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException, IllegalAccessException {
    Object array = FieldUtils.readDeclaredField(object, fieldName, true);
    assertArrayIndex(array, index);
    long offset = floatArrayIndexOffset(index);
    return unsafe.getFloatVolatile(array, offset);
}