Skip to content

Commit

Permalink
7945: Avoid throwing NoSuchFieldException in ValueReaders$ReflectiveR…
Browse files Browse the repository at this point in the history
…eader

Reviewed-by: hirt, jbachorik
  • Loading branch information
richardstartin authored and thegreystone committed Oct 26, 2022
1 parent bd35870 commit a4ee558
Showing 1 changed file with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
Expand Down Expand Up @@ -35,7 +35,10 @@
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

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

private final Map<String, Field> identifiersToFields;
// String to prefix reserved java keywords with when looking for a class field
private static final String RESERVED_IDENTIFIER_PREFIX = "_"; //$NON-NLS-1$
private final List<Field> fields;
Expand All @@ -515,6 +519,7 @@ static class ReflectiveReader extends AbstractStructReader {
<T> ReflectiveReader(Class<T> klass, int fieldCount, ContentType<? super T> ct) {
super(fieldCount);
this.klass = klass;
this.identifiersToFields = IdentifierFieldCache.INSTANCE.get(klass);
this.ct = ct;
fields = new ArrayList<>(fieldCount);
}
Expand Down Expand Up @@ -562,18 +567,31 @@ public ContentType<?> getContentType() {
void addField(String identifier, String name, String description, IValueReader reader)
throws InvalidJfrFileException {
valueReaders.add(reader);
try {
try {
fields.add(klass.getField(identifier));
} catch (NoSuchFieldException e) {
fields.add(klass.getField(RESERVED_IDENTIFIER_PREFIX + identifier));
}
} catch (NoSuchFieldException e) {
Field field = identifiersToFields.get(identifier);
if (field == null) {
field = identifiersToFields.get(RESERVED_IDENTIFIER_PREFIX + identifier);
}
if (field == null) {
Logger.getLogger(ReflectiveReader.class.getName()).log(Level.WARNING,
"Could not find field with name '" + identifier + "' in reader for '" + ct.getIdentifier() //$NON-NLS-1$ //$NON-NLS-2$
+ "'"); //$NON-NLS-1$
fields.add(null);
}
fields.add(field);
}
}

static class IdentifierFieldCache extends ClassValue<Map<String, Field>> {

public static final IdentifierFieldCache INSTANCE = new IdentifierFieldCache();

@Override
protected Map<String, Field> computeValue(Class<?> type) {
Field[] fields = type.getFields();
Map<String, Field> map = new HashMap<>(fields.length);
for (Field field : fields) {
map.put(field.getName(), field);
}
return Collections.unmodifiableMap(map);
}
}
}

0 comments on commit a4ee558

Please sign in to comment.