jpype-project / jpype

JPype is cross language bridge to allow Python programs full access to Java class libraries.
http://www.jpype.org
Apache License 2.0
1.12k stars 181 forks source link

Does Jpype support with Java SpringBoot annotation classes defined in jar? #940

Closed sureshsargar closed 3 years ago

sureshsargar commented 3 years ago

Currently, I am trying to use one of the helper java code which is written using SpringBoot framework with an annotation like @Component, @Autowired .

while calling those object method I am getting NullPointerException, Does anyone come across this type of issue?

Thrameos commented 3 years ago

I sm not aware of the mechanism used here. So you would need to show a short example that replicates the issue.

sureshsargar commented 3 years ago

In SpringBoot with annotation, it helps us to get objects ready to use instead of creating those objects with an explicit declaration of new keywords.

Let me show simple class with SpringBoot annotation in it. if you look at loader variable it should automatically initialize and ready to access in init method with @PostConstruct annotation.but I am getting NullPointerException for line loader.loadMetadata() . which usually works when I am running pure java application.

@Lazy @Component public class FhirTransformer {

@Autowired
private RuleEngine ruleEngine;

private static Logger logger = LoggerFactory.getLogger(FhirTransformer.class);

@Autowired
private MetadataLoader loader;
@Autowired
private UAPEnvironment uapenv;

@PostConstruct
public void init() throws MetadataLoadException {
    logger.info("Loading Metadata...");
    metadata = loader.loadMetadata();
    tableLinksByResourceType = loader.loadTableLinks();
    commonColumnsMetadata = loader.loadCommonColumnsMetadata();
    activeResourceList = loader.loadActiveResources();
    loader.closeMetadataStore();
    logger.info("Completed loading Metadata.");
}

whereas if I convert this code to non-springBoot syntax(removing annotation and creating object explicitly) then it works while using in python code.

so want to confirm this behavior w.r.t SpringBoot support with jpype.

NicoKiaru commented 3 years ago

I wonder the same thing. How well are java annotation supported in JPype ?

Thrameos commented 3 years ago

JPype uses Proxy to implement Java classes within Python. As far as I am aware there are no special provisions that allow adding annotation to a class using Proxy.

https://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Proxy.html

It is possible to read an annotation using java.lang.reflect from within Python, but adding an annotation is difficult. There are a few magic hacks to dynamically alter a class which allows annotations to be added at runtime, but those tricks are very version dependent and thus not portable.

The only way to support full annotations would be to use the ASM library to construct a Java representation at runtime based on the specifications provided by Java. There is a prototype of such a scheme in the epypj branch, but I don't have enough developer/user support to complete the test bench which would required to release it. Also there are many of tricks that one can do with annotations in Java don't actually use runtime annotations but rather an annotation processor and compile time annotations which are removed. So unless someone has some specific Python code where the annotations are can actually be passed through to Java that I can use as an example I am not sure how to answer this request.

For example, the most of the spring type annotations are compile time. Therefore, it doesn't matter if JPype can add the annotations or not, because the spring processor will not get run, thus there is no way that they will appear properly in Java.

https://dzone.com/articles/spring-annotation-processing-how-it-works

These are language based limitations imposed by Java, thus they are likely the same in any library that uses JNI to control Java.

NicoKiaru commented 3 years ago

Thanks for the detailed answer. I think ideally your comment (as is) should appear somewhere in the JPype documentation

Thrameos commented 3 years ago

Sure. I will make sure it gets added to the next release.