lundi 17 février 2020

How do I get a reflection on a property's property in Kotlin?

say we have a class with a property subClass.

class TestClass(val property: SubClass)

The SubClass has its own property

class SubClass(val subProperty: Any?)

When I only have access to the class itself, I know that I can easily get a reflection to the property via

val reflection = TestClass::property

However, how do I get access to the subProperty via reflection? The following does not work:

val subReflection = TestClass::property::subProperty

I'd apprectiate any insight on that! Thanks!





Why does the java compiler creates a synthetic constructor in outer class?

I can't understand oracle java tutorial that writes:

public class SyntheticConstructor {
    private SyntheticConstructor() {}
    class Inner {
    // Compiler will generate a synthetic constructor since
    // SyntheticConstructor() is private.
    Inner() { new SyntheticConstructor(); }
    }
}

Since the inner class's constructor references the private constructor of the enclosing class, the compiler must generate a package-private constructor.





C# recursively check all values not null [duplicate]

Without wanting to reinvent the wheel, is there a dotnet nuget library to perform checks on a object recursively for argument checking?

If not, how would I convert the code to check if a property is null, and if a type that can hold properties of its own, recursively check that type?

public static class Assert
{
    public static void AllPropertiesNotNull<T>(T obj)
    {
        if (obj == null)
            throw new ArgumentNullException(nameof(obj));

        var emptyProperties = typeof(T)
                                .GetProperties()
                                .Select(prop => new { Prop = prop, Val = prop.GetValue(obj, null) })
                                .Where(val => IsEmpty((dynamic)val.Val))
                                .Select(val => val.Prop.Name)
                                .ToList();

        if (emptyProperties.Count > 0)
            throw new ArgumentNullException(emptyProperties.First());
    }

    private static bool IsEmpty(object o) { return o == null; }
}




C# properties obtained via reflection: check if it has a default value of its type?

I am iterating an object using reflection like so:

foreach (PropertyInfo property in properties)
   {
    object prop = property.GetValue(myObjectInstance);
     // TODO: I need to check if prop carries a default value for its respective type
   }

How I can check if the prop is equal to the type's default value (for instance, if the property is of DateTime, then check for default(DateTime))?





PHP check "variable variables" isset

For normal variables I can use isset($var) to check if it is set.

How to check if a variable variable (a variable that have the name stored in another variable) is set?





C# get unreferenced assemblies

The problem

I'm working on a project with a plugin like system. The project structure is defined as follows:

- Solution
    - Project A (Cli)
    - Project B (Core logic)
    - Project C (Plugin)

Project A is the startup project, Project B contains the core logic and Project C is a plugin, which implements an interface in project B. Project A only has a reference to Project B and Project C only has a reference to Project B.

To visualize:
visualization

Since I do not reference Project C from Project B (and do not instantiate the class directly), code like AppDomain.CurrentDomain.GetAssemblies() (inside Project B) does not return Project C.

However, once I do (from Project B): var MyImplementation = new MyImplementation(), AppDomain.CurrentDomain.GetAssemblies() does return the assembly, since it's known and loaded at runtime as I instantiated the class MyImplementation explicitly from code.

The question

From Project B, I want to get all assemblies which implement a specific interface from Project B and instantiate them.

So, how do I get these (seemingly) unknown assemblies to get loaded at runtime?





How to set a class Name as a method's parameter?

I would like to send a class name as a parameter of method. I would like to use(execute) it like that.

 orderDao.getTheMostFrequentType(EmployeeOrder.class);

I have this code

@Override
        public String getTheMostFrequentType(Class<T> orderClass){


          String tableName = orderClass.getAnnotation(Table.class).name();

          String sqlQuery = "SELECT STATS_MODE(signed_by_id) FROM " + tableName;

          String result = (String)getSessionFactory().getCurrentSession().createSQLQuery(sqlQuery).uniqueResult();

        return result;
      }

But it executing attempts gives me:

May be there is another classes should i import instead of apache.poi. But which one?

getTheMostFrequentType("org.apache.poi.ss.formula.functions.T") method cannot be aplied in "java.lang.entities.EmployeeOrder"

What i do wrong?