166
167 public String getSimpleName() {
168 return name.substring(name.lastIndexOf('.') + 1);
169 }
170 }
171
172 /**
173 * Reads the value of all {@code static final} fields from a given class into an array of
174 * {@link ConstantValue}s.
175 */
176 public static List<ConstantValue> readConstants(Class<?> fromClass) {
177 try {
178 List<ConstantValue> res = new ArrayList<>();
179 for (Field field : fromClass.getDeclaredFields()) {
180 if (isStatic(field.getModifiers()) && isFinal(field.getModifiers())) {
181 ResolvedJavaField javaField = metaAccess.lookupJavaField(field);
182 Object boxed = field.get(null);
183 if (boxed instanceof JavaConstant) {
184 res.add(new ConstantValue(javaField.format("%H.%n"), (JavaConstant) boxed, boxed));
185 } else {
186 JavaConstant value = constantReflection.readConstantFieldValue(javaField, null);
187 if (value != null) {
188 res.add(new ConstantValue(javaField.format("%H.%n"), value, boxed));
189 if (boxed instanceof Object[]) {
190 Object[] arr = (Object[]) boxed;
191 for (int i = 0; i < arr.length; i++) {
192 JavaConstant element = constantReflection.readArrayElement(value, i);
193 if (element != null) {
194 res.add(new ConstantValue(javaField.format("%H.%n[" + i + "]"), element, arr[i]));
195 }
196 }
197 }
198 }
199 }
200 }
201 }
202 return res;
203 } catch (Exception e) {
204 throw new AssertionError(e);
205 }
206 }
|
166
167 public String getSimpleName() {
168 return name.substring(name.lastIndexOf('.') + 1);
169 }
170 }
171
172 /**
173 * Reads the value of all {@code static final} fields from a given class into an array of
174 * {@link ConstantValue}s.
175 */
176 public static List<ConstantValue> readConstants(Class<?> fromClass) {
177 try {
178 List<ConstantValue> res = new ArrayList<>();
179 for (Field field : fromClass.getDeclaredFields()) {
180 if (isStatic(field.getModifiers()) && isFinal(field.getModifiers())) {
181 ResolvedJavaField javaField = metaAccess.lookupJavaField(field);
182 Object boxed = field.get(null);
183 if (boxed instanceof JavaConstant) {
184 res.add(new ConstantValue(javaField.format("%H.%n"), (JavaConstant) boxed, boxed));
185 } else {
186 JavaConstant value = constantReflection.readFieldValue(javaField, null);
187 if (value != null) {
188 res.add(new ConstantValue(javaField.format("%H.%n"), value, boxed));
189 if (boxed instanceof Object[]) {
190 Object[] arr = (Object[]) boxed;
191 for (int i = 0; i < arr.length; i++) {
192 JavaConstant element = constantReflection.readArrayElement(value, i);
193 if (element != null) {
194 res.add(new ConstantValue(javaField.format("%H.%n[" + i + "]"), element, arr[i]));
195 }
196 }
197 }
198 }
199 }
200 }
201 }
202 return res;
203 } catch (Exception e) {
204 throw new AssertionError(e);
205 }
206 }
|