java/ch/wlkl/env/Meta.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ch.wlkl.env;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import static ch.wlkl.env.Env.dy;
/**
*
* @author walter
*/
public class Meta {
int x = 3;
/**
* return String[] for final public static int fields: field value -> field name
* @param cl the class
* @param fr name of first field or null for from first
* @param to name of last field or null for to last
* @return
*/
public static String[] i2Field(Class cl, String fr, String to) {
Field[] f = cl.getFields();
int op = -1, fx = 0, tx = f.length - 1, mx = -1;
String nm = null;
String[] res = new String[3];
for (int i = 0; i < f.length; i++) {
if (f[i].getName().equals(fr)) {
fx = i;
}
if (f[i].getName().equals(to)) {
tx = i;
}
}
if (fr != null && !f[fx].getName().equals(fr)) {
dy(fr + " not found in " + cl.getName());
}
if (to != null && !f[tx].getName().equals(to)) {
dy(to + " not found in " + cl.getName());
}
for (int i = fx; i <= tx; i++) {
if (f[i].getType() == Integer.TYPE & (f[i].getModifiers() & Modifier.STATIC) != 0 & (f[i].getModifiers() & Modifier.PUBLIC) != 0) {
try {
op = f[i].getInt(null);
nm = f[i].getName();
} catch (Exception ex) {
dy("exception " + ex);
}
if (op < 0) {
dy("field " + nm + " value " + op);
} else if (op >= res.length) {
res = Arrays.copyOf(res, op * 2);
} else if (res[op] != null) {
dy("field " + nm + " value " + op + " duplicate value");
}
res[op] = nm;
}
}
return res;
}
}