Example usage for java.lang.reflect Method setAccessible

List of usage examples for java.lang.reflect Method setAccessible

Introduction

In this page you can find the example usage for java.lang.reflect Method setAccessible.

Prototype

@Override
@CallerSensitive
public void setAccessible(boolean flag) 

Source Link

Usage

From source file:hoot.services.controllers.ingest.RasterToTilesResourceTest.java

@Test
@Category(UnitTest.class)
public void TestIngestOSMResource() throws Exception {
    String processScriptName = RASTER_TO_TILES;
    Assert.assertNotNull(processScriptName);
    Assert.assertTrue(!processScriptName.isEmpty());

    RasterToTilesService rts = new RasterToTilesService();

    JSONObject oExpected = new JSONObject();
    oExpected.put("caller", "RasterToTilesService");
    oExpected.put("exec", processScriptName);

    JSONArray params = new JSONArray();
    JSONObject param = new JSONObject();
    param.put("RASTER_OUTPUT_DIR", tileServerPath);
    params.add(param);/*from  ww w .  jav a 2 s.  com*/

    param = new JSONObject();
    param.put("INPUT", "test");
    params.add(param);

    param = new JSONObject();
    param.put("ZOOM_LIST", "0-1 2-3");
    params.add(param);

    param = new JSONObject();
    param.put("RASTER_SIZE", "500");
    params.add(param);

    param = new JSONObject();
    param.put("MAP_ID", "1");
    params.add(param);

    oExpected.put("params", params);

    oExpected.put("exectype", "make");
    oExpected.put("erroraswarning", "true");

    Method createCommandMethod = RasterToTilesService.class.getDeclaredMethod("createCommand", String.class,
            String.class, int.class, long.class);

    createCommandMethod.setAccessible(true);

    String actual = (String) createCommandMethod.invoke(rts, "test", "0-1 2-3", 500, 1);

    JSONParser parser = new JSONParser();
    JSONObject actualObj = (JSONObject) parser.parse(actual);

    Assert.assertEquals(oExpected, actualObj);
}

From source file:de.thkwalter.koordinatensystem.AchsendimensionierungTest.java

/**
 * Test fr die Methode {@link Achsendimensionierung#ursprungEinbeziehen(Wertebereich)}.
 * /*  w w w .ja v a2s .c om*/
 * @throws NoSuchMethodException 
 * @throws SecurityException 
 * @throws InvocationTargetException 
 * @throws IllegalAccessException 
 * @throws IllegalArgumentException 
 */
@Test
public void testUrsprungEinbeziehen1() throws SecurityException, NoSuchMethodException,
        IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    // Die Testdaten werden erstellt.
    Wertebereich wertebereichPunktemenge = new Wertebereich(10.0, 9.0, 1.0, 2.0);

    // Die zu testende Methode wird ausgefhrt.
    Method method = Achsendimensionierung.class.getDeclaredMethod("ursprungEinbeziehen", Wertebereich.class);
    method.setAccessible(true);
    Wertebereich wertebereich = (Wertebereich) method.invoke(this.achsendimensionierung,
            wertebereichPunktemenge);

    // Es wird berprft, ob der korrekte Wertebereich bestimmt worden ist.
    assertEquals(wertebereichPunktemenge.getMaxX(), wertebereich.getMaxX(), 0.0);
    assertEquals(wertebereichPunktemenge.getMaxY(), wertebereich.getMaxY(), 0.0);
    assertEquals(0.0, wertebereich.getMinX(), 0.0);
    assertEquals(0.0, wertebereich.getMinY(), 0.0);
}

From source file:de.thkwalter.koordinatensystem.AchsendimensionierungTest.java

/**
 * Test fr die Methode {@link Achsendimensionierung#ursprungEinbeziehen(Wertebereich)}.
 * /*w  ww .java  2s.c  o m*/
 * @throws NoSuchMethodException 
 * @throws SecurityException 
 * @throws InvocationTargetException 
 * @throws IllegalAccessException 
 * @throws IllegalArgumentException 
 */
@Test
public void testUrsprungEinbeziehen2() throws SecurityException, NoSuchMethodException,
        IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    // Die Testdaten werden erstellt.
    Wertebereich wertebereichPunktemenge = new Wertebereich(-1.0, -2.0, -3.0, -4.0);

    // Die zu testende Methode wird ausgefhrt.
    Method method = Achsendimensionierung.class.getDeclaredMethod("ursprungEinbeziehen", Wertebereich.class);
    method.setAccessible(true);
    Wertebereich wertebereich = (Wertebereich) method.invoke(this.achsendimensionierung,
            wertebereichPunktemenge);

    // Es wird berprft, ob der korrekte Wertebereich bestimmt worden ist.
    assertEquals(0.0, wertebereich.getMaxX(), 0.0);
    assertEquals(0.0, wertebereich.getMaxY(), 0.0);
    assertEquals(wertebereichPunktemenge.getMinX(), wertebereich.getMinX(), 0.0);
    assertEquals(wertebereichPunktemenge.getMinY(), wertebereich.getMinY(), 0.0);
}

From source file:de.thkwalter.koordinatensystem.AchsendimensionierungTest.java

/**
 * Test fr die Methode {@link Achsendimensionierung#ursprungEinbeziehen(Wertebereich)}.
 * //from   w ww  .  j ava 2s.c o m
 * @throws NoSuchMethodException 
 * @throws SecurityException 
 * @throws InvocationTargetException 
 * @throws IllegalAccessException 
 * @throws IllegalArgumentException 
 */
@Test
public void testUrsprungEinbeziehen3() throws SecurityException, NoSuchMethodException,
        IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    // Die Testdaten werden erstellt.
    Wertebereich wertebereichPunktemenge = new Wertebereich(1.0, 2.0, -3.0, -4.0);

    // Die zu testende Methode wird ausgefhrt.
    Method method = Achsendimensionierung.class.getDeclaredMethod("ursprungEinbeziehen", Wertebereich.class);
    method.setAccessible(true);
    Wertebereich wertebereich = (Wertebereich) method.invoke(this.achsendimensionierung,
            wertebereichPunktemenge);

    // Es wird berprft, ob der korrekte Wertebereich bestimmt worden ist.
    assertEquals(wertebereichPunktemenge.getMaxX(), wertebereich.getMaxX(), 0.0);
    assertEquals(wertebereichPunktemenge.getMaxY(), wertebereich.getMaxY(), 0.0);
    assertEquals(wertebereichPunktemenge.getMinX(), wertebereich.getMinX(), 0.0);
    assertEquals(wertebereichPunktemenge.getMinY(), wertebereich.getMinY(), 0.0);
}

From source file:com.mac.halendpoint.endpoints.ProtocolRouter.java

public Protocol router(Protocol protocol)
        throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException {

    log.info("incoming protocol: " + protocol);
    if (Objects.nonNull(protocol)) {
        Method[] methods = this.getClass().getDeclaredMethods();
        log.info("protocol NOT null");
        for (Method method : methods) {
            if (!method.getName().equals("router")) {
                log.info("current method: " + method.getName());
                method.setAccessible(true);
                ProtocolMapping protoMapping = method.getAnnotation(ProtocolMapping.class);
                log.info("ProtocolMapping: " + protoMapping);
                if (Objects.nonNull(protoMapping)) {
                    log.info("ProtocolMapping not null");
                    if (protoMapping.routeTo().equalsIgnoreCase(protocol.getRoutedTo())) {
                        log.info("routing to: " + protoMapping.routeTo());
                        int numParams = protoMapping.numParams();
                        String[] parameters = protocol.getParameters();
                        if (Objects.nonNull(parameters) && parameters.length == numParams) {
                            log.info("parameter lengths match");
                            Object response = method.invoke(this, (Object[]) parameters);
                            log.info("response after invocation");
                            protocol.setRespondTo(protoMapping.respondTo());
                            if (!(response instanceof String)) {
                                protocol.setResponse(JsonConverter.toJsonString(response));
                            } else {
                                protocol.setResponse((String) response);
                            }//w  w w .  j  a  v  a 2  s  .c om
                            protocol.setIsModified(true);
                            log.info(response.toString());
                            log.info("final protocol: " + protocol);
                            log.info("final protocol: " + protocol.getRespondTo());
                            log.info("final protocol: " + protocol.getResponse());
                            log.info("final protocol: " + protocol.getRoutedTo());
                            log.info("final protocol: " + Arrays.toString(protocol.getParameters()));
                        }
                    }
                }
                method.setAccessible(false);
            }
        }
        return protocol;
    }
    return new FailedProtocol();
}

From source file:Employee.java

 @PostConstruct
public void jndiInject(InvocationContext invocation) {
   Object target = invocation.getTarget();
   Field[] fields = target.getClass().getDeclaredFields();
   Method[] methods = target.getClass().getDeclaredMethods();

   // find all @JndiInjected fields methods and set them
   try {/*ww  w  .j ava  2 s . co m*/
      InitialContext ctx = new InitialContext();
      for (Method method : methods) {
         JndiInjected inject = method.getAnnotation(JndiInjected.class);
         if (inject != null) {
            Object obj = ctx.lookup(inject.value());
            method.setAccessible(true);
            method.invoke(target, obj);
         }
      }
      for (Field field : fields) {
         JndiInjected inject = field.getAnnotation(JndiInjected.class);
         if (inject != null) {
            Object obj = ctx.lookup(inject.value());
            field.setAccessible(true);
            field.set(target, obj);
         }
      }
      invocation.proceed();
   } catch (Exception ex) {
      throw new EJBException("Failed to execute @JndiInjected", ex);
   }
}

From source file:com.mastercard.test.spring.security.SpringSecurityJUnit4ClassRunnerTests.java

@Test
public void describeChildMatchesBlockJUnit4ClassRunnerForNonWithTestUserTest() throws Exception {
    SpringSecurityJUnit4ClassRunner runner = new SpringSecurityJUnit4ClassRunner(MockWithMockUserTest.class);

    FrameworkMethod method = new FrameworkMethod(
            MockWithMockUserTest.class.getMethod("testWithoutWithMockUser"));
    Description actualDescription = runner.describeChild(method);

    BlockJUnit4ClassRunner expectedRunner = new BlockJUnit4ClassRunner(MockWithMockUserTest.class);
    Method method1 = BlockJUnit4ClassRunner.class.getDeclaredMethod("describeChild", FrameworkMethod.class);
    method1.setAccessible(true);
    Description expectedDescription = (Description) method1.invoke(expectedRunner, method);

    assertDescriptionDetailsEqual(expectedDescription, actualDescription);
    assertEquals(expectedDescription.getChildren().size(), actualDescription.getChildren().size());
}

From source file:com.mastercard.test.spring.security.SpringSecurityJUnit4ClassRunnerTests.java

@Test
public void describeChildCreatesDescriptionForWithTestUserTest() throws Exception {
    SpringSecurityJUnit4ClassRunner runner = new SpringSecurityJUnit4ClassRunner(MockWithMockUserTest.class);

    Method method2Test = MockWithMockUserTest.class.getMethod("testWithWithMockUser");
    FrameworkMethod method = new AnnotationFrameworkMethod(new FrameworkMethod(method2Test),
            method2Test.getDeclaredAnnotation(WithMockUser.class));
    Description actualDescription = runner.describeChild(method);

    BlockJUnit4ClassRunner expectedRunner = new BlockJUnit4ClassRunner(MockWithMockUserTest.class);
    Method method1 = BlockJUnit4ClassRunner.class.getDeclaredMethod("describeChild", FrameworkMethod.class);
    method1.setAccessible(true);
    Description expectedDescription = (Description) method1.invoke(expectedRunner, method);

    assertDescriptionDetailsEqual(expectedDescription, actualDescription);
    assertEquals(expectedDescription.getChildren().size(), actualDescription.getChildren().size());
}

From source file:com.salesforce.ide.apex.internal.core.CompilerService.java

/**
 * This is temporary workaround to bypass the validation stage of the compiler while *still* doing the
 * additional_validate stage. We are bypassing the validation stage because it does a deep validation that we don't
 * have all the parts for yet in the offline compiler. Rather than stop all work on that, we bypass it so that we
 * can still do useful things like find all your types, find all your methods, etc.
 * /*from   w ww  . j  a  va 2s .  c  om*/
 */
@SuppressWarnings("unchecked")
private void callAdditionalPassVisitor(ApexCompiler compiler) {
    try {
        List<CodeUnit> allUnits = (List<CodeUnit>) FieldUtils.readDeclaredField(compiler, "allUnits", true);
        CompilerContext compilerContext = (CompilerContext) FieldUtils.readDeclaredField(compiler,
                "compilerContext", true);

        for (CodeUnit unit : allUnits) {
            Method getOperation = CompilerStage.ADDITIONAL_VALIDATE.getDeclaringClass()
                    .getDeclaredMethod("getOperation");
            getOperation.setAccessible(true);
            CompilerOperation operation = (CompilerOperation) getOperation
                    .invoke(CompilerStage.ADDITIONAL_VALIDATE);
            operation.invoke(compilerContext, unit);
        }
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        logger.error("Failed to inokve additional validator", e);
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.auth.policy.bean.PropertyRestrictionPolicyHelperTest.java

/** Invoke the private static method "populateThresholdMap" */
private Map<String, RoleLevel> populateThresholdMap(String propertyUri) {
    Map<String, RoleLevel> map = new HashMap<String, BaseResourceBean.RoleLevel>();
    try {/*from w w  w .  jav  a 2  s.com*/
        Class<?> clazz = PropertyRestrictionPolicyHelper.class;
        Method method = clazz.getDeclaredMethod("populateThresholdMap", OntModel.class, Map.class,
                String.class);
        method.setAccessible(true);
        method.invoke(null, ontModel, map, propertyUri);
        return map;
    } catch (Exception e) {
        fail("failed to populate the map: propertyUri='" + propertyUri + "', " + e);
        return null;
    }
}