Get All Variable Names in a Class

Get all variable names in a class

Field[] fields = YourClassName.class.getFields();

returns an array of all public variables of the class.

getFields() return the fields in the whole class-heirarcy. If you want to have the fields defined only in the class in question, and not its superclasses, use getDeclaredFields(), and filter the public ones with the following Modifier approach:

Modifier.isPublic(field.getModifiers());

The YourClassName.class literal actually represents an object of type java.lang.Class. Check its docs for more interesting reflection methods.

The Field class above is java.lang.reflect.Field. You may take a look at the whole java.lang.reflect package.

Get all variable names in methods

You are passing FieldDeclaration.class into CompilationUnit's findAll() method. So, as asked, it gets you all declared fields.

If you want to list all declared variables, use VariableDeclarator.class from the same package – it will get you all of those, including the ones declared as fields.

How to list all Variables of Class

Your question isn't perfectly clear. It sounds like you want the values of the fields for a given instance of your class:

var fieldValues = foo.GetType()
.GetFields()
.Select(field => field.GetValue(foo))
.ToList();

Note that fieldValues is List<object>. Here, foo is an existing instance of your class.

If you want public and non-public fields, you need to change the binding flags via

var bindingFlags = BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public;
var fieldValues = foo.GetType()
.GetFields(bindingFlags)
.Select(field => field.GetValue(foo))
.ToList();

If you merely want the names:

var fieldNames = typeof(Foo).GetFields()
.Select(field => field.Name)
.ToList();

Here, Foo is the name of your class.

Is there a way to get to all variables of a certain type in a class?

Your code "compiles" to this:

var MyClass = (function () {
function MyClass() {
}
return MyClass;
}());

with nothing in there as you can see... however if you initialize your properties it will result some like this:

source TS:

class MyClass {
myNum1: number = 0;
myNum2: number = 0;
myNum3: number = 0;
myString: string = "";
myBoolean: boolean = false;
}

result JS:

var MyClass = (function () {
function MyClass() {
this.myNum1 = 0;
this.myNum2 = 0;
this.myNum3 = 0;
this.myString = "";
this.myBoolean = false;
}
return MyClass;
}());

then you can check instance properties:

var instance = new MyClass();
Object.keys(instance) //["myNum1", "myNum2", "myNum3", "myString", "myBoolean"]
instance["myNum1"] // 0

with that in mind you can filter the properties that you need:

var numerics = Object.keys(instance).map(k => instance[k]).filter(v => v.constructor === Number)
console.log(numerics) //[0, 0, 0]

How to get all values of variables in class?

There are two ways. This is a straight answer to your question:

class Foo:
pass

class Bar:
x: int = 1
y: str = 'hello'
z: Foo = Foo()

@classmethod
def get_all(cls):
xs = []
for name, value in vars(cls).items():
if not (name.startswith('__') or isinstance(value, classmethod)):
xs.append(value)
return xs

This is what I suggest:

from dataclasses import dataclass, fields

class Foo:
pass

@dataclass
class Bar:
x: int = 1
y: str = 'hello'
z: Foo = Foo()

@classmethod
def get_defaults(cls):
return [f.default for f in fields(cls)]

@classmethod
def get_all(cls):
return [getattr(cls, f.name) for f in fields(cls)]

results:

Bar.get_defaults() == Bar.get_all()
# True -> [1, 'hello', __main__.Foo]

Bar.x = 10
Bar.get_defaults() == Bar.get_all()
# False -> [1, 'hello', __main__.Foo] != [10, 'hello', __main__.Foo]

How to get all variables of a class in angular

I found what im was looking for. The solution is create a ItemController, thanks everyone!

export class Item {
item_id: number;
item_quantidade: number;
item_unidade_medida: number;
item_descricao: string;
item_codigo: string;
item_marca: string;
item_fornecedor: string;
item_unitario_compra: number;
item_unitario_venda: number;
item_parte_caminhao: string;
item_aplicacao: string;
item_usu_cadastro: string;
item_usu_data_cadastro: Date;
item_usu_alteracao: string;
item_usu_data_alteracao: Date;
item_status: string;
}

export class ItemController {
_item: Item;

constructor() {
this._item = new Item();
this.resetItem();
}

public get getItem(): Item {
return this._item;
}
public setItem(item: Item) {
this._item = item;
}

}


Related Topics



Leave a reply



Submit