Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7945: Avoid throwing NoSuchFieldException in ValueReaders$ReflectiveReader #447

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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);
}
}
}