influxdb-java icon indicating copy to clipboard operation
influxdb-java copied to clipboard

Support using package-private classes as POJOs for InfluxDBResultMapper

Open leveluptor opened this issue 6 years ago • 2 comments

I'm using org.influxdb:influxdb-java:2.10. I'd like to limit my @Measurement classes visibility to package-private, but currently it doesn't work because of the following exception:

org.influxdb.InfluxDBMapperException: java.lang.IllegalAccessException: Class org.influxdb.impl.InfluxDBResultMapper can not access a member of class com.example.MyMeasurement with modifiers "public"

The line which causes this is https://github.com/influxdata/influxdb-java/blob/influxdb-java-2.10/src/main/java/org/influxdb/impl/InfluxDBResultMapper.java#L160. Looks like it will be enough to replace

object = clazz.newInstance();

with

Constructor<T> defaultConstructor = clazz.getDeclaredConstructor();
defaultConstructor.setAccessible(true);
object = declaredConstructor.newInstance();

If there are no objections, I'll provide a PR.

leveluptor avatar Jun 08 '18 16:06 leveluptor

I assume there are not objections, please go ahead.

asashour avatar Jan 22 '19 10:01 asashour

@leveluptor do you think you could provide a PR with your implementation? Just a small suggestion: do not change the accessibility of the default constructor inside the loop because this will invoke the SecurityManager[1] every time. You are using reflection to do modify the whole class (and not a single instance) so you can do it just once.

[1] http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/reflect/AccessibleObject.java#l92

fmachado avatar Mar 05 '19 14:03 fmachado