Initialize multiple numeric fields at once in JAVA that begin with certain values

hammerva :

I am working on a Java class that contains a ton of numeric fields. Most of them would begin with something like 'CMTH' or 'FYTD'. Is it possible to initialize all fields of the same type that begin or end with a certain value. For example I have the following fields:

CMthRepCaseACR CMthRepUnitACR CMthRecCaseACR CMthRecUnitACR CMthHecCaseACR CMthHecUnitACR FYTDHecCaseACR FYTDHecUnitACR CMthBBKCaseACR CMthBBKUnitACR CMthPIHCaseACR .

I am trying to figure if it is possible to initialize all fields to zero that end with an 'ACR' or begin with an 'Cmth"

I know I can do something like cmtha = cmthb = cmthc = 0 but I was wondering there was a command where you can some kind of mask to initialize

Thanks

Marvin :

Assuming that you cannot change that said Java class (and e.g. use a collection or map to store the values) your best bet is probably reflection (see also: Trail: The Reflection API). Reflection gives you access to all fields of the class and you can then implement whatever matching you'd like.

Here's a short demo to get you started, minus error handling, sanity checks and adaptions to your actual class:

import java.util.stream.Stream;

public class Demo {

    private static class DemoClass {
        private int repCaseACR = 1;
        private int CMthRepUnit = 2;
        private int foo = 3;
        private int bar = 4;

        @Override
        public String toString() {
            return "DemoClass [repCaseACR=" + repCaseACR + ", CMthRepUnit=" + CMthRepUnit + ", foo=" + foo + ", bar="
                    + bar + "]";
        }
    }

    public static void main(String[] args) {
        DemoClass demoClass = new DemoClass();
        System.out.println("before: " + demoClass);
        resetFields(demoClass, "CMth", null);
        System.out.println("after prefix reset: " + demoClass);
        resetFields(demoClass, null, "ACR");
        System.out.println("after suffix reset: " + demoClass);
    }

    private static void resetFields(DemoClass instance, String prefix, String suffix) {
        Stream.of(instance.getClass().getDeclaredFields())
                .filter(field ->
                        (prefix != null && field.getName().startsWith(prefix))
                            || (suffix != null && field.getName().endsWith(suffix)))
                .forEach(field -> {
                    field.setAccessible(true);
                    try {
                        field.set(instance, 0);
                    } catch (IllegalArgumentException | IllegalAccessException e) {
                        // TODO handle me
                    }
                });
    }
}

Output:

before: DemoClass [repCaseACR=1, CMthRepUnit=2, foo=3, bar=4]
after prefix reset: DemoClass [repCaseACR=1, CMthRepUnit=0, foo=3, bar=4]
after suffix reset: DemoClass [repCaseACR=0, CMthRepUnit=0, foo=3, bar=4]

Note: Both links are seriously dated but the core functionality of reflection is still the same.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=393533&siteId=1