jeudi 2 avril 2020

Generic method with generic result

I have the following service method:

public static <T> T service(final Class<T> klass) {...}

And I use it this way

SomeService someService = service(SomeService.class);

However, using this approach I can't get generic service, for example what should I do if I need

SomeService<Foo> someService = ???

How to do it? How to make service method?





Scala reflection for nested path strings

Given a string of com.company.util.Type[com.company.controller.Response], or com.company.util.Type[scala.collection.Seq[com.company.controller.Response]] how do you use reflection to instantiate com.company.controller.Response?

I could manually use regex or the sort to find the most nested [], but my next task is to try and retain as much information of how it's wrapped as possible, for instance: I would like to know if it's been wrapped in a Seq, or Option or Some.

Using

implicit val mirror = runTimeMirror(classLoader)
mirror.staticClass("com.company.controller.Response").toType

Gives me what I want, but as soon as you introduce nested class path's like above, it understandably throws a not found exception.





Is it possbile to get all references of a Property in C#?

in VS 2019 I can do a right click on a Property and it shows you all references in code. Is there any way to show these references in a script? Can I call the VS tools?

Thank you very much!





mercredi 1 avril 2020

Safely caching java reflection objects for reuse

I'm creating a WebLogic Java 7 application that dynamically resolves beans using reflection then accesses the beans using the reflection-resolved getter and setter Method objects. I'm wanting to cache the Method objects for better performance and have at my disposal a caching mechanism that is implemented as a static variable with calls to the cache being synchronized. Currently I'm storing the resolved Method objects inside an object and caching this for retrieval and reuse in later http calls.

My question is - is storing Method and other reflection objects for reuse in this context "safe"? Meaning thread-safe, safe from memory leaks, safe from somehow becoming an outdated or duplicate version or not being able to be successfully deserialized?

Note that these are custom bean objects and no other program logic with be messing with them. And the cache is emptied when the JVM restarts.

Thanks in advance.





Create wrapper around interface and get method/paramters called

Lets say I have this interface

public interface ITest
{
    int Property1 { get; set; }
    void Method1();
    string GetMethod1();
    void MethodWithParam(string str);
}

How can I create a wrapper object around this?

And then capture the methods called or paramters and values accessed etc.

For example:

var myWrapper = GetWrapper<ITest>();
myWrapper.Property1 = 7;

How would I be able to using reflection or whatever to know the following:

Paramter name being called and value being set

var data = myWrapper.GetMethod1("Test");

Get method name of "GetMethod1" along with paramaters and then return a value based on that?

Hope makes sense





How to modify bean knowing proxy name

I'm adding my own scope to Spring. And I have to implement Serializable in every bean in this scope, beans not guarantee they will implement it, so, my question is, how to knowing a name of proxy(string) get a bean, make it implement Serializable through reflection API and put new class with proxy to context?

I read a few answers about implementation, but, as I understood, it does not suit my approach. Because they just take an interface and implement it, not a class.





How can I get the imports of a java class of other project in my method using URLClassLoader or Reflection API?

I need to check if the import files of another java file from another project or jar are interfaces or classes Suppose Demo is a project and it has a java file CreateDemo.java Now my current project is Sample, I need to know what import statements are there in CreateDemo.java and if those import statements are interfaces or classes. Can Someone help me here??