vendredi 12 janvier 2024

How to resolve JAR conflicts with maven-shade-plugin

I have two projects: jartest and jarprj. jartest depends on poi 3.17, whereas jarprj depends on poi 5.10. jartest encounters an error when dynamically loading jarprj. The error message is as follows:

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.example.App.main(App.java:27)
Caused by: java.lang.NoSuchMethodError: org.apache.poi.xwpf.usermodel.XWPFDocument.getDocComments()Lorg/apache/poi/xwpf/usermodel/XWPFComments;
    at org.example.JarClass.test(JarClass.java:10)
    ... 5 more

The issue may stem from a conflict between the versions of poi. I attempted to rename poi using the maven-shade-plugin, but the error persists. The error message is as follows:

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.example.App.main(App.java:27)
Caused by: java.lang.NoSuchFieldError: Factory
    at shade.org.apache.poi.xwpf.usermodel.XWPFDocument.onDocumentCreate(XWPFDocument.java:304)
    at shade.org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:149)
    at org.example.JarClass.test(JarClass.java:9)
    ... 5 more

jartest cannot upgrade its poi version, while jarprj requires poi 5.1.0. What methods are there to solve this? How can we resolve it using maven-shade-plugin?





mercredi 10 janvier 2024

lambdametafactory creation is quite slow compared to just using lambda

@Benchmark
public void testLambdaGeneration(Blackhole bh) {
    Function<Object, Object> function = a -> {
        if(a instanceof Alpha alpha) {
            return alpha.primitiveInt();
        }else {
            throw new RuntimeException();
        }
    };
    bh.consume(function);
}

@Benchmark
public void testManualGeneration(Blackhole bh) {
    try{
        MethodHandle mh = MethodHandles.lookup().findVirtual(Alpha.class, "primitiveInt", MethodType.methodType(int.class));
        MethodType type = mh.type();
        type = type.changeReturnType(Integer.class);
        CallSite callSite = LambdaMetafactory.metafactory(MethodHandles.lookup(),
                "apply",
                MethodType.methodType(Function.class),
                type.erase(), mh, type);
        Function<Object, Object> f = (Function<Object, Object>) callSite.getTarget().invokeExact();
        bh.consume(f);
    }catch (Throwable e) {
        throw new RuntimeException("Unreached");
    }
}

I want to use lambdametafactory to create constructor, getter and setter methods for reflection usage, and generated Function<> is just as fast as the direct call, which greatly satisfies my needs. However, when I use the above code to test the generation performance between lambdafactory and pure lambda, the result is quite unpromising:

LambdaTest.testLambdaGeneration  avgt   50      0.574 ±     0.015  ns/op
LambdaTest.testManualGeneration  avgt   50  29424.815 ± 20402.801  ns/op

I think the lambda function would use LambdaMetafactory internally, so the result should be consistent, but the actual result really shock me.

Is there any thing wrong with my code? How could I improve the LambdaMetafactory.metafactory()'s performance to match the pure lambda generation?





why always i have who add the time package on diverse progamming languages? [closed]

It's always strange to me to have to import the time package into the code, something that in my opinion is so important and basic that it's not directly included without being imported from.

Python sample:

import time time.sleep(1)


I see this in almost all programming languages. I want to understand why this happens, if it does, I'm trying to increase my general understanding of programming.

Ps: Thanks to everyone who wants to help by answering me!

I hope to learn a little more!