Access static methods and fields with class type returned by dlsym - dynamic-loading

Can we access static methods and fields with class type returned by dlsym?
Ex:
MyClass* pMyClass= (MyClass*)dlsym(phandle, "MyClass");
pMyClass->staticFunction();
pMyClass->staticField=0;
;

Related

Is there a way to access a mixin's private variable in the class using the mixin?

In dart when creating a mixin, you can declare properties and methods like a class. When declaring a private property/method, it seems the inheriting class should also have access to this private member (see below for example).
Is there a way to access a mixin's private variable in the class using the mixin?
If it's not possible, how can I declare a member in the mixin object but make it private in the inheriting class's interface.
mixin.dart
mixin A {
String propertyOne = '1';
// This property is not accessible to any inheriting class.
int _privateProperty = 2;
}
class.dart
class B with A {
String get mixinString => propertyOne;
// This property is not accessible to the B class.
int get mixinInt => _privateProperty;
}
No. A property being library private means that you can only express its name inside the same library. In any other library, the identifier _privateProperty is a different name, one private to that other library.
If you cannot declare both mixin and class in the same library, and you definitely need access to the property, then you can do any number of things to allow that.
Make the property public and tell people not to use it except in subclasses. They still can if they want to.
Make the property public and mark it #protected, to have the analyzer tell people to not use it except in subclasses. They still can if they want to.
Keep the property private and provide a separate method to access it:
mixin A {
// This property is not accessible to any inheriting class.
int _privateProperty = 2;
static int getPrivateProperty(A a) => a._privateProperty;
static void setPrivateProperty(A a, int value) {
a._privateProperty = value;
}
}
Anyone can still get to the property if they really want to, but they need to know that
it comes from A.

Dart: should the instance variables be private or public in a private class?

For example:
class _Foo {
String _var1;
String var2;
}
I always use public variable var2 because I think it's no point to make private variables when the class is already private, because you can not access private class anyway.
But I found many people use private variable _var1. Is this just a personal preference? When the class is private, what is the point of private instance variable? If you can not access the private class, you can not access all of its instance variables regardless whether they are private or not. If you can access the private class in the same lib, then you can access its all instance variables regardless whether they are private or not.
Making the class private doesn't make its members private and it doesn't make instances of that class inaccessible.
Assume
lib/private_class.dart
class Foo {
final _PrivateClass privateClass = _PrivateClass();
}
class _PrivateClass {
String publicFoo = 'foo';
String _privateBar = 'bar';
}
bin/main.dart
import 'package:so_53495089_private_field_in_private_class/private_class.dart';
main(List<String> arguments) {
final foo = Foo();
print(foo.privateClass.publicFoo);
// print(foo.privateClass._privateBar); // invalid because of ._privateBar
}
You can't declare variables or parameters of the type of a private class or extend or implement the class in another library or create an instance of that class,
but otherwise there is not much difference.
So if the field is supposed to be hidden (internal state) to users of the API, then make the field private.

java.lang.IllegalAccessException: can not access a member of class java.util.Collections$UnmodifiableCollection with modifiers "public"

I have classes PrimitiveProperty and ComplexProperty and the interface Property. I want to create an implementation of Property which enforces an empty unmodifiable Set of Property instances as return value of Property.getProperties, e.g.
public interface Property {
Set<Property> getProperties();
}
public class ComplexProperty implements Property {
private Set<Property> properties;
//getter overrides the interface method
}
public class PrimitiveProperty implements Property {
private final static Set<Property> EMPTY_PROPS = Collections.unmodifiableSet(new HashSet<Property>(1));
#Override
public Set<Property> getProperties() {
return EMPTY_PROPS;
}
}
With Glassfish 4.0 and I'm getting
java.lang.IllegalAccessException: Class javax.el.ELUtil can not access a member of class java.util.Collections$UnmodifiableCollection with modifiers "public"
when I access the property in the leaf attribute of a Richfaces tree, e.g.
<r:tree id="aTree"
toggleType="ajax" var="item" >
<r:treeModelRecursiveAdaptor roots="#{aCtrl.roots}"
nodes="#{item.properties}" leaf="#{item.properties.isEmpty()}">
<r:treeNode>
#{item.name}
</r:treeNode>
<r:treeModelAdaptor nodes="#{item.properties}" leaf="#{item.properties.isEmpty()}"/>
</r:treeModelRecursiveAdaptor>
</r:tree>
The issue disappears if I make the EMPTY_PROPS constant modifiable (by assigning an instance of HashSet instead of the return value of Collections.unmodifiableSet) which is not my intention.
Is it possible to achieve what I want to do? Do I have to invent or use an implementation of what Collections$UnmodifiableCollection ('s subclasses) do(es) which is compatible with the JSF access needs?
Here is the problem:
leaf="#{item.properties.isEmpty()}"
You're attempting to invoke a method directly on the UnmodifiableSet instance. Although it implements Collection, which is public, the UnmodifiableSet implementation itself, where EL (read: Reflection API) is trying to find the method on the class, is not public.
Exactly this problem is reproducible in plain Java (a main() method) as follows:
Set<Object> set = Collections.unmodifiableSet(new HashSet<>());
for (Method method : set.getClass().getMethods()) {
if ("isEmpty".equals(method.getName())) {
method.invoke(set); // IllegalAccessException.
}
}
This is essentially a bug in the EL implementation used.
You'd better just use EL's own empty operator:
leaf="#{empty item.properties}"

Can't visit static variable of parent class in Dart?

Dart code:
main() {
print(PPP.name);
print(CCC.name);
}
class PPP {
static String name = "PPP";
}
class CCC extends PPP {
}
It prints:
PPP
Unhandled exception:
No static getter 'name' declared in class 'CCC'.
NoSuchMethodError : method not found: 'name'
Receiver: Type: class 'CCC'
Arguments: [...]
So it's not available to visit static variables of parent class in Dart?
From Dart Programming Language Specification:
The static members of a class are its static methods, getters, setters and static variables.
Superclass static members are not in scope in subclasses, and do not conflict with subclass members.
Static members are never inherited.
Static members never override anything.
So if you declare some static members in superclass then these members are not inherited in subclasses.
They remain in that class where they declared and do not conflicts with other declarations static members in the subclasses.
Q: Can't visit static variable of parent class in Dart?
A: Static variable of parent class cannot be accessed (as its own) in child class because it does not exists (not inherited) in child class.

Get parent class in Gobject

I am new to GObject.I confused something about getting pointer to father of one class.For example ,
On Gobject Spec, we have a class:
struct _MamanBarClass {
GObjectClass parent;
/* class members */
};
what is the difference between :
MamanBarClass klass;
GObjectClass parent_class=G_OBJECT_CLASS(g_type_class_peek_parent (klass));
and
MamanBarClass klass;
GObjectClass g_object_class=G_OBJECT_CLASS(klass);
what is the difference between g_object_class and parent_class
One more question:
the difference between casting MamanBarClass(klass) and MAMANBARCLASS(klass)
thank you!
In GObject, any new type keeps a class struct in a register to keep runtime info, virtual methods and private class data.
The casting (G_OBJECT_CLASS(klass)) is used to retrieve the GObjectClass of klass while G_OBJECT_CLASS(g_type_class_peek_parent(klass)) returns the GObjectClass of the parent of klass. In your example, the former simply returns the GObjectClass struct of MamanBarClass while the latter returns the GObjectClass struct of the GObjectClass struct (the parent of MamanBarClass) found in the register. In practice:
klass = G_OBJECT_CLASS(maman_bar_class);
/* This will override MamanBar::dispose */
klass->dispose = my_dispose;
/* Now my_dispose will be called by g_object_dispose(object) when object is a MamanBar object */
klass = G_OBJECT_CLASS(g_type_class_peek_parent(klass));
/* This will override GObject::dispose */
klass->dispose = my_dispose
/* Now my_dispose will be called by g_object_dispose(object) when object is a GObject (the parent class of MamanBar) object */
(MamanBarClass *) klass and MAMAN_BAR_CLASS(klass) are equivalent, but the latter performs runtime type checking (emits a g_critical if klass is not a MamanBarClass *).

Resources