lundi 21 novembre 2016

Swift Reflection mapping attributes

Are there anything look like java reflection in swift or I have to always map one by one attribute like following code?

class User: Model {

var name: String

override init(data: Dictionary<String, AnyObject>){
    super.init(data: data)
    self.name = data["name"] as? String
    if let vouchers_count = data["vouchers_count"] as? Int {
        self.vouchers_count = vouchers_count
    }
}





Get all extension methods for type by use roslyn api

I want get all extensions methods for type. For example for type "string" if i'am use semanticmodel method lookupsymbols

var sourceText = @"string a = "";
a."
var tree= CSharpSyntaxTree.ParseText(sourceTextx...);
....
var members = semanticModel.LookupSymbols(source.IndexOf("a."), someType, includeReducedExtensionMethods: true);
//this return all members with extension methods for type string someExtMethod(this string text)....


//I want get all extension with methods where first param might be one of them 
var interfaces = someType.AllInerfaces;
//for example IEnumerable<out T>, methods like: Select, Where, Aggregate...





Spurious method frame on Java call stack when using generics

I developed the following sample class to better clarify my doubt.

package test;

import java.util.Arrays;

public class Main {
    public static void main( String[] args ) {
        new A( new StackDumper() ).exec( "test" );
        new B( new GenericStackDumper() ).exec( "test" );
    }

    public interface I {
        void test(String s);
    }

    public static class A {
        private final I i;

        public A( I i ) {
            this.i = i;
        }

        public void exec(String b) {
            i.test( b );
        }
    }

    public static class StackDumper implements I {
        public void test( String b ) {
            StackTraceElement[] elements = Thread.currentThread().getStackTrace();
            System.out.println( Arrays.toString( elements ) );
        }
    }

    public interface GenericI<T> {
        void test(T t);
    }

    public static class B {
        private final GenericI<String> i;

        public B( GenericI<String> i ) {
            this.i = i;
        }

        public void exec(String b) {
            i.test( b );
        }
    }

    public static class GenericStackDumper implements GenericI<String> {
        @Override
        public void test( String b ) {
            StackTraceElement[] elements = Thread.currentThread().getStackTrace();
            System.out.println( Arrays.toString( elements ) );
        }
    }
}

As expected the first statement of in my main method prints something like:

[java.lang.Thread.getStackTrace(Thread.java:1552),
test.Main$StackDumper.test(Main.java:29), 
test.Main$A.exec(Main.java:23), 
test.Main.main(Main.java:7)]

Conversely the output of the second statement is a bit more surprising:

[java.lang.Thread.getStackTrace(Thread.java:1552), 
test.Main$GenericStackDumper.test(Main.java:53), 
test.Main$GenericStackDumper.test(Main.java:50), 
test.Main$B.exec(Main.java:46), 
test.main(Main.java:8)]

What I don't understand is why on this last call stack I see Main$GenericStackDumper.test printed twice, where one entry (line 53) is the actual invocation, while the other (line 50) corresponds to the line where the GenericStackDumper class is declared.

The only difference between the 2 implementations, and then the cause of this weird behaviour, is in the fact that in the second case I'm implementing a generified interface. However I couldn't find any justification for this in Java or JVM specs. Can anybody explain why this is happening?





Get MethodInfo of method - This operation is only valid on generic types

I have the following two Entity Framework's Include methods:

public static IIncludableQueryable<TEntity, TProperty> Include<TEntity, TProperty>(
    [NotNullAttribute] this IQueryable<TEntity> source, 
    [NotNullAttribute] Expression<Func<TEntity, TProperty>> navigationPropertyPath) 
    where TEntity : class;

public static IQueryable<TEntity> Include<TEntity>(
    [NotNullAttribute] this IQueryable<TEntity> source,
    [NotNullAttribute][NotParameterized] string navigationPropertyPath) 
    where TEntity : class;

I need to get the MethodInfo for both methods. For the first one I used:

  MethodInfo include1 = typeof(EntityFrameworkQueryableExtensions)
    .GetMethods().First(x => x.Name == "Include" && x.GetParameters()
    .Select(y => y.ParameterType.GetGenericTypeDefinition())
    .SequenceEqual(new[] { typeof(IQueryable<>), typeof(Expression<>) }));

This works but when I try to get the second one using the following:

  MethodInfo include2 = typeof(EntityFrameworkQueryableExtensions)
    .GetMethods().First(x => x.Name == "Include" && x.GetParameters()
    .Select(y => y.ParameterType.GetGenericTypeDefinition())
    .SequenceEqual(new[] { typeof(IQueryable<>), typeof(String) }));

I get the error:

This operation is only valid on generic types

What am I missing?





How to use reflection to identify sections of code that are hitting REST services

I have a fairly robust REST API that is being consumed by a variety of intranet applications. The REST service and consuming applications are all written in .Net. Framework versions vary a bit, but the majority of the code is 4.5.2.

Is there any tools I can use to look through a solution and find the places where my applications are calling my service. The goal is to be able to know which applications need to be updated/tested given a change to a specific section of the API.

I feel like I can do this with reSharpers 'Find usages', but I need to do it in a way where it loops through say all the methods in a namespace rather than doing each individual method.





dimanche 20 novembre 2016

How to convert scala Manifest to TypeTag?

TypeTag in scala 2.10 is notoriously useless as it cannot be serailized into anything else without losing higher kind information. So I'm reverting back to good old Manifest. However, some of the libraries I used still use TypeTag, to use them I have to convert Manifest into TypeTag. Is there a consistent and reliable way to do it across all scala version?

Thanks a lot for your opionion





Absolute Singleton for multiple classloaders in the same JVM

I am trying to build an RMI Client/Server in the same JVM but I am facing a problem while using the same Singleton instance for both Client and Server when running each of them. For both client and the server a distinct instance of my singleton is created. After reading a bit about the potential problems a developer may face while implementing the Singleton design pattern, I figured out that is mainly due to the different ClassLoaders of both Client and Server although they are running on the same JVM (Or there's may be some other reason I couldn't see?!). So after reading some recommended articles like Singleton Pattern in Java and Java Reflection I finally implemented this solution which I am not sure if it is the right way to do and which throws by the way an exception :

//This is the method used to get my Singleton (i.e: Controller) instance
public static Controller getInstance() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

    Class obj = getClassLosader().loadClass(Controller.class.getName());//java.lang.ClassNotFoundException thrown here
    Method getInstanceMethod = obj.getDeclaredMethod("getInstanceSingleton", new Class[] { });      
    Object absoluteController = getInstanceMethod.invoke(null, new Object[] { } );
    return (Controller)absoluteController;      
}

public static Controller getInstanceSingleton(){
        if(controller==null)
            controller = new Controller();

        return controller;
}

private static ClassLoader getClassLosader() throws ClassNotFoundException {
    ClassLoader classLoader = Controller.class.getClassLoader().getParent();
    if(classLoader == null)
            classLoader = Controller.class.getClassLoader();
    return classLoader;
}

Any help will be more than welcome. Thanks in advance.