mercredi 30 mars 2022

How can I get class object value if given a variable in the form of a string

I have a class

@lombok
class a {
private String status;
}

and I have a method that accepts a string value

public string getValue (String input, Class a) {
//  Let's say input value is status
   return a.getStatus();
}
````
How can I return a.getStatus()?

I'm not able to figure out a way to map these values with and without using reflection.

I can change status to getStatus as well in the input if it helps.




Get raw value of property attribute using Reflection

I need to mark some properties as passwords so that they could be automatically screened. I found a standard attribute for that:

[PasswordPropertyText]
public string ThePassword { get; set; }

Following method checks if the attribute is there:

private static bool _isPassword(PropertyInfo p)
{
    PasswordPropertyTextAttribute passProp = (PasswordPropertyTextAttribute)p.GetCustomAttribute(typeof(PasswordPropertyTextAttribute));
    return (passProp != null); // Additional condition should go here
}

Now I would like to have my own logic here:

  • [PasswordPropertyText] should result in true.
  • [PasswordPropertyText(true)] should result in true.
  • [PasswordPropertyText(false)] should result in false.

but the default value of PasswordPropertyTextAttribute.Password is false when the argument is omitted.

Is there any way to get the raw attribute value?





How can I iterate through a list of strings and call them on a dbo?

I'm a student so sorry if this is really stupid. I have a model called Person which is IEnumerable. Person holds a large amount of attributes such as height weight, eyecolor etc. I can get the properties of Person using

(from t in typeof(Person).GetProperties() select t.Name)

which results in a list. I need to then insert all the Person objects into a 2d array is there a way with a double for loop I can use the list to access the dbo instead of calling every property as hard code?

I was thinking something like

foreach (var (item, i) in properties.Select((item, i ) => (item, i)))
{
    array[0][i] = properties[i];
    foreach (var(person, j) in people.Select((person, j)=>(person, j)))
    {
    //this is the part I'm struggling with
    array[j][i] = People.property[i];//???
    }
}




Java: runtime reflection or type introspection?

Is the following code considered to be runtime reflection or is it type introspection?

Class c = java.util.ArrayList.class;
String className = c.getName();

I want to use this in the compilation phase, and do not want to use any resources (including time) in runtime. Does it use any runtime resource?





Java: Runtime reflection in compilation phase (?!)

In Element#getAnnotation(Class<A> annotationType) javadoc it is stated that

Note: This method is unlike others in this and related interfaces. It operates on runtime reflective information — representations of annotation types currently loaded into the VM — rather than on the representations defined by and used throughout these interfaces. Consequently, calling methods on the returned annotation object can throw many of the exceptions that can be thrown when calling methods on an annotation object returned by core reflection. This method is intended for callers that are written to operate on a known, fixed set of annotation types.

Yet, this method is frequently used in annotation processing which is part of the compilation phase. What I want to understand is what, how, and why things gets loaded to VM at compilation time, and what are the pros and cons.

For example, in the case of Element#getAnnotation(Class<A> annotationType), is there any drawbacks (except possibly not being able to access values of type class in the annotation), compared to if we get the same information using mirrors (which is usually the longer code)?





Trouble creating a List of properties of a certain type from a class using reflection

I have a class with (at the present time) 3 Azure CloudTable properties. I am trying to create a private method to return a list of properties of type CloudTable as there are times where I need to grab commission and fee information from all of them

Main Class

public class EquityAndOptionDataRetrievalManager
{
    private readonly IAzureStorageService _azureStorageService;

    public EquityAndOptionDataRetrievalManager(IAzureStorageService azureStorageService)
    {
        _azureStorageService = azureStorageService;
    }

    // properties

    public CloudTable EquityTradeRecordsTable => AzureHelpers.GetCloudTable(_azureStorageService.PrimaryConnectionString, Common.StorageTable.EquityTradeRecordsTable);

    public CloudTable OptionTradeRecordsTable => AzureHelpers.GetCloudTable(_azureStorageService.PrimaryConnectionString, Common.StorageTable.OptionTradeRecordsTable);

    public CloudTable SymbolsTradedTable => AzureHelpers.GetCloudTable(_azureStorageService.PrimaryConnectionString, Common.StorageTable.InteractiveSymbolsTraded);

...rest removed for brevity

and I am trying to do something like this

private List<CloudTable> AzureTables()
{
    var tables = new List<CloudTable>();
    var obj = new EquityAndOptionDataRetrievalManager(_azureStorageService);
    var properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
    
    foreach (var p in properties)
    {
        if (p.GetType() == typeof(CloudTable))
        {
            tables.Add(p.GetValue(obj,null) as CloudTable);
        }
    }

    return tables;
}

but in the tables.Add(p) and using p. to see all the available properties and methods, I cannot find anything that works. Is this not doable?

Note: Per MakePeaceGreatAgain's comments

I added a property to test the method in the class

public List AzureTableList { get; set; }

and I'm calling, just to test it It is failing, because the if inside the foreach loop never captures the CloudTable properties, even when they actually are CloudTable properties!

It's the right type, but it exits the if statement

enter image description here





Reflection - getting private field value

I am testing viewmodel and would like to access private field:

    val currentTrainingField = viewModel.javaClass.getDeclaredField("currentTraining")
    currentTrainingField.isAccessible = true
    val currentTraining = currentTrainingField.get(currentSetField)

I receive an error: Can not set com.myapp.Training field com.myapp.WorkoutExerciseViewModel.currentTraining to java.lang.reflect.Field

How should I handle that?