dimanche 5 mai 2019

How can i get the name of an element that invoked an action using IInterceptor from Castle.DynamicProxy

So, pretty much what I'm trying to do is an aspect using the IInterceptor interface from the Castle.DynamicProxy namespace for logging certain element actions and save them to a file.

The implementation I have looks as describe below.

The interface type actions i'm intercepting look like this:

public interface IPageElement
{
   bool Visible { get; }
   void Select();
   ...
}

I have a class that its function is to serve as a factory for creating objects of type IPageElement. So, every time an factory method is called, I returned a proxy element by using CreateInterfaceProxyWithTarget<T>, this way:

return ProxyGenerator()
   .CreateInterfaceProxyWithTarget<IPageElement>(
      elementToProxy,
      new MyInterceptor(elementToProxy));

As you my guess, MyInterceptor class implements the IInterceptor interface from Castle.DynamicProxy. So, what I'd like to know is how to get the name of that IPageElement object that is being intercepted.

I've managed to get the method it calls, the params (if any), etc. by using the IInvocation parameter that Intercept method has:

invocation.Method; // for the method
invocation.Arguments;  // for the arguments of the method

Tried other of the methods/properties IInvocation has, but not luck.

Any help or suggestion is greatly appreciated :) Thanks in advanced.





I just can't understand

Is what I am doing even possible??? Because it doesn't work it doesn't even throw exceptions

[AttributeUsage(AttributeTargets.Field, AllowMultiple = true, Inherited = false)]
public class ScreenObject : Attribute
{

}

public class MenuCanvasController : MonoBehaviour

[ScreenObject] public GameObject wellcomeScreen;
[ScreenObject] public GameObject wellcomeMenuScreen;
[ScreenObject] public GameObject RegistrationScreen;
[ScreenObject] public GameObject SignInScreen;
[ScreenObject] public GameObject HomeScreen;

public static MenuCanvasController instance;

private void Awake()
{
    instance = this;
}

void Update ()
{
    (typeof(MenuCanvasController).GetField("wellcomeScreen").GetValue(instance) as GameObject).SetActive(false);
}

I want to make simple functions for controlling a so that my friend will stop write code like this ...

public void GoToRegistrationScreen()
{
    Debug.Log("Make some transitions ");
    wellcomeScreen.SetActive(false);
    wellcomeMenuScreen.SetActive(false);
    RegistrationScreen.SetActive(true);
    SignInScreen.SetActive(false);
    HomeScreen.SetActive(false);
}





Golang: reverse of reflect.TypeOf

I want to retrieve back the type of value I saved once. I used reflect.Typeof() and saved the type. then try to use switch type. the type will always be "*reflect.rtype ". I couldn't retrieve either by type assertion.

package main

import (
    "fmt"
    "reflect"
)

func main() {
    var alltypes []interface{}

    alltypes = append(alltypes, reflect.TypeOf(true))
    alltypes = append(alltypes, reflect.TypeOf(0.0))
    alltypes = append(alltypes, reflect.TypeOf(0))
    fmt.Printf("%T\t%q\n", alltypes, alltypes)

    for _, v := range alltypes {
        fmt.Printf("%T\t%q\n", v, v)
        res, ok := v.(bool)
        fmt.Println("res: ", res, " ok: ", ok)
        switch v.(type) {
        default:
            fmt.Printf("unexpected type %T\n", v)
        case bool:
            fmt.Println("bool type!")
        case int:
            fmt.Println("int type!")
        case float64:
            fmt.Println("float64 type!")
        }
    }

}

Playground: https://play.golang.org/p/kqDo4DPYjra





Is there a way to simulate property selectors in java?

In C# we have the option to specify a property of an object using property selectors (for example when using LINQ) like so:

var usernames = customers.Where(x => x.Age > 20).Select(x => x.Username);

Here we first select the property Age to perform the comparison on and then specify the property Username to the Select(...) clause.

I am currently trying to replicate this functionality using lambda expressions in Java to enable users of my code to specify which property should be used for some action later on. The final result should look similar to the following:

public class Builder<T> {

// ...
private Field field;

Builder<T> forField(SomeFunctionalInterface s) {
    this.field = s.evaluate();
    return this;
 }
// ...
}

Thank you for your effort.





samedi 4 mai 2019

using getDeclaredMethod java

I've got this piece of code:

Class[] parameter_types = new Class[parametros_servicio.size()];
int i = 0;
while(i < parametros_servicio.size()){
    parameter_types[i] = String.class;
    i++;
}
Method m = c.getDeclaredMethod(nom_servicio, parameter_types);

If I leave it like that, everything works fine. If I change it and I set this parameter_types[i] = Object.class, it doesn't work. The methods I'm working with have only String parameters but I'd like to have some others which can have other datatypes.





vendredi 3 mai 2019

What is the TypeLoadException from Reflection?

I have the following code:

    public static IEnumerable<MethodInfo> FindStaticMethods<T>()
    { 
        var M =
            from Assemblies in AppDomain.CurrentDomain.GetAssemblies().AsParallel()
            from Types in Assemblies.GetTypes()
            from Methods in Types.GetRuntimeMethods()
            where Methods.IsStatic
            let Attributes = Methods.GetCustomAttributes(typeof(T), true)
            where Attributes?.Length > 0
            select Methods;

        var L = M.ToList();

        return L;
    }

and I get the following error:

System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Could not load file or assembly 'Microsoft.AspNetCore.Mvc.Core, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

Could not load file or assembly 'Microsoft.AspNetCore.Mvc.ViewFeatures, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

Could not load file or assembly 'Microsoft.AspNetCore.Mvc.ViewFeatures, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

Could not load file or assembly 'Microsoft.AspNetCore.Mvc.ViewFeatures, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

Could not load file or assembly 'Microsoft.AspNetCore.Mvc.ViewFeatures, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

Could not load file or assembly 'Microsoft.AspNetCore.Mvc.Core, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

Could not load file or assembly 'Microsoft.AspNetCore.Mvc.Core, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module) at System.Reflection.RuntimeModule.GetTypes() at System.Reflection.Assembly.GetTypes() ...

I don't understand this error, can anyone explain?





Finding all occurrences of a given code snippet in java code

I am working on a particular utility to identify all occurrences of the given java snippet in the codebase.

E.g. I want to find the occurrences of list.get(0) in my java file. I would like to identify the method and class names of each such occurrence.

This could be done by parsing a .java file but I'm looking for a cleaner solution. Is it possible with reflection