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

xiaoyao

Member
Licensed User
Longtime 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
NativeMe.InitializeContext
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:
\Objects\src\b4a\example\main.java
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[])
(anywheresoftware.b4a.keywords.Common.Null)))


反射的用途 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. 由于反射允许代码执行一些在正常情况下不被允许的操作(比如访问私有的属性和方法),所以使用反射可能会导致意料之外的副作用--代码有功能上的错误,降低可移植性。反射代码破坏了抽象性,因此当平台发生改变的时候,代码的行为就有可能也随着变化。
 

xiaoyao

Member
Licensed User
Longtime User
inline java call in main.java:
public String FirstMethod() {
return "Hello World!";
}

_s = BA.ObjectToString(_v5.RunMethod("FirstMethod",(Object[])
(anywheresoftware.b4a.keywords.Common.Null)))


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

i think it running will be quickly


What is the purpose of this thread?

You know that inline Java is supported, right?

Inline Java Code
 
Upvote 0

xiaoyao

Member
Licensed User
Longtime 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,我们不用建立XML文档就能调用,那该多好。
如果一个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
 

Attachments

  • b4a use javaclass.JPG
    b4a use javaclass.JPG
    91.8 KB · Views: 179
Upvote 0

xiaoyao

Member
Licensed User
Longtime 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可以插入JAVA代码混和编程,这已经是很好很强大了。但现在做到的只是插入一段代码,相当于是另外建了一个JAVA文件一样,在编绎的时候一起生成JAVA文件。

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


'JavaObject.jar,JavaObject.xml
如果我写了这句代码
#AdditionalJar JavaObject
 

Attachments

  • method.JPG
    method.JPG
    56.8 KB · Views: 177
Upvote 0

xiaoyao

Member
Licensed User
Longtime User
It will not have any effect on the performance.
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.

可能是我想多了,你可以不用理会的,谢了,有时我会异想天开,总是想的太过完美

利用JAVA反射的方法去实现一些功能,其实性能还是影响挺大的。
如果只是进行一些简单的调用,确实问题不大,如果用于像游戏开发,图像处理等高强度CPU运算,GPU运算时,假如每秒需要调用几万次Jar中的方法
那用反射的方法就会使速度大幅降低,这个有关反射影响性能的文章我刚看了很多篇。

其实我的意思是如果能把SLC编绎工具的原理结合到JAVA INLINE里来就好了。
因为B4A想开发出高速,更强大的功能,确实离不开另外去写JAVA,生成JAR或者是inline的方式。
但是如果生成的JAR都是用反射的方法才能在B4A中调用,这个环节就会对开发的应用造成性能减低。
我寻求的是一种不用反射就能和B4A互通的方法。
 
Upvote 0

xiaoyao

Member
Licensed User
Longtime User
It will not have any effect on the performance.

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.

如果用了反射的方式,想要代码混肴使程序不会被轻易反编对,就可能造成程序出错,因为混肴过后的JAVA代码,原有的方法名称都变成无序的字母了,而反射是用字符串的方式去获取某个过程的混肴时并不会把字符串也变成对应的新方法的乱序字母名称。
 
Upvote 0

xiaoyao

Member
Licensed User
Longtime User
It will not have any effect on the performance.
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 this.so 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);
System.out.println(info);
long v2=System.nanoTime();
System.out.println("getclassinfo usedtime=" + ( v2-v1));



public xiaoyao.NewClass extends java.lang.Object
{
//Fields:
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=
load;
//Constructs:
public xiaoyao.NewClass();
//Methods:
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);
}


这只是一个方便B4A工具的用途,因为很多时候我们拿到一个现成的JAR文件,但是没有XML文件。
这对于开发某些网站的SDK时,非常有用,这样我们就不需要另外用JAVA去写一个接口又多生成一个JAR给B4A用。
我最大的用途是,希望在写 java inline时,B4A能够自动提取该段JAVA代码,生成一个临时的XML库文件专门给本应用使用。
如main.jar.mylib.xml,我的意思是可以把这段java inline的代码当成一个虚拟JAR.
目的就是为了B4A能够直接读取到这段代码的方法和属性等。

我说了这么多,主要就是因为如果添加一些没有配备XML文件的JAR库,或者直接在B4A中写的JAVA代码,
B4A没有办法显示JAR内部的方法,我是想发明一种动态的XML方法,方便开发人员。
 
Upvote 0

xiaoyao

Member
Licensed User
Longtime 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;
}
}
 
Upvote 0

xiaoyao

Member
Licensed User
Longtime 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;
System.out.println("go2");
try {
javaFileManager = javaCompiler.getStandardFileManager(null, null, null);
//5.文件管理器根与文件连接起来
Iterable it = javaFileManager.getJavaFileObjects(new File(dir, "Hello.java"));
//6.创建编译的任务
CompilationTask task = javaCompiler.getTask(null,
javaFileManager, null, Arrays.asList("-d", "./src"), null, it);
//执行编译
task.call();
javaFileManager.close();
Test1();
Test2();

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

JAVA动态编绎,java代码变CLASS文件,动态获取JAVA字符串中的过程人共变量等
 
Upvote 0
Top