Example usage for java.lang.reflect Field setInt

List of usage examples for java.lang.reflect Field setInt

Introduction

In this page you can find the example usage for java.lang.reflect Field setInt.

Prototype

@CallerSensitive
@ForceInline 
public void setInt(Object obj, int i) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Sets the value of a field as an int on the specified object.

Usage

From source file:Main.java

/**
 * Called internally by installGtkPopupBugWorkaround to fix the thickness
 * of a GTK style field by setting it to a minimum value of 1.
 * //from  w ww  .j  ava  2s .c om
 * @param style
 *            The GTK style object.
 * @param fieldName
 *            The field name.
 * @throws Exception
 *             When reflection fails.
 */
private static void fixGtkThickness(Object style, String fieldName) throws Exception {
    Field field = style.getClass().getDeclaredField(fieldName);
    boolean accessible = field.isAccessible();
    field.setAccessible(true);
    field.setInt(style, Math.max(1, field.getInt(style)));
    field.setAccessible(accessible);
}

From source file:org.apache.kylin.rest.DebugTomcat.java

public static void setupDebugEnv() {
    try {/*from w w  w.  ja  v a  2s.c  om*/
        System.setProperty("log4j.configuration", "file:../build/conf/kylin-tools-log4j.properties");

        // test_case_data/sandbox/ contains HDP 2.2 site xmls which is dev sandbox
        KylinConfig.setSandboxEnvIfPossible();
        overrideDevJobJarLocations();

        System.setProperty("spring.profiles.active", "testing");

        //avoid log permission issue
        if (System.getProperty("catalina.home") == null)
            System.setProperty("catalina.home", ".");

        if (StringUtils.isEmpty(System.getProperty("hdp.version"))) {
            System.err.println(
                    "No hdp.version set; Please set hdp.version in your jvm option, for example: -Dhdp.version=2.4.0.0-169");
            System.exit(1);
        }

        // workaround for job submission from win to linux -- https://issues.apache.org/jira/browse/MAPREDUCE-4052
        if (Shell.WINDOWS) {
            {
                Field field = Shell.class.getDeclaredField("WINDOWS");
                field.setAccessible(true);
                Field modifiersField = Field.class.getDeclaredField("modifiers");
                modifiersField.setAccessible(true);
                modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
                field.set(null, false);
            }
            {
                Field field = java.io.File.class.getDeclaredField("pathSeparator");
                field.setAccessible(true);
                Field modifiersField = Field.class.getDeclaredField("modifiers");
                modifiersField.setAccessible(true);
                modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
                field.set(null, ":");
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:org.jodconverter.filter.DefaultFilterChainITest.java

static void setFinalStatic(final Field field, final Object newValue) throws Exception {

    field.setAccessible(true);//  www.  jav a 2s  .  c  o  m
    final Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    field.set(null, newValue);
}

From source file:org.apache.atlas.repository.graphdb.titan0.Titan0Database.java

/**
 * Titan loads index backend name to implementation using
 * StandardIndexProvider.ALL_MANAGER_CLASSES But
 * StandardIndexProvider.ALL_MANAGER_CLASSES is a private static final
 * ImmutableMap Only way to inject Solr5Index is to modify this field. So,
 * using hacky reflection to add Sol5Index
 *//*from   w ww.  j a  va  2 s .  c om*/
private static void addSolr5Index() {
    try {
        Field field = StandardIndexProvider.class.getDeclaredField("ALL_MANAGER_CLASSES");
        field.setAccessible(true);

        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

        Map<String, String> customMap = new HashMap<>(StandardIndexProvider.getAllProviderClasses());
        customMap.put("solr", Solr5Index.class.getName()); // for
                                                           // consistency
                                                           // with Titan
                                                           // 1.0.0
        customMap.put("solr5", Solr5Index.class.getName()); // for backward
                                                            // compatibility
        ImmutableMap<String, String> immap = ImmutableMap.copyOf(customMap);
        field.set(null, immap);

        LOG.debug("Injected solr5 index - {}", Solr5Index.class.getName());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:ReflectionHelper.java

/**
 * Set the value of the field of the object to the given value.
 * //from  www. j a  v a  2 s  .com
 * @param original
 *            object to modify
 * @param field
 *            field to modify
 * @param value
 *            new value for the given object and field
 * @throws NoSuchFieldException
 * @throws IllegalAccessException
 */
public static void setStaticFinalField(Object original, Field field, Object value)
        throws NoSuchFieldException, IllegalAccessException {
    // we mark the field to be public
    field.setAccessible(true);
    // next we change the modifier in the Field instance to
    // not be final anymore, thus tricking reflection into
    // letting us modify the static final field
    Field modifiersField = Field.class.getDeclaredField(MODIFIERS_FIELD);
    modifiersField.setAccessible(true);
    int modifiers = modifiersField.getInt(field);
    // blank out the final bit in the modifiers int
    modifiers &= ~Modifier.FINAL;
    modifiersField.setInt(field, modifiers);
    FieldAccessor fa = reflection.newFieldAccessor(field, false);
    fa.set(original, value);
}

From source file:com.coinblesk.client.utils.ClientUtils.java

private static void setFinalStatic(Field field, Object newValue) throws Exception {
    field.setAccessible(true);/*from w  ww.ja  va2s . c  om*/

    // remove final modifier from field
    try {
        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    } catch (Exception e) {
        Log.w(TAG, "Could not remove 'final' modifier: " + e.getMessage());
    }
    field.set(null, newValue);
}

From source file:org.granitemc.granite.reflect.ReflectionUtils.java

/**
 * Will force access to a field. This even works with private static final fields!
 * <p/>/*w w  w. j a v  a 2  s . c om*/
 * Internally, this uses some reflection-on-reflection trickery I found on StackOverflow :)
 *
 * @param f The field to force access to
 */
public static void forceStaticAccessible(Field f) {
    try {
        f.setAccessible(true);

        // Some reflection-ception trickery
        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(f, f.getModifiers() & ~Modifier.FINAL);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace();
    }
}

From source file:org.venice.piazza.servicecontroller.messaging.ServiceMessageThreadManagerTest.java

static void setFinalStatic(Field field, Object newValue) throws Exception {
    field.setAccessible(true);/*from  w ww .j av a  2 s. c om*/
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

    field.set(null, newValue);

}

From source file:org.apache.lens.ml.AlgoArgParser.java

/**
 * Extracts feature names. If the algo has any parameters associated with @AlgoParam annotation, those are set
 * as well./* w w w .j  a  v  a 2 s. c  o  m*/
 *
 * @param algo the algo
 * @param args    the args
 * @return List of feature column names.
 */
public static List<String> parseArgs(MLAlgo algo, String[] args) {
    List<String> featureColumns = new ArrayList<String>();
    Class<? extends MLAlgo> algoClass = algo.getClass();
    // Get param fields
    Map<String, Field> fieldMap = new HashMap<String, Field>();

    for (Field fld : algoClass.getDeclaredFields()) {
        fld.setAccessible(true);
        AlgoParam paramAnnotation = fld.getAnnotation(AlgoParam.class);
        if (paramAnnotation != null) {
            fieldMap.put(paramAnnotation.name(), fld);
        }
    }

    for (int i = 0; i < args.length; i += 2) {
        String key = args[i].trim();
        String value = args[i + 1].trim();

        try {
            if ("feature".equalsIgnoreCase(key)) {
                featureColumns.add(value);
            } else if (fieldMap.containsKey(key)) {
                Field f = fieldMap.get(key);
                if (String.class.equals(f.getType())) {
                    f.set(algo, value);
                } else if (Integer.TYPE.equals(f.getType())) {
                    f.setInt(algo, Integer.parseInt(value));
                } else if (Double.TYPE.equals(f.getType())) {
                    f.setDouble(algo, Double.parseDouble(value));
                } else if (Long.TYPE.equals(f.getType())) {
                    f.setLong(algo, Long.parseLong(value));
                } else {
                    // check if the algo provides a deserializer for this param
                    String customParserClass = algo.getConf().getProperties().get("lens.ml.args." + key);
                    if (customParserClass != null) {
                        Class<? extends CustomArgParser<?>> clz = (Class<? extends CustomArgParser<?>>) Class
                                .forName(customParserClass);
                        CustomArgParser<?> parser = clz.newInstance();
                        f.set(algo, parser.parse(value));
                    } else {
                        LOG.warn("Ignored param " + key + "=" + value + " as no parser found");
                    }
                }
            }
        } catch (Exception exc) {
            LOG.error("Error while setting param " + key + " to " + value + " for algo " + algo);
        }
    }
    return featureColumns;
}

From source file:org.apache.lens.ml.TrainerArgParser.java

/**
 * Extracts feature names. If the trainer has any parameters associated with @TrainerParam annotation, those are set
 * as well.//  www.  j a v  a2s  .co m
 *
 * @param trainer
 *          the trainer
 * @param args
 *          the args
 * @return List of feature column names.
 */
public static List<String> parseArgs(MLTrainer trainer, String[] args) {
    List<String> featureColumns = new ArrayList<String>();
    Class<? extends MLTrainer> trainerClass = trainer.getClass();
    // Get param fields
    Map<String, Field> fieldMap = new HashMap<String, Field>();

    for (Field fld : trainerClass.getDeclaredFields()) {
        fld.setAccessible(true);
        TrainerParam paramAnnotation = fld.getAnnotation(TrainerParam.class);
        if (paramAnnotation != null) {
            fieldMap.put(paramAnnotation.name(), fld);
        }
    }

    for (int i = 0; i < args.length; i += 2) {
        String key = args[i].trim();
        String value = args[i + 1].trim();

        try {
            if ("feature".equalsIgnoreCase(key)) {
                featureColumns.add(value);
            } else if (fieldMap.containsKey(key)) {
                Field f = fieldMap.get(key);
                if (String.class.equals(f.getType())) {
                    f.set(trainer, value);
                } else if (Integer.TYPE.equals(f.getType())) {
                    f.setInt(trainer, Integer.parseInt(value));
                } else if (Double.TYPE.equals(f.getType())) {
                    f.setDouble(trainer, Double.parseDouble(value));
                } else if (Long.TYPE.equals(f.getType())) {
                    f.setLong(trainer, Long.parseLong(value));
                } else {
                    // check if the trainer provides a deserializer for this param
                    String customParserClass = trainer.getConf().getProperties().get("lens.ml.args." + key);
                    if (customParserClass != null) {
                        Class<? extends CustomArgParser<?>> clz = (Class<? extends CustomArgParser<?>>) Class
                                .forName(customParserClass);
                        CustomArgParser<?> parser = clz.newInstance();
                        f.set(trainer, parser.parse(value));
                    } else {
                        LOG.warn("Ignored param " + key + "=" + value + " as no parser found");
                    }
                }
            }
        } catch (Exception exc) {
            LOG.error("Error while setting param " + key + " to " + value + " for trainer " + trainer);
        }
    }
    return featureColumns;
}