Skip to content

Commit

Permalink
Optimize codes and update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
T5750 committed Feb 9, 2020
1 parent 804907d commit a731731
Show file tree
Hide file tree
Showing 35 changed files with 287 additions and 97 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
## What's included
A ~ C | D ~ G | H ~ L | M ~ P | Q ~ S | T ~ Z
----|----|----|----|----|----
| [blockchain](blockchain/README.md) | | [Javassist](jdk8/README.md) | [nio](nio/README.md) | [security](security/README.md) | [threads](threads/README.md)
| | | [jdk7](jdk7/README.md) | [patterns](patterns/README.md) | | [utils](utils/README.md)
| [ASM](jdk8/README.md) | | [Javassist](jdk8/README.md) | [nio](nio/README.md) | [security](security/README.md) | [threads](threads/README.md)
| [blockchain](blockchain/README.md) | | [jdk7](jdk7/README.md) | [patterns](patterns/README.md) | | [utils](utils/README.md)
| | | [jdk8](jdk8/README.md) | | |
| | | [jvm](jvm/README.md) | | |

Expand Down
2 changes: 1 addition & 1 deletion doc/source/jdk7/keyword/index.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
keyword
=======
===============================

.. toctree::
:maxdepth: 3
Expand Down
2 changes: 1 addition & 1 deletion doc/source/jdk7/lang/index.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
lang
=======
===============================
`java.lang`

.. toctree::
Expand Down
2 changes: 1 addition & 1 deletion doc/source/jdk7/util/index.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
util
=======
===============================
`java.util`

.. toctree::
Expand Down
4 changes: 4 additions & 0 deletions doc/source/jdk8/instrumentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
## Introduction
[Java Instrumentation API](https://docs.oracle.com/javase/7/docs/api/java/lang/instrument/Instrumentation.html) provides the ability to add byte-code to existing compiled Java classes.

Java Instrumentation will give a demonstration of how powerful Java is. Most importantly, this power can be realized by a developer for innovative means.
- For example using Java instrumentation, we can access a class that is loaded by the Java classloader from the JVM and modify its bytecode by inserting our custom code, all these done at runtime.
- Don’t worry about security, these are governed by the same security context applicable for Java classes and respective classloaders.

## Key Components
- Agent – is a jar file containing agent and transformer class files.
- Agent Class – A java class file, containing a method named `premain`.
Expand Down
17 changes: 3 additions & 14 deletions doc/source/jvm/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ JVM
Java Virtual Machine ( JVM )

.. toctree::
:maxdepth: 2
:maxdepth: 3
:numbered: 0

jmxCollection
Expand All @@ -13,16 +13,5 @@ Java Virtual Machine ( JVM )
jvmJdk7
jvmJdk8
jvmNote
UnderstandingTheJvm
UnderstandingTheJvm02
UnderstandingTheJvm03
UnderstandingTheJvm04
UnderstandingTheJvm05
UnderstandingTheJvm06
UnderstandingTheJvm07
UnderstandingTheJvm08
UnderstandingTheJvm09
UnderstandingTheJvm10
UnderstandingTheJvm11
UnderstandingTheJvm12
UnderstandingTheJvm13
jvms/index
understand-jvm/index
9 changes: 9 additions & 0 deletions doc/source/jvm/jvms/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
JVM Specification
===============================
Java Virtual Machine Specification

.. toctree::
:maxdepth: 3
:numbered: 0

jvms4
126 changes: 126 additions & 0 deletions doc/source/jvm/jvms/jvms4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Chapter 4. The class File Format

## 4.1. The ClassFile Structure
```
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}
```

## 4.3. Descriptors

### 4.3.2. Field Descriptors

FieldType term | Type | Interpretation
---|---|---
`B` | `byte` | signed byte
`C` | `char` | Unicode character code point in the Basic Multilingual Plane, encoded with UTF-16
`D` | `double` | double-precision floating-point value
`F` | `float` | single-precision floating-point value
`I` | `int` | integer
`J` | `long` | long integer
`L` ClassName ; | `reference` | an instance of class ClassName
`S` | `short` | signed short
`Z` | `boolean` | `true` or `false`
`[` | `reference` | one array dimension

## 4.4. The Constant Pool
```
cp_info {
u1 tag;
u1 info[];
}
```

Constant Type | Value
---|---
`CONSTANT_Class` | 7
`CONSTANT_Fieldref` | 9
`CONSTANT_Methodref` | 10
`CONSTANT_InterfaceMethodref` | 11
`CONSTANT_String` | 8
`CONSTANT_Integer` | 3
`CONSTANT_Float` | 4
`CONSTANT_Long` | 5
`CONSTANT_Double` | 6
`CONSTANT_NameAndType` | 12
`CONSTANT_Utf8` | 1
`CONSTANT_MethodHandle` | 15
`CONSTANT_MethodType` | 16
`CONSTANT_InvokeDynamic` | 18

## 4.5. Fields
```
field_info {
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[attributes_count];
}
```

Flag Name | Value | Interpretation
---|---|---
`ACC_PUBLIC` | 0x0001 | Declared `public`; may be accessed from outside its package.
`ACC_PRIVATE` | 0x0002 | Declared `private`; usable only within the defining class.
`ACC_PROTECTED` | 0x0004 | Declared `protected`; may be accessed within subclasses.
`ACC_STATIC` | 0x0008 | Declared `static`.
`ACC_FINAL` | 0x0010 | Declared `final`; never directly assigned to after object construction (JLS §17.5).
`ACC_VOLATILE` | 0x0040 | Declared `volatile`; cannot be cached.
`ACC_TRANSIENT` | 0x0080 | Declared `transient`; not written or read by a persistent object manager.
`ACC_SYNTHETIC` | 0x1000 | Declared synthetic; not present in the source code.
`ACC_ENUM` | 0x4000 | Declared as an element of an `enum`.

## 4.6. Methods
```
method_info {
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[attributes_count];
}
```

Flag Name | Value | Interpretation
---|---|---
`ACC_PUBLIC` | 0x0001 | Declared `public`; may be accessed from outside its package.
`ACC_PRIVATE` | 0x0002 | Declared `private`; accessible only within the defining class.
`ACC_PROTECTED` | 0x0004 | Declared `protected`; may be accessed within subclasses.
`ACC_STATIC` | 0x0008 | Declared `static`.
`ACC_FINAL` | 0x0010 | Declared `final`; must not be overridden (§5.4.5).
`ACC_SYNCHRONIZED` | 0x0020 | Declared `synchronized`; invocation is wrapped by a monitor use.
`ACC_BRIDGE` | 0x0040 | A bridge method, generated by the compiler.
`ACC_VARARGS` | 0x0080 | Declared with variable number of arguments.
`ACC_NATIVE` | 0x0100 | Declared `native`; implemented in a language other than Java.
`ACC_ABSTRACT` | 0x0400 | Declared `abstract`; no implementation is provided.
`ACC_STRICT` | 0x0800 | Declared `strictfp`; floating-point mode is FP-strict.
`ACC_SYNTHETIC` | 0x1000 | Declared synthetic; not present in the source code.

## 4.7. Attributes
```
attribute_info {
u2 attribute_name_index;
u4 attribute_length;
u1 info[attribute_length];
}
```

## References
- [Chapter 4. The class File Format](https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions doc/source/jvm/understand-jvm/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
深入理解Java虚拟机
===============================
深入理解Java虚拟机:JVM高级特性与最佳实践

.. toctree::
:maxdepth: 3
:numbered: 0

UnderstandingTheJvm
UnderstandingTheJvm02
UnderstandingTheJvm03
UnderstandingTheJvm04
UnderstandingTheJvm05
UnderstandingTheJvm06
UnderstandingTheJvm07
UnderstandingTheJvm08
UnderstandingTheJvm09
UnderstandingTheJvm10
UnderstandingTheJvm11
UnderstandingTheJvm12
UnderstandingTheJvm13
4 changes: 2 additions & 2 deletions jdk8/src/main/java/t5750/asm/CustomClassWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import t5750.asm.visitor.AddFieldAdapter;
import t5750.asm.visitor.AddInterfaceAdapter;
import t5750.asm.visitor.PublicizeMethodAdapter;
import t5750.util.Global;

public class CustomClassWriter {
private final static String CLASS_NAME = "java.lang.Integer";
private ClassReader reader;
private ClassWriter writer;
private AddFieldAdapter addFieldAdapter;
Expand All @@ -19,7 +19,7 @@ public class CustomClassWriter {

public CustomClassWriter() {
try {
reader = new ClassReader(CLASS_NAME);
reader = new ClassReader(Global.INTEGER);
writer = new ClassWriter(reader, 0);
} catch (IOException e) {
e.printStackTrace();
Expand Down
6 changes: 0 additions & 6 deletions jdk8/src/main/java/t5750/asm/util/AsmUtil.java

This file was deleted.

2 changes: 1 addition & 1 deletion jdk8/src/main/java/t5750/instrument/Premain.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void premain(String agentArgs, Instrumentation inst) {
// CustomClassWriter cr = new CustomClassWriter(b);
// return cr.addField();
// }
// if (name.equals(AsmUtil.PATH_POINT)) {
// if (name.equals(Global.PATH_POINT)) {
// System.out.println("transform: " + name);
// }
// return b;
Expand Down
7 changes: 5 additions & 2 deletions jdk8/src/main/java/t5750/module/log/util/LogUtil.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package t5750.module.log.util;

import t5750.util.Global;

public class LogUtil {
public static final String METHOD_ANNOTATION = "t5750.module.log.annotation.ImportantLog";
public static final String METHOD_ANNOTATION = Global.T5750
+ ".module.log.annotation.ImportantLog";
public static final String ANNOTATION_ARRAY = "fields";
public static final String PATH_METHOD_ANNOTATION = METHOD_ANNOTATION
.replace(".", "/");
.replace(".", Global.JAVASSIST_SEPARATOR);
/**
* Lt5750/module/log/annotation/ImportantLog;
*/
Expand Down
15 changes: 15 additions & 0 deletions jdk8/src/main/java/t5750/util/FileUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package t5750.util;

import java.io.FileOutputStream;

public class FileUtil {
public static void write(byte[] b, String name) {
try {
FileOutputStream out = new FileOutputStream(name);
out.write(b);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
13 changes: 13 additions & 0 deletions jdk8/src/main/java/t5750/util/Global.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package t5750.util;

public class Global {
public static final String T5750 = "t5750";
/**
* Javassist separator
*/
public static final String JAVASSIST_SEPARATOR = "/";
public static final String POINT = "java.awt.Point";
public static final String PATH_POINT = POINT.replace(".",
Global.JAVASSIST_SEPARATOR);
public static final String INTEGER = "java.lang.Integer";
}
9 changes: 8 additions & 1 deletion jdk8/src/test/java/t5750/asm/CustomClassWriterTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package t5750.asm;

import java.io.File;

import org.junit.Before;
import org.junit.Test;

import t5750.util.FileUtil;
import t5750.util.Global;

/**
* 4. Working With the Event-based ASM API
*/
Expand All @@ -19,7 +24,9 @@ public void setup() {
*/
@Test
public void addField() throws Exception {
customClassWriter.addField();
byte[] b = customClassWriter.addField();
String integerClass = Global.T5750 + File.separator + "Integer.class";
FileUtil.write(b, integerClass);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion jdk8/src/test/java/t5750/javassist/ClassLoaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import t5750.javassist.service.Hello;
import t5750.javassist.service.impl.MyTranslator;
import t5750.javassist.util.JavassistUtil;
import t5750.util.Global;

/**
* 3. Class loader
Expand Down Expand Up @@ -52,7 +53,7 @@ public void javaClassLoader() throws Exception {
public void javassistLoader() throws Throwable {
Loader cl = new Loader(pool);
CtClass ct = pool.get(JavassistUtil.DOMAIN + "Rectangle");
ct.setSuperclass(pool.get(JavassistUtil.POINT));
ct.setSuperclass(pool.get(Global.POINT));
Class c = cl.loadClass(JavassistUtil.DOMAIN + "Rectangle");
Object rect = c.newInstance();
System.out.println(rect.toString());
Expand Down

0 comments on commit a731731

Please sign in to comment.