vendredi 30 juin 2023

Instrumenting Java code (Spring beans) in most non intrusive way runtime wise

I am working on designing a library which would gather metrics and runtime data in Java code. The classes I want to instrument are Spring beans. Can someone suggest how to build such a system or existing libraries/tools (for reference) or any reference books/articles? Currently, I am exploring how to use AOP to intercept various methods but not very sure of the performance impact it will have.





mercredi 28 juin 2023

Reflection get field of Java class

I am a little confused working with the newer versions of Java and Reflections. I have a somewhat hacky code part which requires a tiny bit of changing the Java Runtime behavior.

I want to update the parent field on the ClassLoader class. This worked with Java 8 without any issues, but trying versions like Java 11+ I get the following exception:

java.lang.NoSuchFieldException: parent
    at java.base/java.lang.Class.getDeclaredField(Class.java:2707)

So my first thought was that it has been removed or renamed, but that isn't the case. I then read about their changes to prevent reflection access with the illegal reflective access. However this was removed with Java 17.

In fact, I cannot access or get any field at all. When running the following line, I get 0 as a result (using Java 20):

System.out.println(Main.class.getClassLoader().getClass().getDeclaredFields().length);

I can somewhat understand the reason for this, but it is hard to believe that they completly prevent reflective access to the runtime. Does anybody know how I can "fix" this?

PS: I know that reflection is unsafe and should be prevented, but I cannot use an alternative to get the desired behavior of my application.





mardi 27 juin 2023

Python get imported modules from source code returned by inspect.getsource

I'm using inspect.getsource to retrieve the source code of functions. However, since some variables are imported at top level of the module containing the functions, it is not returned by inspect.getsource. For example, if we have

import numpy as np
def sqrt(x):
    return np.sqrt(x)

then inspect.getsource(sqrt) will not return the line import numpy as np. Is it possible to automatically retrieve what np here refers to? The import can also be other forms, such as from some_module import some_func, from some_module import *, import some_module as some_name.

Right now I'm using this function in trace function called by sys.settrace, so the solution can be both static (without running the code) or dynamic (when running the code, such as examining the frames).





Why upperBounds and lowerBounds of WildcardType are array in java?

Why upperBounds and lowerBounds of WildcardType are array in java?

// Only has a Number
? extends Number

T has two (Number and Serializable) but T is TypeVariable not WildcardType

? extends Number & Serializable

And in jdk implementation com.sun.beans.WildcardTypeImpl and sun.reflect.generics.reflectiveObjects.WildcardTypeImpl I see this code in toString():

for(int var3 = 0; var3 < var2.length; ++var3) {
            if (var3 > 0) {
                var1.append(" & ");
            }

            var1.append(var2[var3] instanceof Class ? ((Class)var2[var3]).getName() : var2[var3].toString());
        }

Why? Is this legal??? ? extends Number & Serializable

Same problem for rawType of ParameterizedType: Why return Type not Class?





Scala: Convert Iterable[Any] to the actual type of its content

I want to be able cast a generic iterable to the actual type of its contents, but I don't know the type at compile time. Is there a way to have a function like this?

def castIterable[IN, OUT](iterable: Iterable[IN]): Iterable[OUT] = {
  iterable.asInstanceOf[Iterable[ figureOutContentType(...) ]]
}




lundi 26 juin 2023

Why java.lang.Class doesn't implement java.lang.reflect.Member interface?

I'm working on a Package Browser in browser in Java and trying to write a generic method which can handle all members of the class - basically collecting string representation of them, choosing an image and displaying it in a tree view.

For all Members (Field, Method, Constructor) it has been working great - they all have getName() and isSynthetic() which allows me to treat them in the same way because that's all I need.

But Class is special - even though it has all the methods of that interface, it doesn't technically implement it!.

And to handle it I need a special-case-method or do an instanceof checks, which is not very clean.

So, i guess, this is more of a philosophical question - why is that? Because classes are not considered to be "proper" members of classes and instead are nested? Or because of something else?

Please shine the light on this topic!

I tried to treat objects of type Class<?> as if they implemented interface Member.





Scala: Convert string to lambda

I want to be able to parse strings like "x => x + 1" and be able to actually use it as a lambda function like this:

    val string = "x => x + 1"
    val lambda = parseLambda(string)
    val numbers = List(1, 2, 3)
    val result = numbers.map(lambda) // Should be [2, 3, 4]

I have the following implementation for the parseLambda function:

  def parseLambda(string: String): Any => Any = {
      val toolbox = runtimeMirror(getClass.getClassLoader).mkToolBox()
      val tree = toolbox.parse(string)
      val lambdaFunction = toolbox.compile(tree)().asInstanceOf[Any => Any]
      lambdaFunction
  }

It works for strings like "(x: Int) => x * 2" or "(s: String) => s.split(" ")" but if I omit the types e.g. "x => x * 2" or "s => s.split(" ")" I get the following error

';' expected but '=>' found.

Is there a way to be able to omit the types in the string? Any help would be appreciated!





vendredi 23 juin 2023

Get nested property name and its value from object C#

I have an object and I want to get full primitive property path and its value.

Type1 Prop1
   |_ Type2 Prop 2
         |_ string Text
   |_ Type3 Prop3
         |_ Type4 Prop4
                |_ int Number
    ...

What I tried so far:

var props = obj.Prop1.GetType().GetProperties();

foreach (var prop in props)
{
     var name = GetData(prop, obj);
     // add name to list
}
static bool IsPrimitive(Type t)
{
    return t.IsPrimitive || t == typeof(string);
}
static KeyValuePair<string,object> GetData(PropertyInfo property, object reference)
{
            if (IsPrimitive(property.PropertyType))
            {
                return new KeyValuePair<string, object>(property.Name, property.GetValue(reference,null));
            }

            var props = property.PropertyType.GetProperties();
            var name = property.Name;
            object value = null;

            foreach (var p in props)
            {
                name += $".{GetData(p, reference)}.";
                value = p.GetValue(reference, null);

                if (IsPrimitive(p.PropertyType))
                {
                    break;
                }
            }

            return new KeyValuePair<string, object>(name.Trim('.'), value);
}

The problem is that for GetValue(reference, null), I get exception:

'Object does not match target type.'

What I'm doing wrong ? I tried:

GetData(prop, obj); also GetData(prop, obj.Prop1);, but same error

The expected output would be:

Prop1.Prop2.Text and its value
Prop1.Prop3.Prop4.Number and its value




jeudi 22 juin 2023

C# and multi-level reflection

I have a very complex object for which I am trying to write validation methods. The information comes in, is serialized into the object and then I'm trying to do one of two things:

  1. Check if the required fields have data
  2. For those fields that have default values that can be used, if no value exists, set the field to the default value.

A simplified version of the JSON data looks as follows:

{
    "User": {
        "Name": "John Dude",
        "Department": "Sales"
    },
    
    "IDNumber": "23-0019872",
    "Addresses": [{
            "Street": "123 One St",
            "City": "Pittsville",
            "State": "CA",
            "Zip": "95432"
        },
        {
            "Street": "987 Dead End Dr",
            "City": "Nowhere",
            "State": "MI",
            "Zip": "45987"
        }
    ]
}

So my complex object has simple fields (string, int, bool, etc.), objects (User), and lists of objects (Addresses).

In my config file I have a list of data to check. So the various fields are either like .User or Addresses.Street. Nothing before the dot indicates the element is at the root and if it has something before the dot, it means that I have to check that object for a field.

I would think that checking the fields should be just a simple matter of using reflection. It doesn't matter what kind of object it is accessing, so long as the value is not null or empty, it is enough to pass this validation level (more specific checks will come later).

I'm able to check the first level fields (User, IDNumber, and Address) with the standard code.

if (messageData.GetType().GetProperty(FullField[1]).ToString() == null ||
    messageData.GetType().GetProperty(FullField[1]).ToString() == String.Empty) {
       wasFieldFound = false;
}

The problem comes when I'm trying to check the complex object fields or the list object fields.

var container = messageData.GetType().GetProperty(FullField[0]);
string ContainerName = container.Name;
string[] ListElements = 
     ConfigurationManager.AppSettings["ListFields"].ToString().Split('|');
bool isList = false;

foreach (string Element in ListElements) {
    if (ContainerName.Equals(Element))
       isList = true;
}

if (!isList) {
   if (container.GetType().GetProperty(FullField[1]).ToString() == null ||
       container.GetType().GetProperty(FullField[1]).ToString().Equals(String.Empty))
            wasFieldFound = false;
}

In the case where the item is just a complex object, the field I am checking is read into the string array FullField (so User.Name is split into FullField[0] = User and FullField[1] = Name). The variable container is getting the User element but the check on FullField[1] is null. I've thought about trying to cast the container to another variable of the correct type but I've not had any luck with that.

The short question is how do you get an object via reflextion and then access a field or property of that object via reflextion?





Type.InvokeMember not invoking method and not throwing

I have this method that invokes other methods by reflection:

public void InvokeInstanceMethod(string projectName, string typeName,
        string methodName, IDictionary<string, object> args, object instance)
{
    var assembly = GetAssembly(projectName);

    var type = GetType(projectName, typeName, assembly);

    type.InvokeMember(methodName, BindingFlags.InvokeMethod |
        BindingFlags.Public | BindingFlags.Instance, null, instance,
        args.Select(a => a.Value).ToArray(), null, null, args.Select(a => a.Key).ToArray());

}

Normally it works ok, but sometimes it just doesn't invoke the member and it doesn't throw, it simply continues execuction as if everything went fine, there is no pattern I can spot, for the same method sometimes it works, others it doesn't.

Anything wrong with my implementation?

https://learn.microsoft.com/en-us/dotnet/api/system.type.invokemember?view=netcore-3.1

Using dotnet Core 3.1





Binding on dynamically generated controls using reflection

I am trying to make a page to display and edit the details on a subject. Depending on the type of subject, we want to display different fields to the user. Some subject types need to display a barcode field, some needs a Tutor name, etc etc.

Some may have a total of 5-6 fields, another one can have 12 fields to display, and this is all configurable, so my page has to be dynamic, I cannot have anything hardcoded.

What I did so far is that I created this ItemsControl on my page (which is a UserControl):

<!--  Subject Info section  -->
<Grid Grid.Row="2" Grid.Column="0">
    <ItemsControl ItemsSource="{Binding SubjectDataFields}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Columns="2" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid Margin="5">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <TextBlock Text="{Binding Name}" />
                    <TextBox Grid.Column="1">
                        <TextBox.Text>
                            <MultiBinding Converter="{StaticResource SubjectFieldValueConverter}">
                                <Binding Path="DataContext.Subject" RelativeSource="{RelativeSource AncestorType=UserControl}" />
                                <Binding Path="Field" />
                            </MultiBinding>
                        </TextBox.Text>
                    </TextBox>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

SubjectDataFields is a List<SubjectDataField>. SubjectDataField is defined as such:

public class SubjectDataField
{
    [Key, Column(Order = 0)]
    public string SubjectTypeIDFromServer { get; set; }
    [Key, Column(Order = 1)]
    public string Field { get; set; }
    public string Name { get; set; }
    public int DisplayOrder { get; set; }
    public bool Required { get; set; }

    
    public SubjectDataField()
    {
    }
}

I have created a converter to use multibinding in order to get the correct fields bound:

public class SubjectFieldValueConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values.Length >= 2 && values[0] is Subject subject && values[1] is string fieldName)
        {
            string dynamicFieldPrefix = "dynamic_";

            // Take dynamic fields into consideration
            if (fieldName.StartsWith(dynamicFieldPrefix))
            {
                fieldName = fieldName.Substring(dynamicFieldPrefix.Length);
                using (DbContext db = new DbContext())
                {
                    return db.SubjectCustomDatas.Where(scd => scd.SubjectId == subject.Id && scd.Key == fieldName).FirstOrDefault()?.Value ?? "";
                }
            }

            // Not a dynamic field, Retrieve the property, if it exists.
            var propertyInfo = subject.GetType().GetProperty(fieldName);
            if (propertyInfo != null)
                return propertyInfo?.GetValue(subject);

            return "";
        }

        return null;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        return new object[] { Binding.DoNothing, Binding.DoNothing };
    }

}

As you can see, some fields are actual properties from the object and are fetched using reflection, and some other have to go look into a dynamic fields table. The Convert function is working as intended. Both my properties and my dynamic fields are fetched properly, and performances look good.

However, when I wanted to implement the ConvertBack method, I am having an issue. The value contains the new value as entered in the text box, but I don't get neither the Subject object, nor am I getting the Field name. The Convert back receives a Type[] targetTypes, but as I understand, this is nothing but the definition of types, so I do se Subject for the subject and String for the field name, but I don't see how I can actually access either the Subject itself or the Filed name itself.

I have asked GPT for help, but he quickly went into a circle. It tried to get me to add ConverterParameter to the binding in the xaml and bind it with the subject, but then my page doesn't load at all, but I am not getting any error message... I am unsure how to debug this part. I am using the correct approach? What am I doing wrong?





Trying to run current script again causes stack overflow or parserror?

I'm writing a script that will modify the code in the script and rerun it automatically but whichever way I do it, it always goes into repeated output until the kernel restarts and if I keyboard interrupt sometimes I get: ParserError: Error tokenizing data. C error: Calling read(nbytes) on source failed. Try engine='python'. And usually this is a Pandas error, but there's no Pandas. Ideally, I would like to accomplish this without using shell and also be able to set a breakpoint so instead of accessing the current file I can just access everything written up until that point. This is every way I've tried it. I think maybe if there was a way to do introspection from a context manager (like if there is a way to access an object from yield) that would be great to know.


    current_file = clear_me()
    code_to_be = "".join(rf"{e}" for e in current_file)
    #str(clear_me()).strip('[]')
    import parser
    xyyx = parser.suite(code_to_be)
    exec(xyyx.compile())
    
    import os
    
    # path_to_script = os.path.dirname(os.path.abspath(__file__))
    # my_filename = os.path.join(path_to_script, "my_file.py")
    
    # with open(my_filename, "w") as handle:
    #     print(code_to_be, file=handle)
    
    #exec(compile(code_to_be, "<string>", "exec"))
    
    import subprocess
    #subprocess.run(["python", f"{__file__}"])
    #exec(open("my_file.py").read())

def clear_me():
    f = open(rf"{__file__}", 'r')
    takethel = f.readlines()
    return takethel




mercredi 21 juin 2023

How to access JPA Repository methods from bean object of the particular repository dynamically

I have a list of repositories that extends JPA Repository and the API needs to fetch a record of a particular table given a primary key..

I have defined a enum from where I'm able to fetch the repository class object from the given table name..

public enum DynamicEntityRepository {

    Table1(Entity1.class),
    Table2(Entity2.class),
    Table3(Entity3.class),
    Table4(Entity4.class),
   
    Class entity;

}

Now, I want to access the findById(id) method of JPA Repository of the particular table..

 DynamicEntityRepository repo = DynamicEntityRepository.valueOf(tableName);
 

 /*
   Here I want to access findById(id) of the particular repository by passing the param
*/

I have tried using reflection to invoke the findById method of the Repository class but it throws NoSuchMethodException as the Repository originally does not have findById method.. It indeed extends JPARepository's default method..

Is there any other way to solve this ?





Unable to retrieve typeArguments for reflected generic in Dart

I'm trying to get real types passed into generic, but I always get empty collection.

So far, to me it is mandatory to use dart:mirrors.

In future though I'm planning to migrate to reflectable.

Please hint me, where I'm wrong?

import 'dart:mirrors'

class A<T> {}

void main() {
  print(
    reflectClass(A<int>).typeArguments
  );
}

Output:

[]

Exited.




Reflection: How to access private method in private inner class that is in different package than caller class

I am having trouble in accessing private method in private inner class that resides in different package than caller class:

package reflectiontest.sompackage;

import reflectiontest.Interface;

public class Outer {
    private class Inner implements Interface {
        public int get() {
            return 4;
        }
    }

    public Interface getInner() {
        return new Inner();
    }
}
package reflectiontest;

public interface Interface {
    int get();
}
package reflectiontest;

import reflectiontest.sompackage.Outer;

import java.lang.reflect.Method;

public class Main {

    public static void main(String[] args) throws Exception {

        Outer outer = new Outer();
        Interface inner = outer.getInner();

        System.out.println(inner.get());

        Method get = inner.getClass().getMethod("get");
        System.out.println(get.invoke(inner));

    }

}

Log after running:

4
Exception in thread "main" java.lang.IllegalAccessException: class reflectiontest.Main cannot access a member of class reflectiontest.sompackage.Outer$Inner with modifiers "public"
    at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:394)
    at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:709)
    at java.base/java.lang.reflect.Method.invoke(Method.java:569)
    at reflectiontest.Main.main(Main.java:17)

During debugging i got to the point in Reflection.verifyMemberAccess() fails at this point:

        if (!Modifier.isPublic(getClassAccessFlags(memberClass))) {
            isSameClassPackage = isSameClassPackage(currentClass, memberClass);
            gotIsSameClassPackage = true;
            if (!isSameClassPackage) {
                return false;
            }
        }

The one option is to create a simple caller class inside the same package as Inner, but this seems a workaround.

Any other ideas on how to get it to work?





mardi 20 juin 2023

Call InitializeComponent without going through the constructor by reflection

I'm developing an application that needs to explore all the UserControls/Forms of a library in a similar way that the Designer of Visual Studio does.

The first part is easy, as I can get all the classes with normal reflection. My problem is when trying to create a new instance, I need to pass the required parameters to the constructor in case there's no empty one.

The solution I'm looking for is to bypass in some way the constructor so I can instantiate the class and then call InitializeComponent in that instance.

How Visual Studio's designer is able to create a new instance for a class with no empty constructor?

I've tried to create a new instance via the Assembly.CreateInstance but if no empty constructor is found an exception is thrown.





lundi 19 juin 2023

Which is the most optimal way to get or set the value of a property using reflection

What is the most optimal way to repeatedly get or set the value of a property using reflection in a long-term perspective?

I tried using plain old reflection but this solution seems a little bit slow to me. I need another solution.





Get class properties of a specific type

I have a WinForm project in c#. I have class named TopProducts that has many DataTable properties and I create a collection of this class.

I want to loop over this collection and create List<Datatable>. You can find details in the comments in the GetListOfDataTables method below.

public class TopProducts
{
    public Guid ID { get; set; }
    public string No { get; set; }
    public string ProductName { get; set; }
    public int ProductQty { get; set; }
    public int Level { get; set; }
    public DataTable dtProfile { get; set; }
    public DataTable dtPBMAndGlass { get; set; }
    public DataTable dtArticals { get; set; }
    public DataTable dtCutting { get; set; }
    public DataTable dtCNC { get; set; }
    public DataTable dtIFS { get; set; }
    public DataTable dtIFSGlassList { get; set; }   
    // public DataTable MoreDataTable
}

public List<DataTable> GetListOfDataTables(List<TopProducts> ProductList)
{
    List<DataTable> dtList = new List<DataTable>();
    foreach (TopProducts product in ProductList)
    {
        //I need codes something similar below
        //if (product.GetType().Equals(DataTable))
        //{
            // dtList.Add(product) 
        //}
    }
    return dtList;
}




C# .net core native AOT with reflection Activator.CreateInstance

I am trying to use reflection's Activator.CreateInstance method to generate the required module with parameters like below.

 public TModule CreateModule<TModule>(params object[]? parameters) where TModule : ApplicationModule
    {
        /*
         * Create module
         */
        TModule? module = Activator.CreateInstance(typeof(TModule), parameters) as TModule;
        if (module is null)
            throw new Exception($"Failed to create module with type: {typeof(TModule).Name}");

        /*
         * Register it to the pending modules
         */
        _pendingModules.Add(module);

        return module;
    }

But it always return

  Unhandled Exception: System.MissingMethodException: No parameterless constructor defined for type 'Runtime.Reflection.ReflectionModule'.
   at System.ActivatorImplementation.CreateInstance(Type, Boolean) + 0x121
   at Runtime.Application.Application.CreateModule[TModule]() + 0x35
   at EditorPlayer.Program.Main(String[]) + 0x107
   at EditorPlayer!<BaseAddress>+0x862f1b

Even though the target class has default constructor it still throws the same error. I thought reflection was present in native AOT builds.





dimanche 18 juin 2023

How to iterate over class properties in Kotlin

I have an abstract class ConfigGroup that has the super class ConfigElement. In ConfigElement a method toJson() is definded. ConfigGroup overrides this method.

Currently toJson() in ConfigGroup looks like this:

override fun toJson(): JsonObject {
        val map: HashMap<String, JsonObject> = HashMap()
        for (child in this::class.declaredMemberProperties) {
            map[child.name] = (child.get(this) as ConfigElement).toJson()
        }
        return JsonObject(map)
    }

But when I try to compile it I get Type mismatch: inferred type is ConfigGroup but Nothing was expected

I am a bit confused since it is almost the same as the example in Kotlin: Iterate over components of object . The only difference being that I try to get the properties from within the same class and therefore have to use the this keyword.





How to use reflect.NewAt on interface{}?

package main

import (
    "encoding/json"
    "fmt"
    "reflect"
    "unsafe"
)

type Stu struct {
    Name string `json:"name"`
}

func MakeStu() interface{} {
    return Stu{
        Name: "Test",
    }
}

func main() {
    jsonData := []byte(`{"name":"New"}`)
    t1 := MakeStu()
    t2 := MakeStu()
    t3 := MakeStu()

    json.Unmarshal(jsonData, &t1)
    fmt.Println(t1) //Type of t1 becomes map[string]interface{} instead of struct Stu

    newPointer := reflect.New(reflect.ValueOf(t2).Type()).Interface()
    json.Unmarshal(jsonData, newPointer)
    fmt.Println(newPointer) //It works,but it need allocate memory to hold temp new variable

    brokenPointer := reflect.NewAt(reflect.ValueOf(t3).Type(), unsafe.Pointer(&t3)).Interface()
    json.Unmarshal(jsonData, brokenPointer)
    fmt.Println(brokenPointer) // I want to get the pointer of original type based on the existed variable t3,but it crashes.
}

If I don't know the concrete type of the interface{} when coding,I can't use interface.(Type) to cast. so how to use reflect.NewAt on interface{}?





samedi 17 juin 2023

Hi a clarification Please about cs50 pset reflection filter

I hope you are doing well. I'm trying to solve the reflection filter in the CS50 Week 4 pset, but I'm stuck. My logic is to use a buffer of type RGBTRIPLE to copy the last pixel from image[n][width - 1] to the first pixel image[n][n] throughout the entire image. However, there seems to be something wrong with my code:

void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    for (int h = 0; h < height; h++)
    {
        RGBTRIPLE buffer;
        for (int w = 0; w < width; w++)
        {
            buffer = image[h][w];
            image[h][w] = image[h][width - w];
            image[h][width - w] = buffer;
        }
    }
    return;
}




vendredi 16 juin 2023

Change object field value by field name

ENV:

  • Spring Boot 3
  • Java 17

CONTEXT:

I have multiple model classes, some of which have fields that should be changed at runtime. For example, a model has a field String name which should be encrypted and decrypted at runtime on demand. I thought about the following solution:

  • I will create a marker annotation that indicates which fields should be encryptable/decryptable.
  • At runtime, there is a method that accepts objects of different classes and analyzes them via reflection. This part already works, and I can find the annotated fields.

Questions:

  1. Is this a good pattern? I thought it would be elegant to just annotate the related fields, and the rest happens decoupled from the object.
  2. Is there a way to read and write a field just by knowing the field name? Or do I have to create a separate instance for each object by reflection and create a copy?




jeudi 15 juin 2023

recursively iterating struct and set fields with go using reflect

I want to build the program that sets field of struct using reflection. I made it work for top-level field but I am struggling with nested struct field. How can iterate over nested struct field?

type Payload struct {
    Type    string   `json:"type"`
    SubItem *SubItem `json:"sub_item"`
}

type SubItem struct {
    Foo string `json:"foo"`
}

func main() {
    var payload Payload
    setValue(&payload, "type", "test1")
    setValue(&payload, "sub_item.foo", "test2")
}

func setValue(structPtr interface{}, key string, value string) {
    structValue := reflect.Indirect(reflect.ValueOf(structPtr))
    for i, subkey := range strings.Split(key, ".") {
        isLast := i == len(strings.Split(key, "."))-1
        var found bool
        // this line is crashing with "reflect: call of reflect.Value.NumField on zero Value"
        for i := 0; i < structValue.NumField(); i++ {
            field := structValue.Type().Field(i)
            jsonTags := strings.Split(field.Tag.Get("json"), ",")
            if jsonTags[0] == subkey {
                found = true
                if isLast {
                    if isLast {
                        // last element
                        // TODO set value
                        fmt.Printf("TODO set value %s to %v", value, structValue)
                        structValue = reflect.Indirect(reflect.ValueOf(structPtr))
                    }
                } else {
                    structValue = reflect.Indirect(reflect.ValueOf(structValue.Field(i).Interface()))
                }
                break
            }
        }
        if !found {
            panic(fmt.Errorf("failed to find field %s", key))
        }
    }
}




Getting generic attribute from a property using reflection without knowing the generic type

C# 11 has introduced generic attributes. Is it possible to get the attributes that are attached to a PropertyInfo using the GetCustomAttribute method (or similar) without knowing it's generic type, particularly where the attribute inherits from another?

Here's what I have:

public class MyTypedAttribute<TValue> : Attribute
{ 
    public virtual TValue ProcessValue(object value) { }
}

public class MyNumberAttribute : MyTypedAttribute<int>
{ 
    public override int ProcessValue(object value) { }
}

public class MyStringAttribute : MyTypedAttribute<string>
{ 
    public override string ProcessValue(object value) { }
}

My class has properties with a range of these attributes on them, and I want to get them using reflection. I would normally use the generic PropertyInfo.GetCustomAttribute<> function, but this doesnt seem to work when the attributes themselves also expect a generic parameter (unless the generic type is known at compile time, which in my case they it's not)

PropertyInfo myProp = ...;
var attr = myProp.GetCustomAttribute<MyTypedAttribute<>>();

Compile error: Unexpected use of an unbound generic name

It seems I can use the non-generic equivalent function, but the returned attribute is of type Attribute?, and it seems I cannot cast this without knowing the generic type. We cannot specify empty angle brackets when casting, so a type must be specified, but I dont know what the type is. The only thing I can think of doing is using object as the generic type, but this fails at run time:

var attr = (MyTypedAttribute<object>?)myProp.GetCustomAttribute(typeof(MyTypedAttribute<>))

Runtime error: Unable to cast object of type 'MyNumberAttribute' to type 'MyTypedAttribute`1[System.Object]'.'

Essentially what I'm trying to do is get the attributes and call the ProcessValue method, capturing the returned value. Is there any way to do this?





mercredi 14 juin 2023

True condition evaluates to false

I am writing a basic model validator that uses reflection with a custom "Required" attribute. When a model is passed into the validator method, all properties are checked for this required attribute. Complex types are recursively validated as well. When a property is found with this attribute, it will either compare the value to null if it is a nullable type, else it will compare to default.

I'm having issues when comparing non-nullable type to default. The following condition is evaluating to false for type int, but it should be true:

if (prop.GetValue(testObject) == default)
{
    Console.WriteLine($"{VALIDATION_FAILED_MESSAGE}: {baseObjectName}{testObject.GetType().Name}.{prop.Name} is null.");
    isNullorDefault = true;
}

I see in the debugger that the value being compared is without a doubt the default value -> default(int) == 0, but it is evaluating to false. Why?

enter image description here





Get companion object (declared in an inner class) of generic type without instance

I am working on a method which is given only one piece of information: a type that extends ClassTag (this can be changed to TypeTag if strong guarantees are necessary for resolving certain parts of the type). We are not given an instance such that we can use scala.reflect.runtime.currentMirror to obtain a InstanceMirror from the Scala libraries available. We are not given the arguments necessary to construct such an instance. I am only able to obtain a RuntimeClass of this generic type. Scala has the concept of a companion object. I want to check if the companion object of the given generic class T extends a specific trait A, cast that object as that trait, and invoke the method which is overriden by that companion object. The current code to do what I described is below:

def invokeOverridenMethodOfCompanionObject[T : ClassTag]() {
  import scala.reflect.runtime.currentMirror
  import scala.reflect.runtime.universe.NoSymbol

  val runtimeClass = scala.reflect.classTag[T].runtimeClass
  val companion = currentMirror.classSymbol(runtimeClass).companion

  companion match {
    case NoSymbol => // do nothing
    case _ => 
      // The problem is this line
      currentMirror.reflectModule(companion.asModule).instance match {
        case s: A => s.invokeOverridenMethod
        case _ => // do nothing 
      }
  }
}

This approach is almost perfect. The problem is that if the object is not top level i.e. declared within another object or class, this method no longer works. Scala then complains and spits out this error:

object <objectName> is an inner module, use reflectModule on an InstanceMirror to obtain its ModuleMirror

The problem is I am not able to obtain the instance mirror of the symbol/type which owns the companion object. I cannot construct that instance either since I have no arguments to build a dummy constructor either. I also cannot guarantee the existence of a nullary constructor either, so gimmicks using them are off limits as well. Is there a way to accomplish my goal given these constraints?

Edit: I am aware that Java Class can allow a person to search up the method by name and then invoke it. I know this is possible but my solution would require passing around the object later as well. Therefore, it is a must for me to obtain a variable reference directly to the object. You may assume that after the code block above ends, I return the companion object constructed.





How to obtain a private Lookup?

I've got some piece of code, that is supposed to work with Java 8+. The larger surrounding project is heavily based on Reflection, and should be made ready for Java 9+.

What I am doing now, is this: If I can obtain a private Lookup via MethodHandles.lookup().privateLookupIn(Class), then I am using that private Lookup instead of Reflection. That should be safe for current Java versions, at least to 17, without warnings.

That works, indeed, with one exception: I am getting a warning, when attempting to obtain the private Lookup itself. In other words, the code below emits a warning:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access using Lookup on lookup.demo.LookupDemo (file:/C:/Work/ews/4.28/LookupDemo/bin/) to class java.util.HashMap
WARNING: Please consider reporting this to the maintainers of lookup.demo.LookupDemo
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

In my opinion, the Lookup returned by MethodHandles.publicLookup() should be able with a public class (MethodHandles), thus giving permission to the public method MethodHandles.privateLookupIn?

Thanks,

Jochen

public class LookupDemo {
    public static void main(String[] pArgs) throws Exception {
        System.out.println("Lookup = " +
                           getPrivateLookup(HashMap.class));
    }

    // Note: The following method can still be compiled with Java 8,
    // although it produces a different result.
    private static Lookup getPrivateLookup(Class<?> pType) {
        try {
            // This is supposed to work fine on Java 9+
            final Method method = MethodHandles.class.getDeclaredMethod("privateLookupIn", Class.class, Lookup.class);
            final MethodHandle mh = MethodHandles.publicLookup().unreflect(method);
            return (Lookup) mh.invoke(pType, MethodHandles.lookup());
        } catch (Throwable t) {
            // We are running on Java 8.
            return null;
        }
    }
}




dimanche 11 juin 2023

How to assign a interface{} to another interface{} whose concrete type is SameStructType?

type SameStructType struct{
    Test string
}

func A() interface{} {
    return SameStructType{
        Test:"123",
    }
}
func B() interface{} {
    return &SameStructType{}
}

a:=A()
b:=B()

// *b=a //I want to make b assigned by a.

If I know the actual type, I can use the code below:

a := A().(SameStructType)
b := B().(*SameStructType)
*b = a

The question is I don't know their actual type,but I know they have the same type.

Is there an easy way instead of using "reflect"? I don't like parsing the struct "field by field" using reflect because it is slow.

If I have to use reflect,is there an easy way? I am sure that they have the same type,so is there a fast way to copy memory like C/C++?





vendredi 9 juin 2023

Use reflection to name methods in a generic trait in Scala

Suppose I have a generic trait that provides a way to get a value of some type (think perhaps fixtures in a test suite).

trait Fixture[A] {
    def value: A
}

Is there any way to use reflection to create a method within this trait that has the same name of the type in a concrete implementation?

So that when I can do:

class BarFoo extends Fixture[Foo] {
    override def foo: Foo = // my concrete code here
}

class BarQux extends Fixture[Qux] {
    override def qux: Qux = // create a Qux value here
}

or even worse, maybe something like:

class BarFooQux extends Fixture[Foo] with Fixture[Qux] {
    override def foo: Foo = // ...
    override def qux: Qux = // ...
}




Trouble with groovy/java, classpaths, and reflection/dynamic method access

TL;DR: I am having trouble with classpaths and a java/groovy execution environment. Java is openjdk version "17.0.7" 2023-04-18. Groovy is groovy-4.0.12.jar.

The Background

I have Java program that loads groovy scripts and runs them. The script can access a number of resource in the parent via an ExecutionContext class that is setup upon initialization. The design uses a base class to standardize the interface, leaving task-specific codes to the subclass. A quick example:

BaseClass.1.groovy:

import org.bson.*;
class BaseClass {
    ExecutionContext exe; 

    public void setContext(ExecutionContext executingEnvironment) {
        this.exe = executingEnvironment
    }
              
    Document fetchSection(String name) {
    return exe.fetchSection(name);
    }
}

MyClass.groovy:
import BaseClass;

import org.bson.*;

class MyClass extends BaseClass {
    void do_something(Document inputs) {
    Document s1 = fetchSection("S1"); // use BaseClass method
...
     }    
}

ExecutionContext is defined in a simple class and placed into a jar ExecutionContext.jar:

import org.bson.*;

public interface ExecutionContext {
    Document fetchSection(String sectionName);
}

The parent program contains a concrete implementation of ExecutionContext:

    class ExecutionContextImpl implements ExecutionContext {
    public Document fetchSection(String sectionName) {
        // Actual work here...
    }
    }

The parent first loads the BaseClass, then MyClass, then creates an instance of MyClass, then creates an instance of ExecutionContext and using reflection sets it into MyClass:

GroovyClassLoader cls = new GroovyClassLoader();

String fwscript = get BaseClass.1.groovy from somewhere;
String myscript = get MyClass.groovy from somewhere;

Class fwclazz = cls.parseClass(fwscript);
Class clazz = cls.parseClass(myscript);
Object oo = clazz.newInstance(); 

ExecutionContext qqq = new ExecutionContextImpl();

Class[] argTypes = new Class[1];
argTypes[0] = ExecutionContext.class;
java.lang.reflect.Method m = fwclazz.getDeclaredMethod("setContext", argTypes);
Object[] margs = new Object[1];
margs[0] = qqq;
Object oo2 = m.invoke(oo, margs);

This works, and later use of dynamic reflection to invoke methods such as do_something also works:

Class[] argTypes = new Class[1];
argTypes[0] = Document.class;
java.lang.reflect.Method m = clazz.getDeclaredMethod( "do_something", argTypes);
Object[] margs = new Object[1];
margs[0] = new Document("adj", 6.3);
Object oo2 = m.invoke(oo, margs);

The Problem

I wish to isolate the groovy classpath environment from the parent. This can be done as follows; note the null in arg #2 to prevent the new classloader from inheriting the environment from the parent program:

URL[] groovyClasspath = new URL[] {
    new File("/path/to/ExecutionContext.jar").toURI().toURL(),
    new File("/path/to/groovy-4.0.12.jar").toURI().toURL(),
    new File("/path/to/guava-17.0.jar").toURI().toURL(),
    new File("/path/to/bson-4.9.0.jar").toURI().toURL()
};
URLClassLoader cpl = new URLClassLoader(groovyClasspath, null);

This almost works. The scripts parse and in particular, resources referenced in the scripts like imports and bson and Table objects are resolved. Commenting out the URL in the classpath produces an expected unable to resolve class runtime exception so it is definitely finding the intended codes. The problem is that with an explicit classpath, the groovy environment cannot seem to find methods in the loaded groovy scripts. This call:

java.lang.reflect.Method m = fwclazz.getDeclaredMethod("setContext", argTypes);

throws an exception java.lang.NoSuchMethodException: BaseClass.setContext(ExecutionContext). Any call to getDeclaredMethod for either the base class fwclazz or the subclass MyClass has this problem. And what is weird is that if I list the methods with something like this:

for (Method method : fwclazz.getDeclaredMethods()) {
   System.out.println("  " + method.toString());
}

Then I clearly see public void BaseClass.setContext(ExecutionContext):

  public groovy.lang.MetaClass BaseClass.getMetaClass()
  public void BaseClass.setMetaClass(groovy.lang.MetaClass)
  protected groovy.lang.MetaClass BaseClass.$getStaticMetaClass()
  public org.bson.Document BaseClass.fetchSection(java.lang.String)
  public void BaseClass.setContext(ExecutionContext)
  public static java.lang.invoke.MethodHandles$Lookup BaseClass.$getLookup()
  public ExecutionContext BaseClass.getExe()
  public void BaseClass.setExe(ExecutionContext)

I am sure there is a very simple addition to the GroovyClassLoader URL list to make this work. Any clues?





jeudi 8 juin 2023

Which method is equivalent to the instanceof operator in Java?

I need to check if an object is instance of a class that is get at runtime by it's name. I can't use Class.isInstance, because I can't create an instance of the class

Class.isAssignableFrom always returns false, this is the code that I am using:

if (pm.getClass().isAssignableFrom(Class.forName("java.lang.reflect.Proxy"))) { ...

Using pm instanceof Proxy true is returned.





Instantiating unknown generic type with unknown generic parameter

I am trying to build a plugin engine. The only requirement is that a plugin must implement two types: ISettings and IPlugin from the contract.cs below. A basic implementation of a plugin can be found in plugin.cs.

The main application will scan for assemblies implementing these types, creates instances of them and calls the standard Start() method.

It works fine, but I am missing any compile time check because I cannot correctly cast the instances to the desired types in main.cs (I know nothing about plugin.cs in the main app), that's why I am using dynamic.

The main reason for using a generic type is to give access to implementor of plugin.cs to his TSettings type. I could use IPlugin<ISettings> instead but this will oblige the implementor to write Start(ISettings settings) which makes TSettings unusable without a cast.

Another approach was to follow the official IOptions<TSettings> pattern, but the settings object cannot be injected into constructor because settings are built or modified after object creation.

I there any better way to design the contract or to obtain a strong typed reference in main.cs instead of dynamic?

Please note that this is a stripped down example to illustrate the principle, in reality the plugin interface is more complex, TSettings is used in more places and I am using dependency injection instead of the classic Activator.

contract.cs

interface ISettings {}

interface IPlugin<TSettings> where TSettings: ISettings
{
  void Start(TSettings settings);
}

plugin.cs

class SomeSettings: ISettings {}
class SomePlugin<SomeSettings> : IPlugin<SomeSettings>
{
  void Start(SomeSettings settings) { }
}

main.cs

void Load()
{
  Type settingsType = ... // extract settings type from plugin.dll
  Type pluginType = ... // extract plugin type from plugin.dll

  dynamic settings = Activator.CreateInstance(settingsType);
  //all I know is that settings can be cast to ISettings
  dynamic plugin = Activator.CreateInstance(pluginType)
  //all I know is that plugin can be cast to IPlugin<UnknownSettings>

  plugin.Start(settings)
  //it works but plugin being dynamic, I can write anything here.
}








mercredi 7 juin 2023

Receiving a class implementation of a interface as a method argument

Is it possible to create a method who receives a Class implementation of an interface, but using the interface as argument and avoiding to receive the interface class.

I want to create an instance (and control it) of the interface implementation via reflection, so I need a valid class, not an interface class

for example:

interface MyInterface {}

class MyClass implements MyInteface{}


void myMethod(Class<? extends MyInterface> clazz){
    Constructor<MyInteface> constructor = (Constructor<MyInteface>) clazz.getConstructor();          
    MyInteface handler = constructor.newInstance();
}

This way works, but it also accept MyInterface.class as an argument, and I can't instantiate an interface.

myMethod(MyClass.class); // it's ok, what I want
myMethod(MyInterface.class); // this won't work - I want to avoid this case of use




How can I tell when my code has been reflected?

I am a newer in Java, I know anyone can use reflection to get private methods and private data, but how can I konw whether my code being reflected? Or, I don't want to my code be reflected, is there any methods to prevent it?

I don't know how to do





mardi 6 juin 2023

How to access to interface of a pointered function

For the validator I have the following interface

type validatorInterface interface {
    IsValid() error
}

func customValidation(fl validator.FieldLevel) bool {
    field := fl.Field().Interface()
    validatorObject, ok := field.(validatorInterface)
    if !ok {
        return false
    }
    err := validatorObject.IsValid()
    if err != nil {
        return false
    }

    return true
}

It work perfect if the field to validate has the function implemented without pointer. Like that:

func (s s) IsValid()

but the conversion to the interface fails if it is implemented like that

func (s *s) IsValid()

How can I improve the customValidation to solve the problem





lundi 5 juin 2023

Field.class.getDeclaredFields() is returning an empty array [duplicate]

When trying to get the modifiers field from the Field class reflectively, I get a NoSuchFieldException. And when printing out a list of the declared fields I get an empty array.

 try {
        modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
 } catch (NoSuchFieldException e) {
        throw new RuntimeException(e + Arrays.toString(Arrays.stream(Field.class.getDeclaredFields()).map(Field::getName).toArray()));
 }

Output: java.lang.RuntimeException: java.lang.NoSuchFieldException: modifiers[]

Tried updating Java, Tried doing it in Kotlin. To no avail.





Golang import package on runtime

I have two base github.com packages: github.com/xxx/package1 and github.com/xxx/package2

In the package1, I create a core folder and define a struct with a function:

package core

import (
    "fmt"
    "go/importer"
)

type Test struct {}

func (navi *Test) Run() {
    pkg, err := importer.Default().Import("github.com/xxx/package2/core")

    if err != nil {
        fmt.Print(err)
    }

    return
}

In the package2, I also create a core folder and a struct with a function:

package core

import "fmt"

type Hello struct {

}

func (hello *Hello) Test() {
    fmt.Print("Test\n")
}

I create a main.go file in the package2 with code:

package main

import (
    "fmt"
    "github.com/xxx/package1/core"
)

func main() {
    test := &core.Test {}

    test.Run()
}

I pushed both two this packages to the github.

On the package2, I use the go get command to download two above packages, then execute command "go run main.go"

However, I get the message can't find import: "github.com/xxx/package2/core"

If I use the import statement and import directly the github.com/xxx/package2/core, I can use the struct Hello under this package.

How can I resolve this error?





In this case, will C# use reflection or casting?

In c#, if I write code like this

dynamic target = "abcde";
dynamic arg = "c";
Console.WriteLine(target.Contains(arg));

Will C# use reflection to get the method "Contains," or cast dynamic as a string automatically and call the method "Contains"?





vendredi 2 juin 2023

Entity Framework and C# Assembly class reflection

I'm trying to figure out to store .NET objects in SqlServer to minimize binding effort. Example:

public DbSet<Assembly> Assemblies { get; set; }

The code above results in the following exception:

'The entity type 'Assembly' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types

Is there a way to consider the name of the Assembly primary key?