vendredi 2 octobre 2015

Reflection, ParameterInfo Short ParameterType [duplicate]

This question already has an answer here:

In Java, is is possible to get the "short" identifier for an object type, i.e. without the package affix. In C#, there is some really cool stuff you can do with reflection and methods, particularly with respect to the parameters.

I am trying to get the "short" parameter type from a parameter; however, so far, I have only been able to grab the fully qualified type. Is there a way to grab the short type without performing a substring-oriented operation on the type identifier?

For reference, here is how to get the parameter type:

ParameterInfo parameterInfo = MethodBase.GetCurrentMethod().GetParameters();
parameterInfo[1].ParameterType.ToString(); // do what you want with it, log it, print it, etc. 

I have to log the information to a file; however, I don't want (or need) to reveal package information.





C# MLApp Feval Variable Number of Arguments

I am using a COM component to run Matlab code from a c# app.

Within this I am using the Feval function which has the following form:

matlab.Feval("MatlabFunction", 1, out result, param1, param2...., paramN);

Having the parameters passed in like this isn't very ideal as I would like to have one function matlabExecutor which can run any function but because of the way Feval takes in parameters it looks as though I will need to have a wrapper function per Matlab function.

Note: Yes I can pass in an array as a parameter but that would mean changing ALL of my Matlab scripts which I don't particularly want to do.

I've given reflection a quick go but couldn't seem to work out how to use it with a COM object.





Get name of class that implements an interface

I have some entities, that may or may not inherit from other objects, but they will implement an interface, lets call it IMyInterface.

public interface IMyInterface {
    long MyPropertyName { get; set; }
}

An object will always implement this interface, but it may have been implemented on a class that the object inherits from. How can i get the name of the class that has this interface implemented?

Examples should give these results

public class MyClass : IMyInterface {

}

public class MyHighClass : MyClass {

}

public class MyPlainClass {

}

public class PlainInheritedClass : MyPlainClass, IMyInterface {

}

If i pass in MyClass, it should return MyClass, because MyClass implements the interface.

If i pass in MyHighClass, it should return MyClass, because MyClass was inherited, and it implements the interface.

If i pass in PlainInheritedClass, it should return PlainInheriedClass, because it inherited from MyPlainClass, but that did not implement the interface, PlainInheritedClass did





How to compare classes with primitive type classes? [duplicate]

This question already has an answer here:

Since Class does not implement Comparable I use the following method to test two classes:

public int compare(Class o1, Class o2) {
    return o1.isAssignableFrom(o2) && o2.isAssignableFrom(o1) ?
        0 :
        o1.getName().compareTo(o2.getName());
}

I'd expect compare(float.class, Float.class) == 0 or compare(int.class, Integer.class) == 0; how to do that without checking explicitly for certain types?





Comparison of enums values() method and class.getEnumConstants()

I'm trying to compare this two ways to achieving the enums values (with and without reflection).

So this is my test class:

public class ReflectionOnEnumsTests2 {

    enum TestEnum { ONE, TWO, THREE; }

    public static void main(String[] args) {
        long n = 600_000_000;
        int stub;

        //test without Reflection
        long timeStartWithoutReflection = System.currentTimeMillis();
        for (int i = 0; i < n; i++){
            TestEnum[] values = TestEnum.values();
            stub = values.length;
        }
        System.out.println("Time consuming with reflection: " + (System.currentTimeMillis() - timeStartWithoutReflection));

        //test Reflection
        long timeStartWithReflection = System.currentTimeMillis();
        for (int i = 0; i < n; i++){
            TestEnum[] values = TestEnum.class.getEnumConstants();
            stub = values.length;
        }
        System.out.println("Time consuming with reflection: " + (System.currentTimeMillis() - timeStartWithReflection));
    }
}

And I'm confused about the test results. There is approximately the same time consuming. I expected that class.getEnumConstants would be much slower than values() method.

Results:
Time consuming with reflection: 6050
Time consuming with reflection: 7483

JDK version: 1.8.0_60

Question:
So why there is no difference in performance?





Java efficient decorator

Almost 60 classes have a function, which sth like this:

List<Group> groups(int p) {
    List<Group> groups = new ArrayList<Group>();
    groups.add(new GroupA(p));
    groups.add(new GroupB(p));
    groups.add(new GroupC(p));

    return groups;
}

However, it's not readable, and not maintainable (it's more complicated that it is above). What I would like to do is to create generic efficient function something like that:

List<Group> groups(int p) {
    return groupFactory(p, GroupA, GroupB, GroupC);
}

I have heard about reflection mechanism, however it is very slow and working dynamically. What I should like to have is something which is generated statically, similarly as template in C++.

Thank You.





Deserialize KendoFilter from http query string

I am trying to deserialize Kondo filter object, which is passed to my API withing query string:

?groupBy=4&groupDir=asc&filter%5Blogic%5D=and&filter%5Bfilters%5D%5B0%5D%5Bfield%5D=List&filter%5Bfilters%5D%5B0%5D%5Boperator%5D=eq&filter%5Bfilters%5D%5B0%5D%5Bvalue%5D=34727&sort%5B0%5D%5Bfield%5D=priority&sort%5B0%5D%5Bdir%5D=asc&sortContext=34727&tzo=3600&tz=Europe/London&_=1443780599583

filter[filters][0][field] = List

filter[filters][0][operator] = eq

filter[filters][0][value] = 34727

filter[logic] = and

groupBy = 4

groupDir = asc

[DataContract]
public class KendoFilter
{
    [DataMember(Name = "field")]
    public string Field { get; set; }

    [DataMember(Name = "operator")]
    public string Operator { get; set; }

    [DataMember(Name = "value")]
    public object Value { get; set; }

    [DataMember(Name = "logic")]
    public string Logic { get; set; }

    [DataMember(Name = "filters")]
    public IEnumerable<KendoFilter> Filters { get; set; }
}

It works perfectly in Controller using model binders.

public HttpResponseMessage Get([FromUri] KendoFilter filter)

But in my API i am using command pattern inkstand of controllers. So i am forced to deserialize it from raw query string.