lundi 19 septembre 2016

Why is getMethod() returning null in this case?

var v = xyz.GetType();    
var generic = v.GetMethod("Update", new Type[] { typeof(IEnumerable<>) });

...

public class xyz
{
    public void Update<T>(IEnumerable<T> translations) where T : ijk
...

Calling v.GetMethods()[1] returns the correct method.





load object to stack in Reflection.Emit C#

I'm trying to adapt this workaround to my project. Instead of a string I want to give an object (of type RevitFamily) to the base constructor.

Unfortunately, I can't find out how to load an object or object reference onto the stack using Reflection.Emit. Can Anyone help me?

So far my code looks like this, but I can't give the object to gen.Emit(opcode,_):

                TypeBuilder tb = mb.DefineType(newtypename, new TypeAttributes(), typeof(LoadFamily));
                var ci = typeof(LoadFamily).GetConstructor(new[] { typeof(RevitFamily) });
                var constructorBuilder = tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[0]);
                var gen = constructorBuilder.GetILGenerator();
                gen.DeclareLocal(typeof(RevitFamily));

                Label ilLabel = gen.DefineLabel();
                gen.Emit(OpCodes.Ldarg_0);                // Load "this" onto eval stack
                // Here I should some how get he object f in the stack....
                gen.Emit(OpCodes.Ldloca, f);              // Load pointer to RevitFamily on stack
                gen.Emit(OpCodes.Call, ci);               // call base constructor (consumes "this" and the string)
                gen.Emit(OpCodes.Nop);                    // Fill some space - this is how it is generated for equivalent C# code
                gen.Emit(OpCodes.Nop);
                gen.Emit(OpCodes.Nop);
                gen.Emit(OpCodes.Ret);                    // return from constructor
                t = tb.CreateType();

It is supposed to create dynamic type with a constructor similar to this:

public class LoadconcreteFamily : LoadFamily
{
    public LoadconcreteFamily() : base(f)
    { }
}





Java reflection: getting list of declared variables within same class? [duplicate]

This question already has an answer here:

Is it possible to get a list of declared variables within the same class? To be very honest, I looked at nearly every post about reflection I could find. There is too much going on though, I just need a simple example. I know reflection api has getDeclaredField() method.

What I am trying to achieve is something like this:

for (var : variablesList) {
  var = null;
}

The reason I need this is because I want to set variable.getStyleClass().add(variableName); for every variable there is.





dimanche 18 septembre 2016

Best practice for loading multiple dynamic services and them dependencies services

I want to devlop a custom system for my self.

I want to loading custom services by configuratin with dependency services - for example:

<Services>
    <Service name="ServiceA" args="" type="CommonLib.IServiceA" dependencies=""/>
    <Service name="ServiceB" args="" type="CommonLib.IServiceB" dependencies="ServiceA"/>
    <Service name="ServiceC" args="" type="CommonLib.IServiceC" dependencies="ServiceA,ServiceB"/>
    <Service name="ServiceD" args="" type="CommonLib.IServiceD" dependencies="ServiceA,ServiceB,ServiceC"/>
    <Service name="ServiceE" args="" type="CommonLib.IServiceE" dependencies="ServiceA,ServiceB,ServiceC,ServiceD"/>
    <Service name="ServiceF" args="" type="CommonLib.IServiceF" dependencies="ServiceA,ServiceB,ServiceC,ServiceD,ServiceE"/>
</Services>

All those services are implement custom interface:

public interface IService
{
    bool Start();
    bool Stop();
    bool IsReady {get;}
}

What the best practice for loading dynamic services toghether but depends on those dependencies?

Loop every service and postpone till those dependencies are loaded and ready?

Have any tutorial for that?





Recursively find a property in an object

I have a RootObject which can have multiple properties in it e.g A, B C below. RootObject is the result of an API call. It can have variable number of properties returned depending on the query used to call the API. In one call it may return A,B,C. For a different query it may return A,B,X,Y. When I get the RootObject I do not know what all properties it will have.

Now each property will have a screenshot object in it e.g. B->screenshot. Or it will have screenshot property burried under its sub property. eg. B->B1->Screenshot.

How can I get all the screenshot objects present in my RootObject? I want to get the following:

rootobject.a.s

rootobject.b.b1.s

public class RootObject
{
  A a;
  B b;
  C c;

}
public class A
{
  Screenshot S;
}
public class B
{
  B1 b1;

}
public class B1
{
  Screenshot S;
}





How to find classes in .war file right after deployment

I want to inspect some classes in my .war file right after I have deployed it. But I don't know how to achieve this (most likely due to my lack of JavaEE understanding).

I tried something like below using ClassPath from google's Guava library. It finds all sorts of classes when I pass “com” as an argument. However when passing “com.mypackage” nothing is found.

Any help is appreciated.

@Startup
@Singleton
public class Inspector {

    @PostConstruct
    public void inspect() {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        //ClassLoader classLoader = ClassLoader.getSystemClassLoader(); // doesn't make any difference
        ClassLoader classLoader = this.getClass().getClassLoader(); // // doesn't make any difference
        try {
            for (final ClassPath.ClassInfo info : ClassPath.from(classLoader).getTopLevelClassesRecursive("com.mypackage")) {
                    System.out.println(info.getName());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }   

    } 
    ….
    ..





How do I get a Class object from a method's type parameter?

I have a type-parameterized method but I can't call getClass on the type-parameterized parameter because it can be null, so the only solution I see is to get the Class from the type parameter itself, somehow:

public <T> Class myMethod(T obj)
{
    //can't do this: return obj.getClass() because null is permitted.
}