Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use run code

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use run code". In daily operation, I believe many people have doubts about how to use run code. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use run code". Next, please follow the editor to study!

Reproduce the code with a syntax error:

Text

1. Android knowledge part

The IDE hint is also clear: res's id cannot be applied in switch syntax in library-level module. The reason is that the id of res is not constant.

Note: the same code has no syntax problems in application-level module. So for the id of res, there are constants in application and not constants in library. If some students have read the contents of R, they will find that this is indeed the case:

This is the R file in application:

This is the R file in library:

This appearance leads to a knowledge point of android packaging: resource consolidation in the aapt [1] process [2].

One sentence describes this knowledge point: repetitive resources between different module will be merged and overwritten according to priority. The problems caused by this process have been encountered by many veteran drivers, the resources have been overwritten, and the resources we refer to will always be pointed to the only res. This is definitely not in line with expectations.

Therefore, schemes such as prefixing resource names emerge as the times require.

Why not final?

Let's talk about a question here: what's so special about constants? In the following code, when compiled, you can see what makes the constant special:

Class TestFinal {static final int sInt = 1; void testFinal () {int temp = sInt; System.out.println (temp);}}

The compiled code would look like this:

Public void testFinal () {System.out.println (1);}

You will find that the compiler optimization will inline the constant directly to the code reference. So let's think about this: what would happen if the res in library is also constant?

The constant is inlined, and once the resource is duplicated in the project, it will be overwritten in the packaging process, then the inline constant can no longer be mapped to the real resource, after all, the resource has been overwritten.

That is, it will appear: crash where the resource cannot be found.

It's not the problem caused by final.

The R reference in library is not constant, which means that this usage does not work:

As you can see, annotations are also constant, so this problem has a great impact on our daily life. Wait, wait! Butterknife is the use of annotations, why no problem?

Students who have an in-depth understanding of Butterknife should know that Butterknife has made a special treatment for this situation:

Butterknife's plan

In order to prevent syntax errors in the comments, Butterknife created a class called R2. This class is actually the original copy R, the only difference is that R2 is constant.

It is true that there will be no syntax errors, but we have just analyzed: constant inlining, resource coverage. So once case is satisfied, it is crash. So how does Butterknife circumvent this problem?

Anyone who has seen the findViewById () source code in Butterknife should know that the implementation of Butterknife here is something like this:

Public TestActivity_ViewBinding (T target, View source) {this.target = target; target.parentLayout = Utils.findRequiredViewAsType (source, R.id.test, "field 'parentLayout'", ViewGroup.class)

We can see that there is no constant inline in the code that Butterknife finally typed into the package! So how does it do it?

When you see the students here, you might as well stop and think, how would you solve this problem? Here I would like to talk about what I can think of:

In the ASM phase, the inline code is rewritten into a normal reference to R. Here's the problem: the input to ASM is class, and I can't get a normal reference to R at this time.

What if we continue to advance the process of intervention and put it into the APT stage? I tried it, but it didn't work out. The annotation value obtained in the APT phase is also an inline constant.

This is a bit strange, how does Butterknife do the mapping through inline constants and R references? Looking at the source code of Butterknife, it is found that Butterknife is executed in the APT phase, and the key class is ButterKnifeProcessor [3].

Butterknife gets the reference to R through the api of JCTree, and then changes the inline code back to the reference to R. We will not look at the specific api implementation, interested students can github on their own.

Let's talk about what this JCTree does next.

Second, the principle of compilation

We all know that the code we write every day, which eventually wants to run on the target machine, needs to be compiled into machine code that the target machine can recognize. And those who do this work are called compilers. A general compiler does the following:

The picture is from the second edition of the principles of compilation.

In the implementation of all kinds of source code compilation, we basically abstract a concept: abstract Grammar Tree (AST), in order to make it more convenient in the whole compilation process.

One-sentence explanation Abstract Syntax Tree: an abstract representation of the syntax structure of source code. It represents the syntax structure of the programming language in the form of a tree, and each node on the tree represents a structure in the source code.

We have a rough understanding of the implementation process of the compiler, so how does the compiler implement it? They are implemented in code, of course, and their implementations are often very close to us. Take our java compiler as an example.

When we entered Java, we should all have tried javac. And where is the implementation of this command? It's under the com.sun.tools.javac.Main package in tools.jar in JDK. The core logic is com.sun.tools.javac.main.JavaCompiler.

Here is the realization of how to analyze our source code and how to convert it into class. That's what the compiler is supposed to do in the previous picture.

So what role does JCTree play throughout the compilation process? Bottom line: JCTree is an api-level description of the source code. Or JCTree is the implementation of the syntax tree in the java compilation process.

In other words, through the JCTree-related api, we can access the source structure. It seems abstract to say that we can debug a piece of code to get the meaning of its existence:

Fun main () {val context = Context () val scanner = RScanner () val javaCompiler = JavaCompiler.instance (context) val testJavaCodeFile = File ("/ Users/x/xx/xxx/TestAutoCode.java") ToolProvider .getSystem JavaCompiler () .getStandardFileManager (DiagnosticCollector (), null Null) .getJavaFileObjectsFromFiles (listOf (testJavaCodeFile)) .forEach {javaCompiler.parse (it). Defs.forEach {scanner.scan (it)} class RScanner: TreeScanner () {override fun visitMethodDef (tree: JCTree.JCMethodDecl?) {super.visitMethodDef (tree)}}

Based on this set of api, we can get any information about the source code. And this piece of demo code, only need to import tools.jar to run quickly, the cost is very low.

Third, use the code run code

Above, we compile the java source code dynamically through the example of JavaCompiler, and the result is the class file of the java source code. With the class file, we can load the class through ClassLoader.

With the above foundation, it is no longer important to realize the source code. Please post a link here and take it yourself: How do you dynamically compile and load external java classes? [4]

At this point, the study on "how to use run code" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

  • What is the custom button style in the css3 fillet style

    Css3 fillet style in the custom button style, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something. The code is as follows:

    © 2024 shulou.com SLNews company. All rights reserved.

    12
    Report