B4J Question Accessing a 'protected' member of a class in Java

jkhazraji

Active Member
Licensed User
Longtime User
How to access a member of a class in Java with a modifier 'protected' using Javacode or inline java in B4J so that a new instance can be initialized from it.
e.g.,
B4X:
dim Obj as JavaObject = (Me).As(JavaObject).InitializeNewInstance("classA.classB.protectedClassMember",Null);
I searched in the forum and found the jReflection library of Mr. Graham @agraham but I could not figure out the way it accesses the Java objects
not exposed to the B4J language.

The working answer will be greatly appreciated.
 
Solution
The following code also exposes any protected Java class without the need to use 'extends':

B4X:
    Dim Class1 As JavaObject
    Dim unprotected as JavaObject
    Class1.InitializeStatic("java.lang.Class")
    unprotected= Class1.RunMethod("forName",Array("classA.classB.protectedClassmember"))

jkhazraji

Active Member
Licensed User
Longtime User
I tried using jRelection library as the following line:
B4X:
Private rfRoot As Reflector
Private root As JavaObject
'
'
'
'
root=rfRoot.CreateObject("classA.classB.protectedClassmember")
However, it gave the error:
java.lang.RuntimeException: java.lang.IllegalAccessException: class anywheresoftware.b4j.agraham.reflection.Reflection cannot access a member of class classA.ClassB.protectedmemberClass (in module .......) with modifiers "protected"
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Easiest way is to extend the class in a public class.
Example:

B4X:
Sub AppStart (Args() As String)
    ' works as expected
    Dim jo As JavaObject
    jo.InitializeNewInstance("b4j.example.main$NormalClass",Null) 
    Log(jo)
    Log(jo.RunMethod("getName",Null))
    
    ' works because sneaky extends MyProtectedClass
    Dim jo1 As JavaObject
    jo1.InitializeNewInstance("b4j.example.main$Sneaky",Null)
    Log(jo1)
    Log(jo1.RunMethod("getName",Null))

    ' doesn't work as class is protected
    Dim jo2 As JavaObject
    Try
        jo2.InitializeNewInstance("b4j.example.main$MyProtectedClass",Null)
        Log(jo2)
        Log(jo2.RunMethod("getName",Null))
    Catch 
        Log(LastException)
    End Try            
End Sub

#if java
protected static class MyProtectedClass{
    
    protected String name = "fred";
    
    protected String getName(){
        return name;
    }
}
public static class Sneaky extends MyProtectedClass{

    // name is not defined as it exists in MyProtectedClass
    public String getName(){
        return name;
    }
}

public static class NormalClass{

    String name = "Jimmy";
    
    public String getName(){
        return name;
    }
}
    
#End If
 
Upvote 1

drgottjr

Expert
Licensed User
Longtime User
i read his post as meaning he's trying to get "fred"
 
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
The following code also exposes any protected Java class without the need to use 'extends':

B4X:
    Dim Class1 As JavaObject
    Dim unprotected as JavaObject
    Class1.InitializeStatic("java.lang.Class")
    unprotected= Class1.RunMethod("forName",Array("classA.classB.protectedClassmember"))
 
Upvote 0
Solution
Top