Class '(Anonymous class)' incorrectly extends base class 'T' - typescript-decorator

Hi I want to implement a class decorator that extends the constructor of the original class; that is my code:
export function Offline<T extends {new (...args:any[]):T}>(constr:T){
OfflineManagerService.registerService(constr)
console.log("-- decorator function invoked --");
return class extends constr{
constructor(...args:any[]){
super(...args)
console.log('decorated')
}
I get this error :
Class '(Anonymous class)' incorrectly extends base class 'T'.
'(Anonymous class)' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'new (...args: any[]) => T'
unfortunately I do not know how to fix it

Related

What is superclass mismatch in Crystal Lang?

I am trying to implement a rate-limiting handler with Kemal.
I have a class, RateLimiter, that inherits the class Kemal::Handler. On compile I get the error:
Error in src/rate_limiter.cr:5: superclass mismatch for class RateLimiter (Kemal::Handler for Reference)
I'm new to Crystal and that means nothing to me. What am I doing wrong?
This indicates that RateLimiter was defined previously somewhere, without any explicit superclass specification:
class Base; end
class Foo; end
class Foo < Base; end
That gives
Error in line 3: superclass mismatch for class Foo (Base for Reference)
https://carc.in/#/r/3r2l
Search through your project and dependencies for class RateLimiter giving conflicting definitions of that type.

How to set the value of private variable in a super class using mockito or powermockito

I have three classes A, B and C Where A is abstract class, B is also an abstract class and B extends A and C is non-abstract which extends B. I have variable by name AddressService addressService which is public in class B and is private in class A. I am writing a test case for class C which is non-abstract and calling method which is in class A and in class A i need to set the value of AddressService addressService variable which is used to invoke a method. AddressService is an interface which has methods and i am invoking one of the method in my super class A. Following is my code
public interface AddressService{
void test();
}
public abstract class A{
private AddressService addressService = (AddressService) ServiceLocatorBeanFactory.getService(AddressService.class);
public void createDocument(){
addressService . test();
}
}
public abstract class B extends A{
public AddressService addressService = (AddressService) ServiceLocatorBeanFactory.getService(AddressService.class);
}
public class C extends B {
}
Here is my test class
#RunWith(PowerMockRunner.class)
#PrepareForTest({ServiceLocatorBeanFactory.class})
public class createTest {
#SuppressWarnings("unchecked")
#Test
public void createTurnaroundDocument() throws Exception{
PowerMockito.mockStatic(ServiceLocatorBeanFactory.class);
AddressService addressService = Mockito.mock(AddressService.class);
PowerMockito.when(ServiceLocatorBeanFactory.getService(AddressService.class)).thenReturn(addressService);
C original = new C();
C handler = PowerMockito.spy(original);
handler.createDocument();
}
}
I tried setting the value of AddressService addressService present in class A which is abstract in multiple ways but still the value is null and i get NullPointer Exception.
Following are the different ways
1.Whitebox.setInternalState(handler, AddressService.class, addressService);
When i set this way the value is not set and is null and gives me NullPointerException
2.MemberModifier.field(A.class, "addressService").set(A.class, addressService);
When i set this way i get the following exception
java.lang.IllegalArgumentException: Can not set AddressService field A.addressService to java.lang.Class
3.Whitebox.setInternalState(A.class, AddressService.class, addressService);
When i set this way i get the following exception
org.powermock.reflect.exceptions.FieldNotFoundException: No static field assignable from "AddressService" could be found in the class hierarchy of A.
4.Whitebox.setInternalState(A.class, "addressService", addressService);
when i set this way i get the following exception
org.powermock.reflect.exceptions.FieldNotFoundException: No static field named "addressService" could be found in the class hierarchy of A.
Is there a way where we can set the value of private variable present in the super class using Mockito or PowerMockito. Please help
MemberModifier.field(A.class, "addressService").set(A.class, addressService);
This is wrong: java.lang.reflect.Field.set needs an instance of your class if you want to set a non-static method, not the class. Might still not work, perhaps you will have to use setAccessible(true) on the field first:
Field field = MemberModifier.field(A.class, "addressService");
field.setAccessible(true);
field.set(c, addressService);
You can do the first line with pure reflection anway...
Field field = A.class.getDeclaredField( "addressService" );
field.setAccessible(true);
field.set(c, addressService);
But of course, the big mystery is: Why do you have the same variable twice? Smells like bad code. Make the inner one protected instead of static, it's very unlikely that something good will come from having two.

Issues with template classes and inheritance - 'List' does not name a non-static data member or base class

I'm relatively new to c++ and having a very hard time figuring out the issue with what I am doing. I hope my description isn't too confusing, but I have two separate class hierarchies going on. The base for both are class templates.
Class hierarchy one is as follows
template <class T>
class Order {}
// StoreOrder inherits from Order
class StoreOrder: public Order<Item *> {}
Class hierarchy two is as follows. The tricky part here is in OrderList
template <class T>
class List{}
// OrderList inherits from List
class OrderList : public List<StoreOrder *>{}
OrderList::OrderList(): List(), DatabasePath(""){}
When I pass a StoreOrder as the List template type I get an error in the constructor function that says
'List' does not name a non-static data member or base class
All classes work fine in every other situation that I've used them, so I believe it has to do with the fact that StoreOrder and OrderList both derive from template classes. Any help would be great.
Instead of:
OrderList::OrderList(): List(), DatabasePath(""){}
Use:
OrderList::OrderList(): List<StoreOrder *>(), DatabasePath(""){}
List is not a type. List<StoreOrder*> is a type.

Class declaration with a specified base class

I'm trying to declare class as the following:
class MyClass: MyBase;
But I can't because compiler is swearing.
error: expected ‘{’ before ‘;’ token
I'm trying to find a class name declaration to clarify this aspect. But I can't. I'm looking for this in the clause 7 (Declarations) of the c++ working draft.
If you just want to declare the MyClass class, then
class MyClass;
is enough. It tells the compiler that the class MyClass exists, and you can now declare pointers or references to MyClass.
If you want to define the class, then you need to full definition.
About standard(n3797):
9.1 Class names:
A class declaration introduces the class name... A declaration
consisting solely of class-key identifier; is either a redeclaration of
the name in the current scope or a forward declaration of the
identifier as a class name. It introduces the class name into the
current scope.
10 Derived classes:
A list of base classes can be specified in a class definition...
So, you can just tell the compiler: "Oh, I will define this class later". If you need to know the "structure" of the class then you need to define it.
Give definition in this way :
class MyClass: MyBase
{
//////
};
If you only want to declare then do in this way :
class MyClass;

How to instantiate a class properly?

I'm trying to implement into ATL :
std::list<CMyClass> listMC;
CMyClass lmc;
listMC.insert(listMC.end(), lmc);
How do I instantiate lmc which is an ATL class which was created using Class View > New > Class ?
I'm actually getting errors on lmc :
Error: object of abstract class type "CProcessusModel" is not allowed :
function "CProcessusModel::AddRef" is a virtual function
function "CProcessusModel::Release" is a virtual function
function "CProcessusModel::QueryInterface" is a virtual function
Thanks a lot!
According to your error messages, the class CProcessusModel is abstract. You cannot instantiate abstract classes.
To make your class concrete, you have to
A) Implement the virtual functions that are listed in the error message in the class CProcessusModel or
B) derive a concrete subclass from the abstract CProcessusModel, which implements all pure virtual methods and instantiate objects of that type.