Skip to content

Commit

Permalink
Add draft version.
Browse files Browse the repository at this point in the history
Signed-off-by: yatanokarasu <[email protected]>
  • Loading branch information
yatanokarasu committed Dec 29, 2017
1 parent 57150d1 commit 51b5caa
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
39 changes: 39 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sniffer4j</groupId>
<artifactId>sniffer4j</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.javassist/javassist -->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.22.0-GA</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>
src/main/resources/META-INF/MANIFEST.MF
</manifestFile>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
110 changes: 110 additions & 0 deletions src/main/java/io/sniffer4j/Premain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Yusuke TAKEI.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.sniffer4j;


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.instrument.Instrumentation;
import java.security.ProtectionDomain;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.expr.ExprEditor;


/**
*
*/
public final class Premain {

private static final ClassPool CLASS_POOL = ClassPool.getDefault();

/**
* @param agentArguments Java Agent arguments
* @param instrumentation An instrumentation instance for pre-main
*/
public static void premain(final String agentArguments, final Instrumentation instrumentation) {
instrumentation.addTransformer(new ClassFileTransformer() {

/**
* @see java.lang.instrument.ClassFileTransformer#transform(java.lang.Module,
* java.lang.ClassLoader, java.lang.String, java.lang.Class,
* java.security.ProtectionDomain, byte[])
*/
@Override
public byte[] transform(Module module, ClassLoader loader, String fullyClassname, Class<?> arg3, ProtectionDomain protectionDomain,
byte[] classfileByteSequence)
throws IllegalClassFormatException {
return this.transform(loader, fullyClassname, arg3, protectionDomain, classfileByteSequence);
}


/**
* @see java.lang.instrument.ClassFileTransformer#transform(java.lang.ClassLoader,
* java.lang.String, java.lang.Class, java.security.ProtectionDomain, byte[])
*/
@Override
public byte[] transform(ClassLoader loader, String fullyClassname, Class<?> arg3, ProtectionDomain protectionDomain, byte[] classfileByteSequence)
throws IllegalClassFormatException {
if (!accept(fullyClassname)) {
return null;
}

try (final InputStream byteStream = new ByteArrayInputStream(classfileByteSequence)) {
CtClass ctClass = CLASS_POOL.makeClass(byteStream);
CtMethod[] ctMethods = ctClass.getDeclaredMethods();

final String className = ctClass.getName();

for (final CtMethod aMethod : ctMethods) {
final String methodName = aMethod.getName();
final String varName = methodName + "_sniff_start";
aMethod.addLocalVariable(varName, CtClass.longType);
aMethod.insertBefore(varName + " = System.nanoTime();");
aMethod.insertAfter("System.out.println(\"" + className + "#" + methodName + "()[Thread=\" + Thread.currentThread().getName() + \"]: \" + (System.nanoTime() - " + varName + "));");
}

return ctClass.toBytecode();
} catch (IOException | CannotCompileException cause) {
IllegalClassFormatException e = new IllegalClassFormatException();
e.initCause(cause);

throw e;
}
}


private boolean accept(String fullyClassname) {
return fullyClassname.startsWith("io");
}
});
}

}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Premain-Class: io.sniffer4j.Premain

0 comments on commit 51b5caa

Please sign in to comment.