Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Apr 2, 2019
2 parents 6c1f140 + 73e0b4f commit c0cfb25
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ void generateGetFields(JNIClass clazz) {
JNIClass superclazz = clazz.getSuperclass();
String clazzName = clazz.getNativeName();
String superName = superclazz.getNativeName();
String Methodname;
if (!superclazz.getName().equals("java.lang.Object")) {
/*
* Windows exception - cannot call get/set function of super class
Expand Down Expand Up @@ -281,28 +282,60 @@ void generateGetFields(JNIClass clazz) {
if (accessor == null || accessor.length() == 0)
accessor = field.getName();
if (type.isPrimitive()) {
output("\tlpStruct->");
output(accessor);
output(" = ");
output(field.getCast());
if( field.isPointer() ) {
if (field.isSharedPointer()) {
output("\tlpStruct->");
output(accessor);
output(" = ");
output("std::shared_ptr");
output("<");
Methodname =field.getCast();
String Method =Methodname.replace("*", ">");
Method =Method.replace("(", "");
Method =Method.replace(")", "");
output(Method);
output("(");
output(field.getCast());
output("(intptr_t)");
if (isCPP) {
output("env->Get");
} else {
output("(*env)->Get");
}
output(type.getTypeSignature1(!type.equals(type64)));
if (isCPP) {
output("Field(lpObject, ");
} else {
output("Field(env, lpObject, ");
}
output(field.getDeclaringClass().getSimpleName());
output("Fc.");
output(field.getName());
output(")");
output(");");
} else {
output("\tlpStruct->");
output(accessor);
output(" = ");
output(field.getCast());
if (field.isPointer()) {
output("(intptr_t)");
}
if (isCPP) {
output("env->Get");
} else {
output("(*env)->Get");
}
output(type.getTypeSignature1(!type.equals(type64)));
if (isCPP) {
output("Field(lpObject, ");
} else {
output("Field(env, lpObject, ");
}
output(field.getDeclaringClass().getSimpleName());
output("Fc.");
output(field.getName());
output(");");
}
if (isCPP) {
output("env->Get");
} else {
output("(*env)->Get");
}
output(type.getTypeSignature1(!type.equals(type64)));
if (isCPP) {
output("Field(lpObject, ");
} else {
output("Field(env, lpObject, ");
}
output(field.getDeclaringClass().getSimpleName());
output("Fc.");
output(field.getName());
output(");");
} else if (type.isArray()) {
JNIType componentType = type.getComponentType(), componentType64 = type64.getComponentType();
if (componentType.isPrimitive()) {
Expand Down Expand Up @@ -455,8 +488,14 @@ void generateSetFields(JNIClass clazz) {
if( field.isPointer() ) {
output("(intptr_t)");
}
output("lpStruct->"+accessor);
output(");");
if (field.isSharedPointer()) {
output("(&");
output("lpStruct->" + accessor);
output("));");
} else {
output("lpStruct->"+accessor);
output(");");
}
} else if (type.isArray()) {
JNIType componentType = type.getComponentType(), componentType64 = type64.getComponentType();
if (componentType.isPrimitive()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ public interface JNIField {
public String getConditional();
public boolean ignore();

public boolean isSharedPointer();
public boolean isPointer();
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ public boolean isPointer() {
return getFlag(POINTER_FIELD) || ( type.getWrappedClass() == Long.TYPE && getCast().endsWith("*)") );
}

public boolean isSharedPointer() {
if( annotation == null ) {
return false;
}
return getFlag(SHARED_PTR) || ( type.getWrappedClass() == Long.TYPE && getCast().endsWith("*") );
}


public String getConditional() {
String parentConditional = getDeclaringClass().getConditional();
String myConditional = annotation == null ? null : emptyFilter(annotation.conditional());
Expand Down
17 changes: 14 additions & 3 deletions hawtjni-runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,20 @@
<artifactId>hawtjni-runtime</artifactId>
<name>HawtJNI Runtime</name>
<description>The API that projects using HawtJNI should build against.</description>

<build>
<plugins>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Automatic-Module-Name>org.fusesource.hawtjni.runtime</Automatic-Module-Name>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
Expand All @@ -42,7 +53,7 @@
</execution>
</executions>
</plugin>
</plugins>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,11 @@ public enum FieldFlag {
* Indicate that the field is a pointer.
*/
POINTER_FIELD,

/**
* Indicate that the field is a shared pointer.
*/

SHARED_PTR,

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.io.*;
import java.lang.reflect.Method;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;
Expand Down Expand Up @@ -80,8 +82,15 @@
*/
public class Library {

public static final String STRATEGY_PROPERTY = "hawtjni.strategy";
public static final String STRATEGY_SHA1 = "sha1";
public static final String STRATEGY_TEMP = "temp";

static final String SLASH = System.getProperty("file.separator");

static final String STRATEGY = System.getProperty(STRATEGY_PROPERTY,
"windows".equals(getOperatingSystem()) ? STRATEGY_SHA1 : STRATEGY_TEMP);

final private String name;
final private String version;
final private ClassLoader classLoader;
Expand Down Expand Up @@ -311,7 +320,12 @@ private boolean extractAndLoad(ArrayList<Throwable> errors, String customPath, S
file(System.getProperty("user.home"), ".hawtjni", name))) {
if( path!=null ) {
// Try to extract it to the custom path...
File target = extract(errors, resource, prefix, suffix, path);
File target;
if (STRATEGY_SHA1.equals(STRATEGY)) {
target = extractSha1(errors, resource, prefix, suffix, path);
} else {
target = extractTemp(errors, resource, prefix, suffix, path);
}
if( target!=null ) {
if( load(errors, target) ) {
nativeLibrarySourceUrl = resource;
Expand Down Expand Up @@ -349,7 +363,81 @@ private String map(String libName) {
return libName;
}

private File extract(ArrayList<Throwable> errors, URL source, String prefix, String suffix, File directory) {
private File extractSha1(ArrayList<Throwable> errors, URL source, String prefix, String suffix, File directory) {
File target = null;
directory = directory.getAbsoluteFile();
if (!directory.exists()) {
if (!directory.mkdirs()) {
errors.add(new IOException("Unable to create directory: " + directory));
return null;
}
}
try {
String sha1 = computeSha1(source.openStream());
String sha1f = "";
target = new File(directory, prefix + sha1 + suffix);

if (target.isFile() && target.canRead()) {
sha1f = computeSha1(new FileInputStream(target));
}
if (sha1f.equals(sha1)) {
return target;
}

FileOutputStream os = null;
InputStream is = null;
try {
is = source.openStream();
if (is != null) {
byte[] buffer = new byte[4096];
os = new FileOutputStream(target);
int read;
while ((read = is.read(buffer)) != -1) {
os.write(buffer, 0, read);
}
chmod755(target);
}
return target;
} finally {
close(os);
close(is);
}
} catch (Throwable e) {
IOException io;
if (target != null) {
target.delete();
io = new IOException("Unable to extract library from " + source + " to " + target);
} else {
io = new IOException("Unable to create temporary file in " + directory);
}
io.initCause(e);
errors.add(io);
}
return null;
}

private String computeSha1(InputStream is) throws NoSuchAlgorithmException, IOException {
String sha1;
try {
MessageDigest mDigest = MessageDigest.getInstance("SHA1");
int read;
byte[] buffer = new byte[4096];
while ((read = is.read(buffer)) != -1) {
mDigest.update(buffer, 0, read);
}
byte[] result = mDigest.digest();
StringBuilder sb = new StringBuilder();
for (byte b : result) {
sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
}
sha1 = sb.toString();
} finally {
close(is);
}
return sha1;
}

private File extractTemp(ArrayList<Throwable> errors, URL source, String prefix, String suffix, File directory) {
File target = null;
directory = directory.getAbsoluteFile();
if (!directory.exists()) {
Expand Down Expand Up @@ -381,7 +469,7 @@ private File extract(ArrayList<Throwable> errors, URL source, String prefix, Str
}
} catch (Throwable e) {
IOException io;
if( target!=null ) {
if (target != null) {
target.delete();
io = new IOException("Unable to extract library from " + source + " to " + target);
} else {
Expand All @@ -394,7 +482,7 @@ private File extract(ArrayList<Throwable> errors, URL source, String prefix, Str
}

static private void close(Closeable file) {
if(file!=null) {
if (file != null) {
try {
file.close();
} catch (Exception ignore) {
Expand Down
2 changes: 1 addition & 1 deletion hawtjni-website/src/documentation/developer-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ If you are not familiar with Maven, please checkout
[Maven by Example](http://www.sonatype.com/books/mvnex-book/reference/public-book.html).

The easiest way to get started with HawtJNI is copy and use
[the example Maven project](http://github.com/fusersource/hawtjni/tree/master/hawtjni-example/) as a template for your module.
[the example Maven project](http://github.com/fusesource/hawtjni/tree/master/hawtjni-example/) as a template for your module.

At the root of the Maven project run:
{pygmentize:: text}
Expand Down
5 changes: 3 additions & 2 deletions hawtjni-website/src/index.page
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ Features
* Maven integration:
* Generates JNI source code
* Generates an autoconf and msbuild source project to build the
native library. This gets attached to the maven project as as
native library. This gets attached to the Maven project as as
the native source zip file.
* Builds the native source tar for the current platform
* Built native library is stored in a platform specific jar. This
gets attached to the maven project as a platform specific jar file.
gets attached to the Maven project as a platform specific jar file.
* [Runtime API](documentation/api/) to load the library.

Introduction
------------
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Description
[HawtJNI][1] is a code generator that produces the JNI code needed to implement java native methods. It is based on the [jnigen][2] code generator that is part of the SWT Tools
project which is used to generate all the JNI code which powers the eclipse platform.

[![Maven Central](https://img.shields.io/maven-central/v/org.fusesource.hawtjni/hawtjni-runtime.svg?label=Maven%20Central)](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22org.fusesource.hawtjni%22%20a%3A%22hawtjni-runtime%22)

Features
--------
Expand Down

0 comments on commit c0cfb25

Please sign in to comment.