Skip to content

Commit cd4febd

Browse files
committed
设计模式之Factory系列,FactoryMethod, AbstractFactory,以及spring bean工厂开头
1 parent 5d05c58 commit cd4febd

34 files changed

+1337
-371
lines changed

.idea/libraries/Maven__asm_asm_all_3_3_1.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 455 additions & 371 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DesignPatterns.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
<orderEntry type="library" name="Maven: org.aspectj:aspectjweaver:1.9.4" level="project" />
2121
<orderEntry type="library" name="Maven: cglib:cglib:3.2.12" level="project" />
2222
<orderEntry type="library" name="Maven: org.ow2.asm:asm:7.1" level="project" />
23+
<orderEntry type="library" name="Maven: asm:asm-all:3.3.1" level="project" />
2324
</component>
2425
</module>

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@
3131
<version>3.2.12</version>
3232
</dependency>
3333

34+
<!-- https://mvnrepository.com/artifact/asm/asm-all -->
35+
<dependency>
36+
<groupId>asm</groupId>
37+
<artifactId>asm-all</artifactId>
38+
<version>3.3.1</version>
39+
</dependency>
40+
41+
42+
43+
3444
</dependencies>
3545

3646
</project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.mashibing.dp.ASM;
2+
3+
import org.objectweb.asm.ClassReader;
4+
import org.objectweb.asm.ClassVisitor;
5+
import org.objectweb.asm.FieldVisitor;
6+
import org.objectweb.asm.MethodVisitor;
7+
8+
import java.io.IOException;
9+
10+
import static org.objectweb.asm.Opcodes.ASM4;
11+
12+
public class ClassPrinter extends ClassVisitor {
13+
public ClassPrinter() {
14+
super(ASM4);
15+
}
16+
17+
@Override
18+
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
19+
System.out.println(name + " extends " + superName + "{" );
20+
}
21+
22+
@Override
23+
public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) {
24+
System.out.println(" " + name);
25+
return null;
26+
}
27+
28+
@Override
29+
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
30+
System.out.println(" " + name + "()");
31+
return null;
32+
}
33+
34+
@Override
35+
public void visitEnd() {
36+
37+
System.out.println("}");
38+
}
39+
40+
public static void main(String[] args) throws IOException {
41+
ClassPrinter cp = new ClassPrinter();
42+
ClassReader cr = new ClassReader("java.lang.Runnable");
43+
// ClassReader cr = new ClassReader(
44+
// ClassPrinter.class.getClassLoader().getResourceAsStream("com/mashibing/dp/ASM/T1.class"));
45+
46+
47+
cr.accept(cp, 0);
48+
}
49+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.mashibing.dp.ASM;
2+
3+
import org.objectweb.asm.*;
4+
5+
import java.io.File;
6+
import java.io.FileOutputStream;
7+
import java.io.OutputStream;
8+
9+
import static org.objectweb.asm.Opcodes.*;
10+
11+
public class ClassTransformerTest {
12+
public static void main(String[] args) throws Exception {
13+
ClassReader cr = new ClassReader(
14+
ClassPrinter.class.getClassLoader().getResourceAsStream("com/mashibing/dp/ASM/Tank.class"));
15+
16+
ClassWriter cw = new ClassWriter(0);
17+
ClassVisitor cv = new ClassVisitor(ASM4, cw) {
18+
@Override
19+
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
20+
MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions);
21+
//return mv;
22+
return new MethodVisitor(ASM4, mv) {
23+
@Override
24+
public void visitCode() {
25+
visitMethodInsn(INVOKESTATIC, "TimeProxy","before", "()V", false);
26+
super.visitCode();
27+
}
28+
};
29+
}
30+
};
31+
32+
cr.accept(cv, 0);
33+
byte[] b2 = cw.toByteArray();
34+
35+
String path = (String)System.getProperties().get("user.dir");
36+
File f = new File(path + "/com/mashibing/dp/ASM/");
37+
f.mkdirs();
38+
39+
FileOutputStream fos = new FileOutputStream(new File(path + "/com/mashibing/dp/ASM/Tank_0.class"));
40+
fos.write(b2);
41+
fos.flush();
42+
fos.close();
43+
44+
}
45+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.mashibing.dp.ASM;
2+
3+
import org.objectweb.asm.ClassWriter;
4+
5+
import static org.objectweb.asm.Opcodes.*;
6+
7+
public class ClassWriteTest {
8+
public static void main(String[] args) {
9+
ClassWriter cw = new ClassWriter(0);
10+
cw.visit(V1_5, ACC_PUBLIC + ACC_ABSTRACT + ACC_INTERFACE,
11+
"pkg/Comparable", null, "java/lang/Object",
12+
null);
13+
cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "LESS", "I",
14+
null, -1).visitEnd();
15+
cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "EQUAL", "I",
16+
null, 0).visitEnd();
17+
cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "GREATER", "I",
18+
null, 1).visitEnd();
19+
cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "compareTo",
20+
"(Ljava/lang/Object;)I", null, null).visitEnd();
21+
cw.visitEnd();
22+
byte[] b = cw.toByteArray();
23+
24+
MyClassLoader myClassLoader = new MyClassLoader();
25+
Class c = myClassLoader.defineClass("pkg.Comparable", b);
26+
System.out.println(c.getMethods()[0].getName());
27+
}
28+
}
29+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.mashibing.dp.ASM;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
new T1();
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.mashibing.dp.ASM;
2+
3+
class MyClassLoader extends ClassLoader {
4+
public Class defineClass(String name, byte[] b) {
5+
return defineClass(name, b, 0, b.length);
6+
}
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.mashibing.dp.ASM;
2+
3+
/**
4+
* 光标必须位于类体内,View-Show ByteCode
5+
*/
6+
7+
public class T1 {
8+
int i = 0;
9+
public void m() {
10+
int j=1;
11+
}
12+
13+
}

0 commit comments

Comments
 (0)