Android Question inline java:Uses of Reflection,Drawbacks of Reflection

Discussion in 'Android Questions' started by xiaoyao, Mar 31, 2015.

  1. xiaoyao

    xiaoyao Member Licensed User

    if b4a can write inline java without reflection,i think it's the best

    b4a vb code:
    Sub Process_Globals
    Private NativeMe As JavaObject
    End Sub
    Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
    End If
    Dim s As String = NativeMe.RunMethod("FirstMethod", Null)
    Msgbox("s=" & s,"")
    End Sub

    #If JAVA
    public String FirstMethod() {
    return "Hello World!";
    #End If
    if i use inline java,b4a will create javacode in folder:
    java code:
    public static String _process_globals() throws Exception{
    _v5 = new anywheresoftware.b4j.object.JavaObject();
    public String FirstMethod() {
    return "Hello World!";
    _s = BA.ObjectToString(_v5.RunMethod("FirstMethod",(Object[])

    反射的用途 Uses of Reflection
    Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.
    反射的缺点 Drawbacks of Reflection
    Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.
    性能第一 Performance Overhead Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.
    反射包括了一些动态类型,所以JVM无法对这些代码进行优化。因此,反射操作的效率要比那些非反射操作低得多。我们应该避免在经常被 执行的代码或对性能要求很高的程序中使用反射。 安全限制 Security Restrictions Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet. 使用反射技术要求程序必须在一个没有安全限制的环境中运行。如果一个程序必须在有安全限制的环境中运行,如Applet,那么这就是个问题了。。 内部暴露 Exposure of Internals Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform. 由于反射允许代码执行一些在正常情况下不被允许的操作(比如访问私有的属性和方法),所以使用反射可能会导致意料之外的副作用--代码有功能上的错误,降低可移植性。反射代码破坏了抽象性,因此当平台发生改变的时候,代码的行为就有可能也随着变化。
  2. Erel

    Erel Administrator Staff Member Licensed User

    What is the purpose of this thread?

    You know that inline Java is supported, right?

    Inline Java Code
  3. xiaoyao

    xiaoyao Member Licensed User

    inline java call in
    public String FirstMethod() {
    return "Hello World!";

    _s = BA.ObjectToString(_v5.RunMethod("FirstMethod",(Object[])

    if this can change to :
    _s = BA.ObjectToString(FirstMethod)

    i think it running will be quickly

  4. Erel

    Erel Administrator Staff Member Licensed User

    It will not have any effect on the performance.
  5. xiaoyao

    xiaoyao Member Licensed User

    if i write b4a code for some "simple jar",i did'nt need xml file,it's so easy.
    some jar ,the method will no args,or invoke agrs same like (long ,int ,string),so if the ide can auto show the method without xml file,i think it's good.

    If for some simple JAR, we don't need to create XML documents can call, that this is good.
    If a JAR is referenced, B4A IDE can automatically identify all his methods, public variables, methods and so on, the
    The more convenient. Is there a method, method can not reflected, directly call reference to engineering JAR
    Method and variable packet. If I just insert JAVA code in the B4A development tool, if I insert a class,
    Direct statement and calling, that this is good, even if I need to write some auxiliary symbol is also OK

    如果一个JAR被引用了,B4A IDE能自动识别出他所有的方法,公共变量,方法等,那该多方便。也就是有没有一种方法,能不用反射的方法,直接调用引用到工程中的JAR包的方法和变量。如果我只是在B4A开发工具中插入JAVA代码,如果我插入了一个类,能直接声明并调用,那该多好,就算是需要我再写一些辅助符号也行

    Sub Process_Globals

    Dim a As mylib
    End Sub

    #If java
    //packname jcp.b4a.thirdlib.main;
    public static class mylib{
    public static int cc=15;
    int pos;
    public static int jia(int a,int b){
    return (a+b);
    #End If

    Dim a As mylib,this time can't use
    if i can use inline java class mylib like a library,This will be very convenient

    Attached Files:

  6. xiaoyao

    xiaoyao Member Licensed User

    This is just my suggestion, may have difficulty is big, if not easy to do, when you are a joke to skip over
    The line.
    B4a can be inserted into JAVA code is programmed, this is nice. But now do is for inserting a
    Equivalent code, also built a JAVA file, together with the generated JAVA file when compiling the.
    But to write code in the B4A IDE, B4A of this code doesn't know, still need to use reflection method to
    Access, efficiency way reflection does discount, in addition to write the code is relatively inconvenient. I say this for
    Inline Java is not the most important, the most important thing is if I referred to a JAR file, can be in the absence of XML
    Under the condition of the documents, direct calls to his methods and variables, and this is the most convenient.
    #AdditionalJar I quoted several JAR, but in B4A programming, I don't see any hint of these JAR
    'JavaObject.jar, JavaObject.xml
    If I write this code
    #AdditionalJar JavaObject
    if JavaObject.jar didn't have JavaObject.xml,i write "Dim OO As JavaObject,", "oo.***" auto will be show ,it's good think.


    但是在B4A的IDE中编写代码时,B4A对这段代码却根本不认识,还需要用反射的方法去访问,一来反射的效率确实会打折扣,另外写代码也比较不方便。我说这些呢对于inline java倒不是最重要的,最重要的是如果我引用了一个JAR文件,能够在没有XML文件的情况下,直接调用他的方法和变量,这才是最方便的。
    #AdditionalJar 我引用了几个JAR,但是在B4A编程时,我看不到这些JAR的任何提示

    #AdditionalJar JavaObject

    Attached Files:

  7. xiaoyao

    xiaoyao Member Licensed User

    Maybe I think too much, you can ignore, thanks, sometimes I will indulge in the wildest fantasy, always think too perfect

    To achieve some of the features by using the method of JAVA reflection, in fact, performance or the impact is big.
    If only some simple call, indeed is not a big problem, if used as game development, image processing and high strength CPU operation, GPU operation, if only need to call the method of tens of thousands of times in Jar
    The use of reflection methods will make the speed is greatly reduced, the relevant reflection effect the performance of the article I just read a lot of articles.
    In fact, I mean if you can put the principle of SLC compile tools binding to JAVA INLINE for good.
    Because B4A want to develop the high speed, more powerful, really cannot do without in addition to write JAVA, generate JAR or inline mode.
    But if the generated JAR are used to reflect the method call in B4A, the application of this link will be on the development of performance decrease caused by.
    I seek is a method without reflection can and B4A interworking.



    其实我的意思是如果能把SLC编绎工具的原理结合到JAVA INLINE里来就好了。
  8. xiaoyao

    xiaoyao Member Licensed User

    If using reflection way, want the code confusion so that the program will not be easy to reverse engineer on, it may cause program error, because the confusion after the JAVA code, the name of the original method into disordered letters, reflection is used to string the way to obtain a process
    Confusion will not bring disorder letter string has become a new method of the corresponding name.

  9. xiaoyao

    xiaoyao Member Licensed User

    if write a java class: getclass info for get "*.jar" file's class methohd
    Class newClass1=Class.forName("xiaoyao.NewClass");
    so i can get all method only use 2 seconds
    (getclassinfo usedtime=2705446 nanoTime)

    it's a method for make xml file for one jar have no "xml file",if b4a can work every jar ,we will auto create xml file by Basic4android.exe.

    This is a convenient tool to use B4A, because a lot of the time we get a ready-made JAR file, but no XML file.
    This is very useful for the development of some website, SDK, that we do not need to write an interface and other multi generates a JAR to B4A using JAVA.
    My biggest hope is in use, write java inline, B4A can automatically extract the JAVA code, to generate a temporary XML files for the application of the use of special library.
    Like main.jar.mylib.xml, I mean, can use the Java inline code as a virtual JAR.
    The purpose is to B4A can directly read the code method and attribute etc..
    I said so much, mainly because if you add some not equipped with the XML file of the JAR library, or directly in the B4A to write JAVA code,
    B4A has no way to display method inside the JAR, I'm XML method to the invention of a dynamic, facilitate the development of staff.

    long v1=System.nanoTime();
    Class newClass1=Class.forName("xiaoyao.NewClass");
    String info=GetClassDefinition(newClass1);
    long v2=System.nanoTime();
    System.out.println("getclassinfo usedtime=" + ( v2-v1));

    public xiaoyao.NewClass extends java.lang.Object
    public int a1=0;
    private static int a2=0;
    private static int a3=0;
    private boolean a4=false;
    public static java.lang.Object obj1=xiaoyao.NewClass2@1cb20da;
    private static long c1=555;
    private static final long c2=555;
    final long c3=555;
    private static final long c4=0;
    public java.lang.String str1=
    public xiaoyao.NewClass();
    public static void main([Ljava.lang.String; arg1);
    public static java.lang.String test2([B arg1);
    public static java.lang.String test3([[B arg1);
    public static void sub1();
    private int jia(int arg1, int arg2);
    private int jia2(xiaoyao.NewClass2 arg1);

    我最大的用途是,希望在写 java inline时,B4A能够自动提取该段JAVA代码,生成一个临时的XML库文件专门给本应用使用。
    如main.jar.mylib.xml,我的意思是可以把这段java inline的代码当成一个虚拟JAR.

  10. xiaoyao

    xiaoyao Member Licensed User

    the java class code:

    package xiaoyao;
    import xiaoyao.NewClass2;
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.

    * @author xiaoyao
    public class NewClass {

    public int a1=0;
    private static int a2;
    private static int a3;
    private boolean a4;
    public static Object obj1=new NewClass2();
    //public static Object obj2=null;

    private static long c1=555;
    final private static long c2=555;
    final long c3=555;
    private static final long c4=0;
    public String str1="\nload";

    public static String test2 (byte[] bytes){
    return null;
    public static String test3 (byte[][] bytes){
    return null;
    public static void main(String[] args){

    public static void sub1(){
    private int jia(int a1,int a2){
    return (a1+a2);
    private int jia2(NewClass2 NewClass2OBJ){
    return 3;
  11. xiaoyao

    xiaoyao Member Licensed User

    use this code ,IDE CAN quick make java code str to a "*.class" file,so it can zip to a jar file.
    Of course, we had better be not to generate this JAR, but only read a JAVA source inside and public variables, generates a virtual JAR files and XML files, but these can not generate the file on the hard disk, the process variable parameter in the b4a ide by direct memory recognition in this block of JAVA code this is very simple.

    JAVA dynamic compile java code changes, CLASS files, dynamic access to JAVA string in the process people were variable etc.

    JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();

    StandardJavaFileManager javaFileManager = null;
    try {
    javaFileManager = javaCompiler.getStandardFileManager(null, null, null);
    Iterable it = javaFileManager.getJavaFileObjects(new File(dir, ""));
    CompilationTask task = javaCompiler.getTask(null,
    javaFileManager, null, Arrays.asList("-d", "./src"), null, it);

    当然我们最好是不生成这个JAR,而只是把一段JAVA源码里面的过程和公共变量读出来,生成一个虚拟的JAR文件和XML文件,其实这些完全可以不用生成硬盘上的文件,在b4a ide中由内存直接识别这段JAVA代码的过程变量等参数,这是非常简单的。

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice