Skip to content

Member Access

qtiuto edited this page Apr 4, 2019 · 10 revisions

Type means a java type like String,Object. obj means a java object

Once you have a type from import or Type,you can access its static fields like Type.name or Type['name'] and its methods like Type.name(...) or Type['name'](...). Once you got a java object you can do the similar operation to its non-static fields or methods.

However,if the class has some fields and methods with the same name, you can't get or set the field by Type.name or obj.name. Instead, you must use (Type or obj).name[any value you like] to use the field.

Another special case is that the field with the same name but different types,which is not allowed by the compiler but ok in jvm. To distinguish it,use (Type or obj).name[according type] to operate on the field.

Google ban access to some black list members and warn about gray list members since Android Pie, so I disable this feature once the lib is loaded.

Note:

Type.class will return the class object represented by the type.

Type.innerClass where innerClass is the name of an enclosing type of the type will return that enclosing type if exists. However, if any static field or method(including new,class,instanceof, assignableFrom) of that type have the same name, it's returned first.

Type.new(args...) will construct a new object like new does if no static field or method named new in that type.This is syntax sugar for new.

Type.assignableFrom(anotherType) will return whether the object of this type can be cast to the other type if no static field or method named assignableFrom in that type.

obj.instanceof(givenType) will return whether the object can be cast to the given type if no non-static field or method named instanceof in that type. This is a short way for the instanceof method.

obj.super returns a super object for the given object. It's provided to call super methods which may be override. This is a short way for the super method

'.length' of an array object will return its length,you can use '#' operator to get the length of an array object also.'#' operator also works for any object with length() or size() method declared.

The lua method 'tostring' works for any java object and java type,and toString() method will be invoked.

'==' operator for java object will always return true if the two object is the same java object

'..' operator will concat a java object with any lua object with tostring to be called,and return a lua string.

The lua method 'pairs' works for any java object work Collection or Map or SparseArray or Array, or you can add custom iterator,false is return for null key or value. Though I don't add a function to convert a map to lua table, you can use iteration to work on it like the below

function toTable(map)
   local t={}
   for k,v in ipairs(map) do
       t[k]=v
   end
   return t
end

You can index any java object with set/put,get/at methods.When you index or add an index, these two methods will be invoked,or you can add custom indexer. For example,

   using "java.util"
   local arr=HashMap({a=5,b=6})
   arr.c='kkk'
   arr[2]=8
   print(#arr,arr[2])
   for k,v in ipairs(arr) do
      print(k,v)
   end

If an object has non-private getter/setter method for a non-static property without associated field, then you can operator on the property like a true field. However If the property has only getter method, then set operation will raise an error,and vise versa. For Example,

   using "android.view"
   using "android.widget"
   view=TextView(context)
   view.visibilty=View.GONE
   print(view.visibilty)

A newly accessed member will be by uservalue on lua >= 5.2, so member access should be twice faster than luajit. However, if the member is only access once, the speed may be slower cause a table is requried to be allocated to hold the members. I can not argue which is better. But for performance test, the previous may be better.

Clone this wiki locally