How to register factory of part in Dart - class

I have a lot of part of library. But all of the same type (extends Part)
part1.dart
part of Parts;
class Part1 extends Part { /* ... */ }
parts.add((varOfSomeClass){ return new Part1(varOfSomeClass + 1); });
part2.dart
part of Parts;
class Part2 extends Part { /* ... */ }
parts.add((varOfSomeClass){ return new Part2(varOfSomeClass - 1); });
parts.dart
library Parts;
part "Part1.dart";
part "Part2.dart";
List<Function> parts = new List<Function>();
class Parts {
getPart(int index) {
if (parts.contains(index)) {
return parts[index](someVarOfThisClass);
}
}
}
OUTPUT: error: unexpected token 'parts'
How to get all included factories without create instance all the Part classes?
For example need to do:
BMW.dart
part of Auto;
class BMW {
String color;
BMW(this.color);
}
list.add((color){
return new BMW(color);
});
Lada.dart
part of Auto;
class Lada {
List<int> color;
}
list.add((color){
var auto = new Lada();
auto.color = hex2rgb(color);
return auto;
});
Auto.dart
library Auto;
class Auto {
getByIndex(int index) {
if (list.contains(index)) {
return list[index](color);
}
return null;
}
}

Looks like your problem is that you have code outside of a class or function definition. If I'm guessing what you want to do correctly, you want BMW.dart to look something like
part of Auto;
class BMW {
String color;
BMW(this.color);
}
and then in your main() method have code like
main() {
List list = [];
list.add((color) => new BMW(color));
}
This will get the code above running. It's probably not the best way to structure your program though. You may want to do some more reading on Dart factory constructors. https://www.dartlang.org/dart-tips/dart-tips-ep-11.html is a good place to start.

I don't know what you need it for but how about using mirrors:
main.dart
library cars;
import 'dart:mirrors';
part 'bmw.dart';
part 'audi.dart';
abstract class Car {
}
void main() {
List<Car> cars = new List<Car>();
Map libraries = currentMirrorSystem().libraries;
LibraryMirror mirror = libraries[libraries.keys.last];
mirror.declarations.forEach((Symbol s, DeclarationMirror mirror) {
if(mirror is ClassMirror) {
if(!mirror.isAbstract && mirror.isAssignableTo(reflectType(Car))) {
// new Symbol(mirror.reflectedType.toString()))
cars.add(mirror.newInstance(#Car, []).reflectee);
}
}
});
print(cars);
}
bmw.dart
part of cars;
class Bmw extends Car {
Bmw.Car() {
}
}
audi.dart
part of cars;
class Audi extends Car {
Audi.Car() {
}
}

Related

How to get function body as string using build_runner and source_gen?

My goal is to make my unit tests easy to understand. Currently, they are hard to understand because they have so many nested functions.
I want to use build_runner to generate the code of the unit with all functions unwrapped.
So here is an example of my current test:
test.dart
import 'package:example_usage/src/unwrap.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
class Cat {
String sound() => "Meow";
int walk() => 4;
}
class Dog {
final Cat cat;
Dog(this.cat);
String sayHi() {
return this.cat.sound();
}
int jump() {
return this.cat.walk();
}
}
class MockCat extends Mock implements Cat {}
void main() {
MockCat cat;
Dog dog;
#UnWrap()
void setupCatSoundStub() {
when(cat.sound()).thenReturn("Woof");
}
#UnWrap()
void setupCatWalkstub() {
when(cat.walk()).thenReturn(2);
}
#UnWrap()
void expectCatCalled() {
verify(cat.sound());
}
#UnWrap()
void testDogWoof() {
setupCatSoundStub();
dog = Dog(cat);
final sound = dog.sayHi();
expect(sound, "Woof");
expectCatCalled();
}
void expectCatWalked() {
verify(cat.walk());
}
group('Dog Cat Play', () {
setUp(() {
cat = MockCat();
});
test('Dog woof', () {
testDogWoof();
});
test('Dog woof then jump', () {
testDogWoof();
setupCatWalkstub();
final steps = dog.jump();
expect(steps, 2);
expectCatWalked();
});
});
}
I want to generate a code like this
_$test.dart
void _$main() {
MockCat cat;
Dog dog;
void expectCatWalked() {
verify(cat.walk());
}
group('Dog Cat Play', () {
setUp(() {
cat = MockCat();
});
test('Dog woof', () {
// testDogWoof();
// setupCatSoundStub();
when(cat.sound()).thenReturn("Woof");
dog = Dog(cat);
final sound = dog.sayHi();
expect(sound, "Woof");
// expectCatCalled();
verify(cat.sound());
});
test('Dog woof then jump', () {
// testDogWoof();
// setupCatSoundStub();
when(cat.sound()).thenReturn("Woof");
dog = Dog(cat);
final sound = dog.sayHi();
expect(sound, "Woof");
// expectCatCalled();
verify(cat.sound());
// setupCatWalkstub();
when(cat.walk()).thenReturn(2);
final steps = dog.jump();
expect(steps, 2);
expectCatWalked();
});
});
}
I found some tutorial online but I could find documentations about getting the function body into string ( some like JavaScript's Function.prototype.toString() method ) I am new to code generation so I tried to print all fields but I can't find anything like that.
import 'dart:async';
import 'package:analyzer/dart/element/element.dart';
import 'package:build/build.dart';
import 'package:source_gen/source_gen.dart';
class InfoGenerator extends Generator {
#override
FutureOr<String> generate(LibraryReader library, BuildStep buildStep) {
var buffer = StringBuffer();
// library.allElements.forEach((element) {
// buffer.writeln(
// '// ${element.displayName} - ${element.source.fullName} - ${element.declaration}');
// });
library.allElements.whereType<TopLevelVariableElement>().forEach((element) {
buffer.writeln('/*');
buffer.writeln(element.toString());
buffer.writeln('*/');
buffer.writeln(
'// ${element.name} - ${element.kind.displayName} - ${element.declaration}');
});
return buffer.toString();
}
}
I am also new to annotations so I just made this up
/// What to do here ?
class UnWrap {
const UnWrap();
}
Is what I am trying to do even possible ?

How to override a Dart method on instantiation? [duplicate]

Is there way to overriding method in Dart like JAVA, for example:
public class A {
public void handleLoad() {
}
}
And when overriding:
A a = new A() {
#Override
public void handleLoad() {
// do some code
}
};
No, Dart does not have anonymous classes. You have to create a class that extends A and instantiate it.
No but it much less useful in Dart because you can just reassign function:
typedef void PrintMsg(msg);
class Printer {
PrintMsg foo = (m) => print(m);
}
main() {
Printer p = new Printer()
..foo('Hello') // Hello
..foo = ((String msg) => print(msg.toUpperCase()))
..foo('Hello'); //HELLO
}
However you will need some extra boilerplate to access instance.
Use type Function:
class A {
final Function h
A(this.h);
void handleLoad(String loadResult) { h(loadResult); }
}
Or
class A {
final Function handleLoad;
A(this.handleLoad);
}
A a = new A((String loadResult){
//do smth.
});

Why can't I set a dynamic property inside a nested function?

I'm trying to create a function that can dynamically set the properties on an object like so:
void main() {
final obj = Item();
obj.update(5);
print(obj.xVal);
}
class ObjectBase {
void _setData(current, newValue) {
current = newValue;
print(current);
}
}
class Item extends ObjectBase {
int _x;
int get xVal => _x;
update(x) {
_setData(_x, x);
}
}
The print statement in _setData works fine, but it doesn't actually appear to change _x, even if it has been passed through. I expected that changing the reference here would update it everywhere.
So why isn't this working and is there a fix?
You can assume that I do have good reason to be calling _setData inside update rather than just implementing the functionality in update.
Update:
A real life example of what i'm trying to achieve
class ViewModel extends ChangeNotifier {
void _setDataFromDependency(current, newValue) {
if (!deepDynamicEquality(current, newValue)) {
current = newValue;
notifyListeners();
}
}
}
class ListScreenViewModel extends ViewModel {
int _localCount = 0;
List<int> _globalList;
ListScreenViewModel();
List<int> get globalList => _globalList;
int get localCount => _localCount;
incrementLocal() {
_localCount++;
notifyListeners();
}
void update(ListStore listStore) {
_setDataFromDependency(_globalList, listStore.globalList);
// if (!deepDynamicEquality(_globalList, listStore.globalList)) {
// _globalList = listStore.globalList;
// notifyListeners();
// }
}
}
An oversimplified workaround is to return the value from _setData . #julemand101 has already answered limitations.
class ObjectBase {
int _setData(current, newValue) {
current = newValue;
print('current: $current');
return current;
}
}
class Item extends ObjectBase {
int _x;
int get xVal => _x;
update(x) {
_x = _setData(_x, x);
}
}

java jdk replace native class-file

I have ImageView.class how to get the program to use it instead of the native javax.swing.text.html.ImageView?
javax.swing.text.html.ImageView map = new javax.swing.text.html.ImageView(); //does not work
I was told that it is necessary to use ClassFileTransformer and ClassLoader, but I can not find a working examples
I think that what you really want to do is to...
Extend HTMLEditorKit and override getViewFactory()
Have it return a class that extends HTMLEditorKit.HTMLFactory
In that class, override create() to return your custom view for <img> and super.create() otherwise
Like this:
class MyImageKit extends HTMLEditorKit {
private static final MyImageFactory myFactory = new MyImageFactory();
public ViewFactory getViewFactory() {
return myFactory;
}
static class MyImageFactory extends HTMLFactory {
public View create(Element elem) {
Object type = elem.getAttributes()
.getAttribute(StyleConstants.NameAttribute);
if(type == HTML.Tag.IMG) {
return new MyImageView(elem);
} else {
return super.create(elem);
}
}
}
}
class MyImageView extends ImageView {
MyImageView(Element elem) {
super(elem);
}
protected void setPropertiesFromAttributes() {
super.setPropertiesFromAttributes();
try {
ImageView.class.getDeclaredField("vAlign").set(this, new Float(0.75f));
} catch(Exception e) {
e.printStackTrace();
}
}
}

StructureMap InstanceInterceptor not being called

I want to intercept the creation of an instance in SM and I'm trying the following but it's not calling the InstanceInterceptor implementation, does anyone know why?
ForRequestedType<IPublishResources>()
.TheDefault
.Is
.OfConcreteType<PublisherService>()
.InterceptWith(new PublisherServiceInterceptor());
The test code uses the ObjectFactory to create instances, and is shown below:
// Given we have a configure object factory in StructureMap...
ObjectFactory.Configure(x => x.AddRegistry(new StructureMapServiceRegistry()));
// When we request a publisher service...
var publisher = ObjectFactory.GetInstance<IPublishResources>();
Cheers
AWC
I could not reproduce your problem in release 2.5.4. Here is my code.
public interface IPublishResources {}
class PublishResources : IPublishResources {}
public class LoggingInterceptor : InstanceInterceptor
{
//this interceptor is a silly example of one
public object Process(object target, IContext context)
{
Console.WriteLine("Interceptor Called");
return context.GetInstance<PublishResources>();
}
}
public class MyRegistry : Registry
{
public MyRegistry()
{
For<IPublishResources>()
.Use<PublishResources>()
.InterceptWith(new LoggingInterceptor());
}
}
[TestFixture]
public class Structuremap_interception_configuraiton
{
[Test]
public void connecting_implementations()
{
var container = new Container(cfg =>
{
cfg.AddRegistry<MyRegistry>();
});
container.GetInstance<IPublishResources>();
}
}
A question. Do you really need to use an Interceptor here? If you only need to define a factory you can do somethign like this.
public interface IPublishResourcesFactory
{
IPublishResources Create();
}
public class MyRegistry : Registry
{
public MyRegistry()
{
For<IPublishResources>().Use(c =>
{
return c.GetInstance<IPublishResourcesFactory>().Create();
});
//or
For<IPublishResources>().Use(c =>
{
//other object building code.
return new PublishResources();
});
}
}