aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlavo <zjx001202@126.com>2018-04-11 14:38:54 +0800
committerGlavo <zjx001202@126.com>2018-04-11 14:38:54 +0800
commit5873deb7af12aa2b84221e9e12e0dc7ca5807f0a (patch)
tree2ec69a3c1ff7a98eeba212383c583e8ac7e25fba
parent3a070a3cbb52f8b8d23fe152db00530e95cfa1c1 (diff)
downloadgjavah-5873deb7af12aa2b84221e9e12e0dc7ca5807f0a.zip
gjavah-5873deb7af12aa2b84221e9e12e0dc7ca5807f0a.tar.gz
gjavah-5873deb7af12aa2b84221e9e12e0dc7ca5807f0a.tar.bz2
update0.1.0
-rw-r--r--.gitignore19
-rw-r--r--README.md1
-rw-r--r--build.gradle35
-rw-r--r--gradle/wrapper/gradle-wrapper.jarbin0 -> 54329 bytes
-rw-r--r--gradle/wrapper/gradle-wrapper.properties5
-rwxr-xr-xgradlew172
-rw-r--r--gradlew.bat84
-rw-r--r--out/production/classes/org/glavo/javah/Generator.classbin0 -> 2275 bytes
-rw-r--r--out/production/classes/org/glavo/javah/HeaderGenerator.classbin0 -> 6322 bytes
-rw-r--r--out/production/classes/org/glavo/javah/MethodDesc.classbin0 -> 444 bytes
-rw-r--r--settings.gradle2
-rw-r--r--src/main/java/org/glavo/javah/HeaderGenerator.java209
-rw-r--r--src/test/java/org/glavo/javah/HeaderGeneratorTest.java14
13 files changed, 541 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..dca3b96
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,19 @@
+# Created by .ignore support plugin (hsz.mobi)
+### Gradle template
+.gradle
+/build/
+
+# Ignore Gradle GUI config
+gradle-app.setting
+
+# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
+!gradle-wrapper.jar
+
+# Cache of project
+.gradletasknamecache
+
+# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898
+# gradle/wrapper/gradle-wrapper.properties
+
+*.iml
+.idea/ \ No newline at end of file
diff --git a/README.md b/README.md
index 562fa82..54ea893 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,2 @@
# gjavah
+
diff --git a/build.gradle b/build.gradle
new file mode 100644
index 0000000..8cc9f05
--- /dev/null
+++ b/build.gradle
@@ -0,0 +1,35 @@
+group 'org.glavo'
+version '0.1.0'
+
+apply plugin: 'java'
+
+sourceCompatibility = 1.6
+targetCompatibility = 1.6
+
+repositories {
+ mavenCentral()
+}
+
+dependencies {
+ compile group: 'org.ow2.asm', name: 'asm', version: '6.1.1'
+
+ testCompile group: 'junit', name: 'junit', version: '4.12'
+}
+
+jar {
+ manifest {
+ attributes 'Implementation-Version': '1.2',
+ 'Main-Class': 'org.glavo.javah.HeaderGenerator'
+ }
+}
+
+task fatJar(type: Jar) {
+ baseName = project.name + '-all'
+ description = 'Assembles a jar archive containing the main classes and all the dependencies.'
+ from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
+ manifest {
+ attributes 'Implementation-Version': '1.2',
+ 'Main-Class': 'org.glavo.javah.HeaderGenerator'
+ }
+ with jar
+} \ No newline at end of file
diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar
new file mode 100644
index 0000000..01b8bf6
--- /dev/null
+++ b/gradle/wrapper/gradle-wrapper.jar
Binary files differ
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
new file mode 100644
index 0000000..933b647
--- /dev/null
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -0,0 +1,5 @@
+distributionBase=GRADLE_USER_HOME
+distributionPath=wrapper/dists
+zipStoreBase=GRADLE_USER_HOME
+zipStorePath=wrapper/dists
+distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip
diff --git a/gradlew b/gradlew
new file mode 100755
index 0000000..cccdd3d
--- /dev/null
+++ b/gradlew
@@ -0,0 +1,172 @@
+#!/usr/bin/env sh
+
+##############################################################################
+##
+## Gradle start up script for UN*X
+##
+##############################################################################
+
+# Attempt to set APP_HOME
+# Resolve links: $0 may be a link
+PRG="$0"
+# Need this for relative symlinks.
+while [ -h "$PRG" ] ; do
+ ls=`ls -ld "$PRG"`
+ link=`expr "$ls" : '.*-> \(.*\)$'`
+ if expr "$link" : '/.*' > /dev/null; then
+ PRG="$link"
+ else
+ PRG=`dirname "$PRG"`"/$link"
+ fi
+done
+SAVED="`pwd`"
+cd "`dirname \"$PRG\"`/" >/dev/null
+APP_HOME="`pwd -P`"
+cd "$SAVED" >/dev/null
+
+APP_NAME="Gradle"
+APP_BASE_NAME=`basename "$0"`
+
+# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+DEFAULT_JVM_OPTS=""
+
+# Use the maximum available, or set MAX_FD != -1 to use that value.
+MAX_FD="maximum"
+
+warn () {
+ echo "$*"
+}
+
+die () {
+ echo
+ echo "$*"
+ echo
+ exit 1
+}
+
+# OS specific support (must be 'true' or 'false').
+cygwin=false
+msys=false
+darwin=false
+nonstop=false
+case "`uname`" in
+ CYGWIN* )
+ cygwin=true
+ ;;
+ Darwin* )
+ darwin=true
+ ;;
+ MINGW* )
+ msys=true
+ ;;
+ NONSTOP* )
+ nonstop=true
+ ;;
+esac
+
+CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
+
+# Determine the Java command to use to start the JVM.
+if [ -n "$JAVA_HOME" ] ; then
+ if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
+ # IBM's JDK on AIX uses strange locations for the executables
+ JAVACMD="$JAVA_HOME/jre/sh/java"
+ else
+ JAVACMD="$JAVA_HOME/bin/java"
+ fi
+ if [ ! -x "$JAVACMD" ] ; then
+ die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+ fi
+else
+ JAVACMD="java"
+ which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+fi
+
+# Increase the maximum file descriptors if we can.
+if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
+ MAX_FD_LIMIT=`ulimit -H -n`
+ if [ $? -eq 0 ] ; then
+ if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
+ MAX_FD="$MAX_FD_LIMIT"
+ fi
+ ulimit -n $MAX_FD
+ if [ $? -ne 0 ] ; then
+ warn "Could not set maximum file descriptor limit: $MAX_FD"
+ fi
+ else
+ warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
+ fi
+fi
+
+# For Darwin, add options to specify how the application appears in the dock
+if $darwin; then
+ GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
+fi
+
+# For Cygwin, switch paths to Windows format before running java
+if $cygwin ; then
+ APP_HOME=`cygpath --path --mixed "$APP_HOME"`
+ CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
+ JAVACMD=`cygpath --unix "$JAVACMD"`
+
+ # We build the pattern for arguments to be converted via cygpath
+ ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
+ SEP=""
+ for dir in $ROOTDIRSRAW ; do
+ ROOTDIRS="$ROOTDIRS$SEP$dir"
+ SEP="|"
+ done
+ OURCYGPATTERN="(^($ROOTDIRS))"
+ # Add a user-defined pattern to the cygpath arguments
+ if [ "$GRADLE_CYGPATTERN" != "" ] ; then
+ OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
+ fi
+ # Now convert the arguments - kludge to limit ourselves to /bin/sh
+ i=0
+ for arg in "$@" ; do
+ CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
+ CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
+
+ if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
+ eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
+ else
+ eval `echo args$i`="\"$arg\""
+ fi
+ i=$((i+1))
+ done
+ case $i in
+ (0) set -- ;;
+ (1) set -- "$args0" ;;
+ (2) set -- "$args0" "$args1" ;;
+ (3) set -- "$args0" "$args1" "$args2" ;;
+ (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
+ (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
+ (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
+ (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
+ (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
+ (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
+ esac
+fi
+
+# Escape application args
+save () {
+ for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
+ echo " "
+}
+APP_ARGS=$(save "$@")
+
+# Collect all arguments for the java command, following the shell quoting and substitution rules
+eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
+
+# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
+if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
+ cd "$(dirname "$0")"
+fi
+
+exec "$JAVACMD" "$@"
diff --git a/gradlew.bat b/gradlew.bat
new file mode 100644
index 0000000..e95643d
--- /dev/null
+++ b/gradlew.bat
@@ -0,0 +1,84 @@
+@if "%DEBUG%" == "" @echo off
+@rem ##########################################################################
+@rem
+@rem Gradle startup script for Windows
+@rem
+@rem ##########################################################################
+
+@rem Set local scope for the variables with windows NT shell
+if "%OS%"=="Windows_NT" setlocal
+
+set DIRNAME=%~dp0
+if "%DIRNAME%" == "" set DIRNAME=.
+set APP_BASE_NAME=%~n0
+set APP_HOME=%DIRNAME%
+
+@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+set DEFAULT_JVM_OPTS=
+
+@rem Find java.exe
+if defined JAVA_HOME goto findJavaFromJavaHome
+
+set JAVA_EXE=java.exe
+%JAVA_EXE% -version >NUL 2>&1
+if "%ERRORLEVEL%" == "0" goto init
+
+echo.
+echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:findJavaFromJavaHome
+set JAVA_HOME=%JAVA_HOME:"=%
+set JAVA_EXE=%JAVA_HOME%/bin/java.exe
+
+if exist "%JAVA_EXE%" goto init
+
+echo.
+echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:init
+@rem Get command-line arguments, handling Windows variants
+
+if not "%OS%" == "Windows_NT" goto win9xME_args
+
+:win9xME_args
+@rem Slurp the command line arguments.
+set CMD_LINE_ARGS=
+set _SKIP=2
+
+:win9xME_args_slurp
+if "x%~1" == "x" goto execute
+
+set CMD_LINE_ARGS=%*
+
+:execute
+@rem Setup the command line
+
+set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
+
+@rem Execute Gradle
+"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
+
+:end
+@rem End local scope for the variables with windows NT shell
+if "%ERRORLEVEL%"=="0" goto mainEnd
+
+:fail
+rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
+rem the _cmd.exe /c_ return code!
+if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
+exit /b 1
+
+:mainEnd
+if "%OS%"=="Windows_NT" endlocal
+
+:omega
diff --git a/out/production/classes/org/glavo/javah/Generator.class b/out/production/classes/org/glavo/javah/Generator.class
new file mode 100644
index 0000000..cc55414
--- /dev/null
+++ b/out/production/classes/org/glavo/javah/Generator.class
Binary files differ
diff --git a/out/production/classes/org/glavo/javah/HeaderGenerator.class b/out/production/classes/org/glavo/javah/HeaderGenerator.class
new file mode 100644
index 0000000..e59a371
--- /dev/null
+++ b/out/production/classes/org/glavo/javah/HeaderGenerator.class
Binary files differ
diff --git a/out/production/classes/org/glavo/javah/MethodDesc.class b/out/production/classes/org/glavo/javah/MethodDesc.class
new file mode 100644
index 0000000..bdbf58d
--- /dev/null
+++ b/out/production/classes/org/glavo/javah/MethodDesc.class
Binary files differ
diff --git a/settings.gradle b/settings.gradle
new file mode 100644
index 0000000..55c044f
--- /dev/null
+++ b/settings.gradle
@@ -0,0 +1,2 @@
+rootProject.name = 'gjavah'
+
diff --git a/src/main/java/org/glavo/javah/HeaderGenerator.java b/src/main/java/org/glavo/javah/HeaderGenerator.java
new file mode 100644
index 0000000..368b2d5
--- /dev/null
+++ b/src/main/java/org/glavo/javah/HeaderGenerator.java
@@ -0,0 +1,209 @@
+package org.glavo.javah;
+
+import org.objectweb.asm.*;
+
+import java.io.*;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.Map;
+import java.util.Set;
+
+public final class HeaderGenerator {
+ private static String escape(String source) {
+ StringBuilder builder = new StringBuilder();
+ char ch;
+ for (int i = 0; i < source.length(); i++) {
+ switch (ch = source.charAt(i)) {
+ case '_':
+ builder.append("_1");
+ break;
+ case ';':
+ builder.append("_2");
+ break;
+ case '[':
+ builder.append("_3");
+ break;
+ case '/':
+ builder.append('.');
+ break;
+ default:
+ if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
+ builder.append(ch);
+ } else {
+ builder.append("_0").append(String.format("%04x", (int) ch));
+ }
+ }
+ }
+ return builder.toString();
+ }
+
+ private static String typeToNative(Type tpe) {
+ if (tpe == Type.BOOLEAN_TYPE) {
+ return "jboolean";
+ } else if (tpe == Type.BYTE_TYPE) {
+ return "jbyte";
+ } else if (tpe == Type.CHAR_TYPE) {
+ return "jchar";
+ } else if (tpe == Type.SHORT_TYPE) {
+ return "jshort";
+ } else if (tpe == Type.INT_TYPE) {
+ return "jint";
+ } else if (tpe == Type.LONG_TYPE) {
+ return "jlong";
+ } else if (tpe == Type.FLOAT_TYPE) {
+ return "jfloat";
+ } else if (tpe == Type.DOUBLE_TYPE) {
+ return "jdouble";
+ } else if (tpe == Type.VOID_TYPE) {
+ return "void";
+ } else {
+ String str = tpe.toString();
+ if (str.startsWith("[")) {
+ str = typeToNative(tpe.getElementType());
+ if ("jclass".equals(str)) {
+ return "jobjectArray";
+ } else {
+ return str + "Array";
+ }
+ }
+ return "jobject"; //TODO: jthrowable
+ }
+ }
+
+ public static void generateHeader(ClassReader reader, PrintWriter output) {
+ output.println("/* DO NOT EDIT THIS FILE - it is machine generated */");
+ output.println("#include <jni.h>");
+
+ Generator generator = new Generator();
+ reader.accept(generator, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
+
+ StringBuilder builder = new StringBuilder();
+ String className = escape(generator.getClassName());
+
+ output.println("/* Header for class " + className + " */");
+
+ String includeHeader = "_Include_" + className;
+ output.println("#ifndef " + includeHeader);
+ output.println("#define " + includeHeader);
+
+ output.println("#ifdef __cplusplus\n" +
+ "extern \"C\" {\n" +
+ "#endif");
+
+ for (Map.Entry<String, Set<MethodDesc>> entry : generator.getMethods().entrySet()) {
+ boolean overload = entry.getValue().size() > 1;
+ for (MethodDesc desc : entry.getValue()) {
+ String methodName = escape(entry.getKey());
+ output.println("/*" + "\n" +
+ " * Class: " + className + "\n" +
+ " * Method: " + entry.getKey() + "\n" +
+ " * Signature: " + desc.descriptor + "\n" +
+ " */"
+ );
+
+ Type[] argTypes = Type.getArgumentTypes(desc.descriptor);
+ Type retType = Type.getReturnType(desc.descriptor);
+
+ output.print(
+ "JNIEXPORT " + typeToNative(retType) + " JNICALL Java_" + className + "_" + methodName
+ );
+
+ if (overload) {
+ output.print("__");
+ for (Type tpe : argTypes) {
+ output.print(escape(tpe.toString()));
+ }
+ }
+ output.println();
+
+ output.print(" (JNIEnv *, ");
+ if (desc.isStatic) {
+ output.print("jclass");
+ } else {
+ output.print("jobject");
+ }
+ for (Type tpe : argTypes) {
+ output.print(", " + typeToNative(tpe));
+ }
+ output.println(");\n");
+ }
+
+ }
+
+ output.println("#ifdef __cplusplus\n" +
+ "}\n" +
+ "#endif\n" +
+ "#endif\n");
+
+ }
+
+ public static void generateHeader(byte[] classFile, PrintWriter output) {
+ generateHeader(new ClassReader(classFile), output);
+ }
+
+ public static void generateHeader(byte[] classFileBuffer, int classFileOffset, int classFileLength, PrintWriter output) {
+ generateHeader(new ClassReader(classFileBuffer, classFileOffset, classFileLength), output);
+ }
+
+ public static void generateHeader(String className, PrintWriter output) throws IOException {
+ generateHeader(new ClassReader(className), output);
+ }
+
+ public static void generateHeader(InputStream input, PrintWriter output) throws IOException {
+ generateHeader(new ClassReader(input), output);
+ }
+
+ public static void main(String[] args) throws IOException {
+ File f = new File(args[0]);
+ PrintWriter writer = new PrintWriter(System.out);
+ generateHeader(new FileInputStream(f), writer);
+ writer.flush();
+ }
+}
+
+class MethodDesc {
+ public final boolean isStatic;
+ public final String descriptor;
+
+ public MethodDesc(boolean isStatic, String descriptor) {
+ this.isStatic = isStatic;
+ this.descriptor = descriptor;
+ }
+}
+
+class Generator extends ClassVisitor {
+ private String className;
+
+ private Map<String, Set<MethodDesc>> methods = new LinkedHashMap<String, Set<MethodDesc>>();
+
+ public Generator() {
+ super(Opcodes.ASM5);
+ }
+
+ @Override
+ public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
+ className = name;
+ }
+
+ @Override
+ public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
+ if ((access & Opcodes.ACC_NATIVE) != 0) {
+ if (methods.containsKey(name)) {
+ methods.get(name).add(new MethodDesc((access & Opcodes.ACC_STATIC) != 0, descriptor));
+ } else {
+ LinkedHashSet<MethodDesc> set = new LinkedHashSet<MethodDesc>();
+ set.add(new MethodDesc((access & Opcodes.ACC_STATIC) != 0, descriptor));
+ methods.put(name, set);
+ }
+ }
+ return null;
+ }
+
+ public String getClassName() {
+ return className;
+ }
+
+ public Map<String, Set<MethodDesc>> getMethods() {
+ return methods;
+ }
+} \ No newline at end of file
diff --git a/src/test/java/org/glavo/javah/HeaderGeneratorTest.java b/src/test/java/org/glavo/javah/HeaderGeneratorTest.java
new file mode 100644
index 0000000..97e3e33
--- /dev/null
+++ b/src/test/java/org/glavo/javah/HeaderGeneratorTest.java
@@ -0,0 +1,14 @@
+package org.glavo.javah;
+
+import org.junit.Test;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+public class HeaderGeneratorTest {
+ public static void main(String[] args) throws IOException {
+ PrintWriter writer = new PrintWriter(System.out);
+ HeaderGenerator.generateHeader("java.lang.System", writer);
+ writer.flush();
+ }
+}