Skip to content

Commit

Permalink
Merge pull request #61 from ddyblackhat/master
Browse files Browse the repository at this point in the history
jvm部分数据结构作业
  • Loading branch information
844028312 authored May 7, 2017
2 parents bdd071c + a9f7b1d commit 3b92533
Show file tree
Hide file tree
Showing 107 changed files with 3,466 additions and 55 deletions.
32 changes: 32 additions & 0 deletions group04/1796244932/learn01/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,38 @@
<version>4.0.0.Alpha8</version>
</dependency>


<!-- https://mvnrepository.com/artifact/javassist/javassist -->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>


<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>



</dependencies>


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.dudy.learn01.base.aop.DynamicProxy;

/**
* Created by dudy on 2017/3/22.
*/
public class Business implements IBusiness,IBusiness2{
@Override
public void dosomething() {
System.out.println("dosomething.....");
}

@Override
public void dosomething2() {
System.out.println("dosomething2.....");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.dudy.learn01.base.aop.DynamicProxy;

import net.sf.cglib.proxy.Enhancer;

/**
* Created by dudy on 2017/3/22.
*/
public class CGlibDemo {

public static void main(String[] args) {
byteCodeGe();
}

public static void byteCodeGe(){
//创建一个织入器
Enhancer enhancer = new Enhancer();
// 设置父类
enhancer.setSuperclass(Business.class);
// 设置需要织入的逻辑
enhancer.setCallback(new LogIntercept());
//使用织入器创建子类
IBusiness2 newBusiness = (IBusiness2) enhancer.create();
newBusiness.dosomething2();

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.dudy.learn01.base.aop.DynamicProxy;
import java.lang.reflect.Proxy;

/**
* Created by dudy on 2017/3/22.
*/
public class DynamicProxyDemo {


public static void main(String[] args) {

// 需要代理的接口,被代理类实现的多个接口都必须放在这里
Class[] proxyInterface = new Class[]{IBusiness.class,IBusiness2.class};
// 构建AOP的Advice,这里需要传入业务类的实例
LogInvocationHandler handler = new LogInvocationHandler(new Business());
//生成代理类的字节码加载器
ClassLoader loader = DynamicProxyDemo.class.getClassLoader();
//织入器,织入代码并生成代理类
IBusiness2 proxyBusiness = (IBusiness2) Proxy.newProxyInstance(loader, proxyInterface, handler);

proxyBusiness.dosomething2();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.dudy.learn01.base.aop.DynamicProxy;

/**
* Created by dudy on 2017/3/22.
*/
public interface IBusiness {
void dosomething();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.dudy.learn01.base.aop.DynamicProxy;

/**
* Created by dudy on 2017/3/22.
*/
public interface IBusiness2 {

public void dosomething2();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.dudy.learn01.base.aop.DynamicProxy;

import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
* Created by dudy on 2017/3/22.
*/
public class LogIntercept implements MethodInterceptor {
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy proxy) throws Throwable {
//执行原有的逻辑,注意这里是invokeSuper
Object rev = proxy.invokeSuper(o, objects);
//执行织入的日志
if (method.getName().equals("dosomething2")){
System.out.println("CGlib dosometing2.....");
}


return rev;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.dudy.learn01.base.aop.DynamicProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
* Created by dudy on 2017/3/22.
* 打印日志的切面
*/
public class LogInvocationHandler implements InvocationHandler{

private Object targer; //目标对象

public LogInvocationHandler(Object targer) {
this.targer = targer;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

// 执行原有逻辑
Object rev = method.invoke(targer, args);
// 执行织入的日志,你可以控制哪些哪些方法执行切入逻辑
if (method.getName().equals("dosomething2")){
System.out.println("记录日志");
}

return rev;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## AOP 实现机制

### AOP各种实现
aop就是面向切面编程, 可以从几个层面来实现AOP
编译期
字节码加载前
字节码加载后
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dudy.learn01.juc;
package com.dudy.learn01.base.juc;

/**
* Created by dudy on 2017/3/9.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dudy.learn01.download;
package com.dudy.learn01.coderising.download;

import com.dudy.learn01.download.api.Connection;

import com.dudy.learn01.coderising.download.api.Connection;

import java.io.*;
import java.util.concurrent.BrokenBarrierException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.dudy.learn01.download;
package com.dudy.learn01.coderising.download;

import com.dudy.learn01.download.api.Connection;
import com.dudy.learn01.download.api.ConnectionManager;
import com.dudy.learn01.download.api.DownloadListener;


import com.dudy.learn01.coderising.download.api.Connection;
import com.dudy.learn01.coderising.download.api.ConnectionManager;
import com.dudy.learn01.coderising.download.api.DownloadListener;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
Expand All @@ -14,9 +16,9 @@ public class FileDownloader {

private String url;

private DownloadListener listener;
private DownloadListener listener;

private ConnectionManager cm;
private ConnectionManager cm;


public FileDownloader(String _url) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dudy.learn01.download.api;
package com.dudy.learn01.coderising.download.api;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dudy.learn01.download.api;
package com.dudy.learn01.coderising.download.api;

public class ConnectionException extends Exception {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dudy.learn01.download.api;
package com.dudy.learn01.coderising.download.api;

import java.io.IOException;
import java.net.MalformedURLException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package com.dudy.learn01.download.api;

package com.dudy.learn01.coderising.download.api;
public interface DownloadListener {
public void notifyFinished();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dudy.learn01.download.impl;
package com.dudy.learn01.coderising.download.impl;

import com.dudy.learn01.download.api.Connection;

import com.dudy.learn01.coderising.download.api.Connection;

import java.io.*;
import java.net.HttpURLConnection;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.dudy.learn01.download.impl;
package com.dudy.learn01.coderising.download.impl;

import com.dudy.learn01.download.api.Connection;
import com.dudy.learn01.download.api.ConnectionException;
import com.dudy.learn01.download.api.ConnectionManager;


import com.dudy.learn01.coderising.download.api.Connection;
import com.dudy.learn01.coderising.download.api.ConnectionException;
import com.dudy.learn01.coderising.download.api.ConnectionManager;

import java.io.IOException;
import java.net.HttpURLConnection;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.dudy.learn01.coderising.jvm.attr;

public abstract class AttributeInfo {
public static final String CODE = "Code";
public static final String CONST_VALUE = "ConstantValue";
public static final String EXCEPTIONS = "Exceptions";
public static final String LINE_NUM_TABLE = "LineNumberTable";
public static final String LOCAL_VAR_TABLE = "LocalVariableTable";
public static final String STACK_MAP_TABLE = "StackMapTable";
int attrNameIndex;
int attrLen ;
public AttributeInfo(int attrNameIndex, int attrLen) {

this.attrNameIndex = attrNameIndex;
this.attrLen = attrLen;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.dudy.learn01.coderising.jvm.attr;


import com.dudy.learn01.coderising.jvm.clz.ClassFile;
import com.dudy.learn01.coderising.jvm.cmd.ByteCodeCommand;
import com.dudy.learn01.coderising.jvm.constant.ConstantPool;
import com.dudy.learn01.coderising.jvm.loader.ByteCodeIterator;

public class CodeAttr extends AttributeInfo {
private int maxStack ;
private int maxLocals ;
private int codeLen ;
private String code;
public String getCode() {
return code;
}

private ByteCodeCommand[] cmds ;
public ByteCodeCommand[] getCmds() {
return cmds;
}
private LineNumberTable lineNumTable;
private LocalVariableTable localVarTable;
private StackMapTable stackMapTable;

public CodeAttr(int attrNameIndex, int attrLen, int maxStack, int maxLocals, int codeLen,String code ,ByteCodeCommand[] cmds) {
super(attrNameIndex, attrLen);
this.maxStack = maxStack;
this.maxLocals = maxLocals;
this.codeLen = codeLen;
this.code = code;
this.cmds = cmds;
}

public void setLineNumberTable(LineNumberTable t) {
this.lineNumTable = t;
}

public void setLocalVariableTable(LocalVariableTable t) {
this.localVarTable = t;
}

public static CodeAttr parse(ClassFile clzFile, ByteCodeIterator iter){

return null;
}


public String toString(ConstantPool pool){
StringBuilder buffer = new StringBuilder();
//buffer.append("Code:").append(code).append("\n");
for(int i=0;i<cmds.length;i++){
buffer.append(cmds[i].toString(pool)).append("\n");
}
buffer.append("\n");
buffer.append(this.lineNumTable.toString());
buffer.append(this.localVarTable.toString(pool));
return buffer.toString();
}
private void setStackMapTable(StackMapTable t) {
this.stackMapTable = t;

}





}
Loading

0 comments on commit 3b92533

Please sign in to comment.