Dynamically add a nested T class and fill it with another T class (GetProperty, GetType, SetValue) - setvalue

I just want to pass a class T as a parameter. Example:
from:
SetValue (Car.parts, "engine");
to:
SetValue (T.partes, "engine");
simply pass your nested class of a generic as a parameter.
Thank you very much for the help.
HERE THE DETAILED.
but I just need the top one.
Good day, I would like to dynamically fill a class T with some values.
here my example:
This class is familiar.
Class Model model = new ClassModel ();
has a nested class
model.autos = new ClassModel.Auto ();
Write ClassModel = typeof (ClassModel);
var AutoClass = ModelClass.GetProperty ("autos");
var AutoProp = Type.GetType (ClaseAuto.GetMethod.ReturnType.FullName) .GetProperties ();
AutoProp [0] .SetValue (model.autos, 1);
// here this works and passes the parameter, but if it were a class T
How would the value for cars pass?
example:
AutoProp [0] .SetValue (T.autos, 1);
where cars are unknown because the class is T.
and at the same time fill the class T which is easier to identify and fill
since the first T corresponds to a class.
ClassModel.SetValue (T, AutoProp);
after this to corroborate the success of the variable is passed to a
variable when the process ends
/ * Once my class is recovered, I already know all its methods, but inside class T I do not know the methods, but I send the class cars by name = "autos" and so I get to know it but I still do not know how to pass on the values . * /
int result = model.autos.id;
Thank you very much for the help provided.

Simply the class T you get the nested class, then if you want more all its values ​​as a normal class add GetValue (T) and ready to pass the class as if it were a normal class.
the class tests the one daughter class of T
SetValue(T.GetType().GetProperty("proof").GetValue(T) , 1);

Related

Javaparser AST pattern match

I need to do some operations on the AST produced by the java parser. My problem is I want to check a class initialization cycle problem is there or not.
One example is,
class mark1 {
public static final int x = mark2.p * 5;
//Do some operations here
}
class mark2 {
public static final int p = mark1.x + 100;
//Do some operations here
}
The initialization order of the classes can vary, causing computation of different values for mark1.x and mark2.p. I am trying to implement it using javaparser produced AST but didn't get a feasible solution.
With JavaParser you can easily get all the static fields and the static initializers.
The problem I see with this is that you need to resolve references. For example you need to understand that "mark2.p" and "mark1.x" refer to static fields of other classes. From the point of view of the ASTs they are field accesses, but the AST and JavaParser alone cannot tell you that that particular field is static. To do so you need to use a symbol solver like https://github.com/ftomassetti/java-symbol-solver/ or you can build the logic yourself. For example you could need to look at the imports and see if the class mark1 has been imported or if one class named mark1 is present in the same package as mark2. Doing that you could recognize that mark1 is the name of a class and look into that class for the symbol p. You could then find it and notice it is a static field.
Source: I am a JavaParser contributor

Create a Class with full name and pass it to the custom component

I have class name : some.path.exampleclass and have to instantiate class (like getDefinitionByName()). Problem is that getDefinitionByName() makes class exampleclass and i need class with FULL name : some.path.exampleclass to pass it to custom component :
<tools:MyComp value="some.path.exampleclass"/>
Is there a quick solution for this? :)
If you're intent is to create an instance of an FXG element based on a string that represents it's fully qualified class name, you can do it like this:
var classNameString :String = // your code to get the classname;
var class :Class = getDefinitionByName( className ) as Class;
var newObject : MyObject = new class() as MyObject;
This should work perfectly fine with FXG Elements. I use it in my mobile game.
You mention that:
Setter method...need to be set with full path class
not only with one that getDefinitionByName gives me
getDefinitionByName returns an instance of Class. If your method is expecting a string that is why getDefinitionByName doesn't work.
For more specifics, if needed, you should provide the full source code for the method or property.

How to access a field's value via reflection (Scala 2.8)

Consider the following code:
class Foo(var name: String = "bar")
Now i try to get the value and the correct type of it via reflection:
val foo = new Foo
val field = foo.getClass.getDeclaredField("name")
field.setAccessible(true)
//This is where it doesn't work
val value = field.get(????)
I tried things like field.get(foo), but that just returns an java.lang.Object but no String. Basically I need the correct type, because I want to invoke a method on it (e. g. toCharArray).
What is the suggested way to do that?
As others have mentioned, the reflection methods return Object so you have to cast. You may be better using the method that the Scala compiler creates for field access rather than having to change the visibility of the private field. (I'm not even sure if the name private field is guaranteed to be the same as that of the accessor methods.)
val foo = new Foo
val method = foo.getClass.getDeclaredMethod("name")
val value = method.get(foo).asInstanceOf[String]
getDeclaredField is a method of java.lang.Class.
You have to change foo.getDeclaredField("name") to foo.getClass.getDeclaredField("name") (or classOf[Foo].getDeclaredField("name")) to get the field.
You can get the type with getType method in class Field but it won't help you because it returns Class[_]. Given than you know that the type is a String you can always cast the value returned using field.get(foo).asInstanceOf[String]
AFAIK, reflection always work with Object, and you have to cast the results yourself.
This is how one can get list of fieldnames and its value of a case class:
First, using reflection, get fields info as follows -
val TUPLE2_OF_FIELDNAME_TO_GETTERS = typeOf[<CLASS>].members
.filter(!_.isMethod)
.map(x => (x.name.toString, classOf[<CLASS>].getDeclaredMethod(x.name.toString.trim)))
How to use it?
getFieldNameAndValue(obj: <CLASS>): Seq[(String, String)] {
var output = Seq[(String, String)]()
for(fieldToGetter <- TUPLE2_OF_FIELDNAME_TO_GETTERS) {
val fieldNameAsString = fieldToGetter._1
val getter = fieldToGetter._2
val fieldValue = getter.invoke(obj).toString
output += (fieldName, fieldValue)
}
}
foo.getClass.getDeclaredField("name").getString(foo)
should work if you want to avoid asInstanceOf. get* is available for various types

ActionScript 3 constructor scope question

out of curiosity I decided to experiment with the following in a Flex 4 project:
public class MyGroup extends Group
{
public function MyGroup()
{
super();
var myLabel:Label = new Label();
myLabel.id = "myLabel";
myLabel.text = "My label!";
this.addElement(myLabel);
} etc.
This custom component does what I'd expect; it looks like a label control with text="My label!".
Question: is there any way to reference the myLabel label control (e.g. to change the text) elsewhere in the project?
At the moment the only way I can get to the inner label control is by calling something like myGroup.getElementAt(0).
I realize that it would make more sense to have the label be a class variable -- I'm just wondering how this code works.
You can make a public setter to change you text label:
public class MyGroup extends Group
{
private var _label:Label=new Label();
public function set label(value:String):void{
_label.text=value;
}
public function MyGroup()
{
super();
_label.id = "myLabel";
label = "My label!";
addElement(_label);
}
.....
}
var myGroup:MyGroup=..
myGroup.label="Hello";
In your case since you are declaring your var myLabel inside a function, it 's scope will only apply inside this function
In ActionScript, variables are named handles that you can pull on to get objects and data. Variables have something called scope, which specifies in what parts of the code the handle is valid.
When you create a variable inside a function, its scope is that function. That is, that particular named handle is only usable inside that function.
In your code, you create a handle called myLabel, and put a Label in it--let's call it Label123. Next you put Label123 into the element list of MyGroup, which gives MyGroup a handle attached to Label123. Then the function ends, and the handle called myLabel is no longer usable. Label123 still exists, because MyGroup has a handle to it.
If you create myLabel as a private or protected class variable, that handle will be usable from any function inside MyGroup. If you create it as a public class variable, it will be usable anywhere inside MyGroup, and also anywhere in the code you have an instance of MyGroup. (And if you create it as an internal class variable, it will be usable anywhere inside the package MyGroup is in.)

How do I cast one Flex object as a derived type?

Here's the example:
var cartesian:CartesianChart = new CartesianChart();
cartesian.width = 100;
var column:ColumnChart = new ColumnChart();
column = cartesian as ColumnChart;
Why does this not work? "column" ends up null. ColumnChart is a derived class of CartesianChart, so I would have thought I'd end up with a ColumnChart with a width of 100.
ColumnChart extends CartesianChart? I don't believe you can cast an object from a parent class to an object of it's child class. Your best bet is to write a class function on ColumnChart called something like copyFromCartesianChart that takes the CartesianChart object as a param and gets and sets all the props you need.

Resources