Skip to content

Commit a4ee558

Browse files
richardstartinthegreystone
authored andcommitted
7945: Avoid throwing NoSuchFieldException in ValueReaders$ReflectiveReader
Reviewed-by: hirt, jbachorik
1 parent bd35870 commit a4ee558

File tree

1 file changed

+27
-9
lines changed
  • core/org.openjdk.jmc.flightrecorder/src/main/java/org/openjdk/jmc/flightrecorder/internal/parser/v1

1 file changed

+27
-9
lines changed

core/org.openjdk.jmc.flightrecorder/src/main/java/org/openjdk/jmc/flightrecorder/internal/parser/v1/ValueReaders.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
33
*
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
@@ -35,7 +35,10 @@
3535
import java.io.IOException;
3636
import java.lang.reflect.Field;
3737
import java.util.ArrayList;
38+
import java.util.Collections;
39+
import java.util.HashMap;
3840
import java.util.List;
41+
import java.util.Map;
3942
import java.util.logging.Level;
4043
import java.util.logging.Logger;
4144

@@ -506,6 +509,7 @@ public void addField(String identifier, String name, String description, IValueR
506509
static class ReflectiveReader extends AbstractStructReader {
507510
// FIXME: Change the reflective setting of fields to avoid the conversion workarounds that some classes have to make. See JMC-5966
508511

512+
private final Map<String, Field> identifiersToFields;
509513
// String to prefix reserved java keywords with when looking for a class field
510514
private static final String RESERVED_IDENTIFIER_PREFIX = "_"; //$NON-NLS-1$
511515
private final List<Field> fields;
@@ -515,6 +519,7 @@ static class ReflectiveReader extends AbstractStructReader {
515519
<T> ReflectiveReader(Class<T> klass, int fieldCount, ContentType<? super T> ct) {
516520
super(fieldCount);
517521
this.klass = klass;
522+
this.identifiersToFields = IdentifierFieldCache.INSTANCE.get(klass);
518523
this.ct = ct;
519524
fields = new ArrayList<>(fieldCount);
520525
}
@@ -562,18 +567,31 @@ public ContentType<?> getContentType() {
562567
void addField(String identifier, String name, String description, IValueReader reader)
563568
throws InvalidJfrFileException {
564569
valueReaders.add(reader);
565-
try {
566-
try {
567-
fields.add(klass.getField(identifier));
568-
} catch (NoSuchFieldException e) {
569-
fields.add(klass.getField(RESERVED_IDENTIFIER_PREFIX + identifier));
570-
}
571-
} catch (NoSuchFieldException e) {
570+
Field field = identifiersToFields.get(identifier);
571+
if (field == null) {
572+
field = identifiersToFields.get(RESERVED_IDENTIFIER_PREFIX + identifier);
573+
}
574+
if (field == null) {
572575
Logger.getLogger(ReflectiveReader.class.getName()).log(Level.WARNING,
573576
"Could not find field with name '" + identifier + "' in reader for '" + ct.getIdentifier() //$NON-NLS-1$ //$NON-NLS-2$
574577
+ "'"); //$NON-NLS-1$
575-
fields.add(null);
576578
}
579+
fields.add(field);
580+
}
581+
}
582+
583+
static class IdentifierFieldCache extends ClassValue<Map<String, Field>> {
584+
585+
public static final IdentifierFieldCache INSTANCE = new IdentifierFieldCache();
586+
587+
@Override
588+
protected Map<String, Field> computeValue(Class<?> type) {
589+
Field[] fields = type.getFields();
590+
Map<String, Field> map = new HashMap<>(fields.length);
591+
for (Field field : fields) {
592+
map.put(field.getName(), field);
593+
}
594+
return Collections.unmodifiableMap(map);
577595
}
578596
}
579597
}

0 commit comments

Comments
 (0)