vendredi 18 août 2017

How to access the object-members of an object declaration in kotlin

Say you have the following, nested object declaration:

object Father {   
    val fathersField = "value"
    object Child {
        val childsField = 3.141592654
    }
}

When I use reflection starting from Father, I'm able to find the field fathersField only but no member referencing the Child instance.

Is it possible to find those inner object declarations via reflection? And if so, how?





How to Use dynamic method Delegate to invoke a method

 public class TestClass
    {
        public TestClass()
        {

        }

        void firstmethod(int i,int j,string k)
        {
            Console.WriteLine("first method");
        }
        void firstmethod(int i, int j)
        {
            Console.WriteLine("first method without k");
        }
    }

Using dynamic method via delegate(which is faster found by reading online) How can i call a method in the TestClass?





handle this datatype whith reflection

I don't know how to handle this datatype.

object wert = x.GetValue(instanz);

List<object> element = new List<object>();

if (wert.GetType().GetInterfaces().Any(t => t == typeof(IEnumerable)))
{                           
    foreach (var item in wert as IEnumerable)
    {    
     //this works for Lists, but not for the Datatype i have here(see link in my post)  
     //I get "null" for "y.GetValue(item)"       
     element.Add(item.GetType().GetProperties().ToDictionary(y => y.Name, y => y.GetValue(item)));
    }
}

Immediate window:

wert as IEnumerable

Will return:

Count = 1
    [0]: "51000"

Here you can find deeper informations: click

For the y in the foreach will fail when it hits y.GetValue(item): click

How can I solve this problem ?





How to use Method Reference operator to get the reference of method

I want to reference an annotation value in one class from another class. Like here -

class A {
    private static final String STATIC_STRING = B.class.getMethod("myMethod").getAnnotation(SomeAnnotation.class).name();
}

The problem here is that I cannot use this syntax because it requires getMethod to be wrapped in a try-catch which is not possible. Although I can use a static block to create this variable and just gulp the exception in catch block, I don't want to do that because if in future the method name is changed, compiler won't throw an error in the static block.

I was wondering if I could do something like

private static final String STATIC_STRING = (B::myMethod).getAnnotation(SomeAnnotation.class).name();

Note: neither B nor SomeAnnotation is my source, they come from another library.

I have 2 questions here

  • Is there a better way to get value from annotation, preferably without using any external library like Google Reflections or similar?
  • How can I use :: to get instance of java.lang.reflect.Method? Is it possible altogether? If yes, is vice-versa also possible, i.e. create supplier or mapper from Method's object?




jeudi 17 août 2017

Understanding Java annotations

I've looked around a bit, and I'm failing to understand parts of how Java annotation work.

All the examples I've seen so far create the annotation, then have a main method that runs through the classes in the project using reflection. Then they will do stuff with that and basically make the annotation work.

However I'm failing to understand how this works if I want to make my annotation project into a jar that I can include in another project, like jackson, guice, hibernate, etc. A main method wouldn't work in this case, right?

I've looked for some tutorials on how to make annotation that is done as an jar that my project can include, but I haven't found anything yet. Ideally I'd like to be able to use it inside a web framework such as spring or play.





Abstract Class Constructor in java reflection [on hold]

I am trying to create an Factory in java

I have an Abstract Basic Class

public abstract class AbstractClass {





"Object does not match target type" when calling SetValue from data obtained by GetValue

Variables source and target are two different instances of two different classes. But they both have an int property called ID.

Variables tProp and sProp are both PropertyInfo objects for the ID properties on their respective objects.

sProp has value 54, and the IDE Locals windwows shows it as type obj{int}

tProp is a valid property, not set yet.

I am trying to set target.ID using this code:

var sVal = sProp.GetValue(source);
tProp.SetValue(target, sVal);

When I hit that line I get "Object does not match target type".

I know that I can fix this by casting sVal to int, but I'm trying to write generic code here, and thought that at least .NET would be able to determine that the two objects are exactly the same type. Am I missing something?

Any idea how to solve this, other than checking types and casting?