jeudi 12 septembre 2019

Get class constructor argument names

I'm implementing a certain dependency injection solution for ES classes, and for it I need to know exact names of class constructor parameters.

When I get string form of class or class static method, it does give full code (as usually for functions), but for class constructor it does not.

class C { constructor(a, b) { }; static m(x,y) { } }
console.log(C);
console.log(C.constructor);
console.log(C.m);

results in

class C { constructor(a, b) { }; static m(x,y) { } }
ƒ Function() { [native code] }
ƒ m(x,y) { }

As result, I have to parse whole class code to extract constructor arguments part.

Is there any cleaner way to get constructor argument names?





mercredi 11 septembre 2019

Instantiation Exception while invoking method with parameter

I am trying to invoke method with parameter in a class using reflection. While instantiating the class, I get error "Instantiation Exception".

public class get_reflection {
int b1=20;
String classname="org.la4j.linear.LeastSquaresSolver";
Class<?> c = Class.forName(classname);
 Object user = c.newInstance();
Method m = org.la4j.linear.LeastSquaresSolver.class.getMethod("get",Integer.class);
Object o= m.invoke(user,b1 ); 
}

LeastSquareSolver.java

public class LeastSquaresSolver extends AbstractSolver implements LinearSystemSolver {
public LeastSquaresSolver(Matrix a) {
        super(a);

        // we use QR for this
        MatrixDecompositor decompositor = a.withDecompositor(LinearAlgebra.RAW_QR);
        Matrix[] qrr = decompositor.decompose();

        // TODO: Do something with it.
        this.qr = qrr[0];
        this.r = qrr[1];
    }

    public void get(Integer b9)
    {
        System.out.println(b9);

    }

I expect to invoke the method get and print the value. Currently, I get compiler error at c.newInstance();





How to access a struct tag from inside a field-type in golang

I want to know if and how it is possible to access a struct tag set from a custom type used inside this struct.

type Out struct {
    C Custom `format:"asd"`
}

type Custom struct {
}

func (c Custom) GetTag() string {
    // somehow get access to `format:"asd"`
}

My goal is to be able to define a timeformat for un/marshaling and handle the actual time-unmarshalling parameterized by the structtag.

Thanks





Call scala method from object dynamically

I have a scala case class and object like below,

case class User(userId: Long, UserName: String, ts: Timestamp)

object User {

  def getRdd(rdd: RDD[JsValue], type : String): RDD[User] = {

    val rdd1: RDD[User] = rdd.map(doc => processEvent(doc))
      .filter(event => event._1.equals(rddType)).map(event => {
      User.get_class_obj(event)
    })
    rdd1
  } 

}

I want to call "getRdd" method of User Object from another object without creating instance of object/class. like below,

val object_name = "com.User"
val method_name = "getRdd"

I tried,

val no = Array(1, 2, 3, 4, 5,6,7,8,9,10)
val rdd = sc.parallelize(no)


Class.forName(object_name).getDeclaredMethod(method_name).invoke(rdd)

but fails with nosuchmethod error. I have gone through answers here but I don't want to create multiple instances every time. Is it possible to do it in one-liner.





How to list all DLLs in my solution and get their version?

I've just started using C# and am currently trying to create a class that will compare the version of all dll files with a version string in my database.

However, I am not sure how to get all dll files that belong to my solution. I've tried the following:

Assembly[] applicationDLLs = AppDomain.CurrentDomain.GetAssemblies();

I found this on a forum somewhere. But I don't know what using statements are required and if this is valid code at all.

Can any of you point me in the right direction?





mardi 10 septembre 2019

C# 'type' is a variable but is used like a type

This code throws an error C# 'type' is a variable but is used like a type

Type type = Type.GetType("Objects.Camera");
for (int i = 0; i < ((List<type>)currentObject).Count; i++)
{
 //...
}

currentObject is obtained with .GetValue(object) and has type object. I need to cast it to the proper type in order to use it as a list.

Thank you





How to sort through a generic list object

My code is designed to parse an unknown json file using json.net that has nested classes and create checkboxes to easily look through it.

At some point during the parsing it arrives to a List object

I have the type and the actual object

The type has AssemblyQualifiedName "System.Collections.Generic.List`1[[Calibration.Camera, CalibrationOrganizer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

The object is a List<Calibration.Camera>

If I loop through the List this way it works, but the problem is I'm assuming a known data type, which I don't. This is just one of many data types in the json file

if (currentType.Name == "List`1")
            {
                for (int i = 0; i < ((List<Calibration.Camera>)currentValue).Count; i++)
                {
                    cbox = new CheckBox();
                    cbox.Text = "[" + i.ToString() + "]";
                    cbox.Name = prev_properties + "!" + curr_property;
                    cbox.AutoSize = true;
                    cbox.Location = new Point(1100 + prop_list.Count() * 100, i++ * 20); //vertical
                    cbox.CheckedChanged += new EventHandler(ck_CheckedChanged);
                    this.Controls.Add(cbox);
                }
            }

If I try this, it wont compile

for (int i = 0; i < currentValue.Count; i++) {...}

with error: Operator '<' cannot be applied to operands of type 'int' and 'method group'

If I try this, it crashes

for (int i = 0; i < ((List<object>)currentValue).Count; i++)

with exception: System.InvalidCastException: 'Unable to cast object of type 'System.Collections.Generic.List1[Calibration.Camera]' to type 'System.Collections.Generic.List1[System.Object]'.'

So Im not sure what I can do,

I can parse the AssemblyQualifiedName and get the Object type as a string, but how do I convert it to an object type again?