Invalid constructor name - dart

bbeginner question here:
I have a class like this
class Data {
String name;
String imgUrl;
Data(this.name, this.imgUrl);
}
and I'm trying to create a list like this
var list = new List<Data>();
var data = new Data("caca", "toto");
list.add(data);
But I get an error saying invalid constructor name.
What I'm I doing wrong here?
Thanks for your help

My guess is that your code is not inside a function. Something like:
class Something {
var list = new List<Data>();
var data = new Data("caca", "toto");
list.add(data);
}
Maybe it's due to one-too-many end braces just above the code, or some other syntactic typo.
Ensure that your code is actually inside the body of a function, not just directly inside the class.
class Something {
void tutu() {
...
var list = new List<Data>();
var data = new Data("caca", "toto");
list.add(data);
... use list ...
}
}

Related

frida modify static param as ArrayList

public class OBDSportsModelManager {
public static ArrayList<DataArray> mDiagnosisCommand;
public boolean getData() {
mDiagnosisCommand = new ArrayList<>();
for (String dataArray : this.commandIDs) {
mDiagnosisCommand.add(new DataArray(dataArray));
}
return true;
}
}
I want to add some more item to the 'mDiagnosisCommand',
by using this code:
sports.getData.implementation = function(){
Log.v("hook-sports", "try to add obd commands!");
var ret = this.getData();
var DataArray = Java.use("com.obd2.comm.DataArray");
var items = DataArray.$new("0x00,0x00,0x00,0x00,0x00,0x42");
this.mDiagnosisCommand.add(items); // not working!!!
Log.v("hook-sports", "hook done!");
return ret;
}
but doesn't works well.
I googled frida ArrayList add items without any help.
You have two problems:
You are using this.mDiagnosisCommand but the field is a static field, therefore it belongs to the class OBDSportsModelManager and not to the class instance this.
By calling this.mDiagnosisCommand you only get the Frida object representing this field, not the field value itself. If you want the ArrayList referenced by a field you have to add .value.
Considering both problems the following lines should work (after correcting the class name):
// correct the class name in the next line
var cls = Java.use("<full.name.to>.OBDSportsModelManager");
cls.mDiagnosisCommand.value.add(items);

How do I get an actual object dynamically in Dart? [duplicate]

In dart is it possible to instantiate a class from a string?
For example:
vanilla in javascript:
var myObject = window[classNameString];
Objective-C:
id myclass = [[NSClassFromString(#"MyClass") alloc] init];
You need to know the library name and class name to make things work properly. Assume you know both, the below example will instantiate the TestClass and call doStuff on it.
library test;
import "dart:mirrors";
class TestClass {
doStuff() => print("doStuff was called!");
}
main() {
MirrorSystem mirrors = currentMirrorSystem();
LibraryMirror lm = mirrors.libraries['test'];
ClassMirror cm = lm.classes['TestClass'];
Future tcFuture = cm.newInstance('', []);
tcFuture.then((InstanceMirror im) {
var tc = im.reflectee;
tc.doStuff();
});
}
A few notes about this solution:
The library test we are trying to load the class from is already imported in the VM, which makes this case a bit easier.
the call the newInstance allows for passing parameters to the constructor. Positional arguments are implemented, but named parameters are not yet implemented (as of the M2 release).
newInstance returns a Future to allow it to work across isolates.
The syntax has changed.
I got it working this way
library test;
import "dart:mirrors";
class TestClass {
doStuff() => print("doStuff was called!");
}
main() {
MirrorSystem mirrors = currentMirrorSystem();
LibraryMirror lm = mirrors.libraries.values.firstWhere(
(LibraryMirror lm) => lm.qualifiedName == new Symbol('test'));
ClassMirror cm = lm.declarations[new Symbol('TestClass')];
InstanceMirror im = cm.newInstance(new Symbol(''), []);
var tc = im.reflectee;
tc.doStuff();
}
If there are more libraries named 'test' this will fail though.
Try:
Map models = {"Player": Player.instatiate};
var player = models["Player"]();
class Player{
static instatiate() => Player();
}
This was an issue that has plagued me until I figured that I could implement a crude from method to handle the conversion of encoded Json Objects/strings or Dart Maps to the desired class.
Below is a simple example that also handles nulls and accepts JSON (as the string parameter)
import 'dart:convert';
class PaymentDetail
{
String AccountNumber;
double Amount;
int ChargeTypeID;
String CustomerNames;
PaymentDetail({
this.AccountNumber,
this.Amount,
this.ChargeTypeID,
this.CustomerNames
});
PaymentDetail from ({ string : String, object : Map }) {
var map = (object==null) ? (string==null) ? Map() : json.decode(string) : (object==null) ? Map() : object;
return new PaymentDetail(
AccountNumber : map["AccountNumber"] as String,
Amount : map["Amount"] as double,
ChargeTypeID : map["ChargeTypeID"] as int,
CustomerNames : map["CustomerNames"] as String
);
}
}
Below is it's implementation
PaymentDetail payDetail = new PaymentDetail().from(object: new Map());
PaymentDetail otherPayDetail = new PaymentDetail().from(object: {"AccountNumber": "1234", "Amount": 567.2980908});
Once again, this is simplistic and tedious to clone throughout the project but it works for simple cases.

Get private variable by reflection in dart

I would like to get private variable in an object in dart.
This variable has no getter so I want to do this with reflection.
I try many way but nothing works to me.
For exemple, when I do this:
var reflection = reflect(this);
InstanceMirror field = reflection.getField(new Symbol(fieldName));
I get an error:
No getter for fieldName.
It's normal because the variable hasn't getter.
How can I get this variable ?
EDIT with a test code:
Here is my reflect test (test variable is a reflectClass(MyClass))
reflectClass(Injector).declarations.keys.forEach((e) => test.getField(e, test.type.owner))
I get this error:
Class '_LocalInstanceMirror' has no instance method 'getField' with
matching arguments.
If I do this:
reflectClass(Injector).declarations.keys.forEach((e) => test.getField(e))
I get:
Class 'DynamicInjector' has no instance getter
'_PRIMITIVE_TYPES#0x1b5a3f8d'.
Same thing with values of declarations.
The error message you got is actually correct. The class has a getter for this field.
Dart implicitly creates getters for all and setters for all non-final/non-const fields.
It seems access to private members isn't yet supported in Dart2JS.
see https://code.google.com/p/dart/issues/detail?id=13881
Here an example how to access private fields:
(example from https://code.google.com/p/dart/issues/detail?id=16773)
import 'dart:mirrors';
class ClassWithPrivateField {
String _privateField;
}
void main() {
ClassMirror classM = reflectClass(ClassWithPrivateField);
Symbol privateFieldSymbol;
Symbol constructorSymbol;
for (DeclarationMirror declaration in classM.declarations.values) {
if (declaration is VariableMirror) {
privateFieldSymbol = declaration.simpleName;
} else if (declaration is MethodMirror && declaration.isConstructor) {
constructorSymbol = declaration.constructorName;
}
}
// it is not necessary to create the instance using reflection to be able to
// access its members with reflection
InstanceMirror instance = classM.newInstance(constructorSymbol, []);
// var s = new Symbol('_privateField'); // doesn't work for private fields
// to create a symbol for a private field you need the library
// if the class is in the main library
// var s = MirrorSystem.getSymbol('_privateField', currentMirrorSystem().isolate.rootLibrary);
// or simpler
// var s = MirrorSystem.getSymbol('_privateField', instance.type.owner);
for (var i=0; i<1000; ++i) {
instance.setField(privateFieldSymbol, 'test');
print('Iteration ${instance.getField(privateFieldSymbol)}');
}
}
using dson or serializable you can do it in next way:
library example_lib;
import 'package:dson/dson.dart';
// this should be the name of your file
part 'example.g.dart';
#serializable
class Example extends _$ExampleSerializable {
var _privateVar;
}
main() {
var example = new Example();
example['_privateVar'] = 'some value';
print('example._privateVar: ${example._privateVar}');
print('example["_privateVar"]: ${example["_privateVar']}");
}

how to create a new class instance object dynamically

I have a class
class Account extends Stuff{
String name;
newObject(){
return new Account();
}
}
inside the Stuff class I have a method
//generates list of objects of the same type
//as given object and fills attribute
generateObjectsFromExisting(names)
{
List list = new List();
InstanceMirror instanceMirror = reflect(this);
Symbol formatSymbol = new Symbol("newObject");
for(var name in names){
//calles newObject function from this and returns a new object
var newInstanceObject = instanceMirror.invoke(formatSymbol, []);
Symbol symbol = new Symbol("name");
InstanceMirror field = newInstanceObject.setField(symbol,name);
list.add(newInstanceObject.reflectee)
}
return list;
}
so when writing
main(){
var account = new Account();
List accounts = new List();
accounts = account.generateObjectsFromExisting(['tim','tom']);
print(account.name) // returns null
print(accounts[0].name) // returns tim
print(accounts[1].name) // returns tom
}
the problems with this way are
1 'generateObjectsFromExisting()' is on the 'account' object and not on Account
2 I have to manually add the "newObject" Method to every single class I implement.
I would prefer a static Method like 'Account.generateObjectsFromExisting()'
but how to to access 'this' (since its not available in static)
so I can say "this.new()" or something equivalent to "new Account();" eg "new this();"
and therefor be able to only have one 'newObject' function inside Stuff or maybe wont need it at all.
so now my code would look like this
class Account extends Stuff{
String name;
}
in Stuff
static generateObjectsFromExisting(names)
{
List list = new List();
for(var name in names){
var object = new this();
object.name = name;
list.add(object)
}
return list;
}
in main
main(){
// returns list of Accounts filled with names
accounts = Account.generateObjectsFromExisting(['tim','tom']);
print(accounts[0].name) // returns tim
print(accounts[1].name) // returns tom
}
if you can show me a way to access the Class to do something like this.new(); or new this(); then obviously the class 'Account' needs to be accessed and not the extended 'Stuff'
if the 'this' approach is not possible, then maybe you can show me a way how to access the Class from within an already existing object
like
generateObjectsFromExisting(names)
{
List list = new List();
var class = this.class;
var newObject = class.new():
...
}
or is my current approach the only solution. .. hope not :)
thank you
There are two ways I can think of at the moment. But both of them are pretty close to your initial solution as they both use reflection..
The non-static solution:
class Stuff {
generateObjectsFromExisting(List<String> names) {
var cm = reflectClass(this.runtimeType);
return names.map((name) {
var newInstance = cm.newInstance(const Symbol(''), []).reflectee;
newInstance.name = name;
return newInstance;
}).toList();
}
}
The static solution:
class Stuff {
static generateObjectsFromExisting(type, List<String> names) {
var cm = reflectClass(type);
return names.map((name) {
var newInstance = cm.newInstance(const Symbol(''), []).reflectee;
newInstance.name = name;
return newInstance;
}).toList();
}
}
You would call the static solution like this:
var accounts = Stuff.generateObjectsFromExisting(Account, ['tim', 'tom']);
There might be another solution involving factory constructors but can't think of any right now. Also, this code would easily break when you get another subclass of Stuff that does not have a name attribute. I don't know if you really intended on putting that attribute on Account instead of Stuff.
Also answering you 'Class'-Question. There is no class in Dart, there is only the Type and to get it you can do:
Type type1 = Account;
Type type2 = account.runtimeType;
But the Type doesn't have any methods you could use to create a new instance.

Return values when using Delegate with sendAndLoad in a AS2 class file

I have a Flash animation with AS2 code that calls sendAndLoad in a separate class file, and following guides online I've used a Delegate to handle the onLoad part, like this:
ActionScript Code in class:
function myFunction():String{
...
var send_lv:LoadVars = new LoadVars();
var saveresult_lv:LoadVars = new LoadVars();
send_lv.dataString = rc4_hash; send_lv.sendAndLoad("<my url>",saveresult_lv,"POST");
saveresult_lv.onLoad = Delegate.create(this, onLoad);
}
function onLoad (success:Boolean) {
if (success) {
}else{
}
}
I've got two issues:
Because I call myFunction() from my .fla file, how do I send the result of onLoad() back as the return value of myFunction()?
How do I refer to variables that were created inside myFunction from within onLoad?
In my fla file, I have code like this:
myVar:String=myFunction();
If onLoad takes any time then I think that myFunction() is returning nothing, and myVar ends up undefined. How do I get the fla to wait for the onLoad in the class file to finish and then populate myVar with the result?
Many thanks in advance for any tips
Your class could be restructured to answer your questions.
Separate the constructor, load function and result operation. When load is ready, the class can dispatch an event. Your FLA can listen to this event.
// Class code should something like this: (my AS2 is a little rusty)
class Sender {
private var classSavedData:String;
// these three vars are needed to use EventDispatcher.
public var addEventListener:Function;
public var removeEventListener:Function;
public var dispatchEvent:Function;
// Make your class ready to send events
public function Sender() {
EventDispatcher.initialize(this);
}
public function loadData(){
classSavedData = "Something the FLA can pick up after loading";
var send_lv:LoadVars = new LoadVars();
var saveresult_lv:LoadVars = new LoadVars();
send_lv.dataString = rc4_hash;
end_lv.sendAndLoad("<my url>",saveresult_lv,"POST");
saveresult_lv.onLoad = Delegate.create(this, onLoad);
}
private function onLoad():Void {
dispatchEvent({target:this, type:"eventFired", loadAnswer: saveresult_lv , extraData:classSavedData});
}
}
... and code in FLA can look like this:
var myClassInstance:Sender = new Sender()
myClassInstance.addEventListener ("eventFired", Delegate.create (this, onDataLoaded))
myClassInstance.loadData()
function onDataLoaded(evt:Object){
trace ("loaded loadvars "+evt.loadAnswer)
}

Resources