2013-09-09 16:57:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.gecko.annotationProcessors;
|
|
|
|
|
2013-11-22 00:41:28 +04:00
|
|
|
import org.mozilla.gecko.annotationProcessors.classloader.AnnotatableEntity;
|
|
|
|
import org.mozilla.gecko.annotationProcessors.classloader.ClassWithOptions;
|
2013-09-09 16:57:37 +04:00
|
|
|
import org.mozilla.gecko.annotationProcessors.classloader.IterableJarLoadingURLClassLoader;
|
2013-11-22 00:41:28 +04:00
|
|
|
import org.mozilla.gecko.annotationProcessors.utils.GeneratableElementIterator;
|
2013-09-09 16:57:37 +04:00
|
|
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
public class AnnotationProcessor {
|
2015-07-11 06:41:35 +03:00
|
|
|
public static final String SOURCE_FILE = "GeneratedJNIWrappers.cpp";
|
|
|
|
public static final String HEADER_FILE = "GeneratedJNIWrappers.h";
|
|
|
|
public static final String NATIVES_FILE = "GeneratedJNINatives.h";
|
2013-09-09 16:57:37 +04:00
|
|
|
|
2013-11-22 00:41:28 +04:00
|
|
|
public static final String GENERATED_COMMENT =
|
|
|
|
"// GENERATED CODE\n" +
|
2015-01-10 03:33:57 +03:00
|
|
|
"// Generated by the Java program at /build/annotationProcessors at compile time\n" +
|
|
|
|
"// from annotations on Java methods. To update, change the annotations on the\n" +
|
2015-07-11 06:41:35 +03:00
|
|
|
"// corresponding Java methods and rerun the build. Manually updating this file\n" +
|
2015-01-10 03:33:57 +03:00
|
|
|
"// will cause your build to fail.\n" +
|
|
|
|
"\n";
|
2013-11-22 00:41:28 +04:00
|
|
|
|
Bug 1192079 - Support inner classes in generated JNI wrapper; r=snorp
Currently, when we generate JNI wrapper for an inner class, the
resulting C++ class will not actually be a nested class of the enclosing
class. As a result, the class can be confusing to use. For example,
wrapping Java class GeckoThread.State results in two unrelated C++
classes, GeckoThread and State, and it'd be confusing to use State by
itself.
This patch adds support for inner classes. We start by scanning only for
top-level classes, and when processing each top-level class, we
recursively scan for inner classes through
JarClassIterator.getInnerClasses() and CodeGenerator.generateClasses().
For each Java inner classes, the resulting C++ class will be a nested
class. For example, wrapping GeckoThread.State will produce
widget::GeckoThread and widget::GeckoThread::State.
2015-08-13 07:53:39 +03:00
|
|
|
private static final StringBuilder headerFile = new StringBuilder(GENERATED_COMMENT);
|
|
|
|
private static final StringBuilder implementationFile = new StringBuilder(GENERATED_COMMENT);
|
|
|
|
private static final StringBuilder nativesFile = new StringBuilder(GENERATED_COMMENT);
|
|
|
|
|
2013-09-09 16:57:37 +04:00
|
|
|
public static void main(String[] args) {
|
|
|
|
// We expect a list of jars on the commandline. If missing, whinge about it.
|
|
|
|
if (args.length <= 1) {
|
|
|
|
System.err.println("Usage: java AnnotationProcessor jarfiles ...");
|
|
|
|
System.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.println("Processing annotations...");
|
|
|
|
|
|
|
|
// We want to produce the same output as last time as often as possible. Ordering of
|
|
|
|
// generated statements, therefore, needs to be consistent.
|
|
|
|
Arrays.sort(args);
|
|
|
|
|
|
|
|
// Start the clock!
|
|
|
|
long s = System.currentTimeMillis();
|
|
|
|
|
|
|
|
// Get an iterator over the classes in the jar files given...
|
2013-11-22 00:41:28 +04:00
|
|
|
Iterator<ClassWithOptions> jarClassIterator = IterableJarLoadingURLClassLoader.getIteratorOverJars(args);
|
|
|
|
|
2015-01-10 03:33:57 +03:00
|
|
|
headerFile.append(
|
2015-07-11 06:41:35 +03:00
|
|
|
"#ifndef " + getHeaderGuardName(HEADER_FILE) + "\n" +
|
|
|
|
"#define " + getHeaderGuardName(HEADER_FILE) + "\n" +
|
2015-01-10 03:33:57 +03:00
|
|
|
"\n" +
|
|
|
|
"#include \"mozilla/jni/Refs.h\"\n" +
|
|
|
|
"\n" +
|
|
|
|
"namespace mozilla {\n" +
|
2016-07-21 20:49:04 +03:00
|
|
|
"namespace java {\n" +
|
2015-01-10 03:33:57 +03:00
|
|
|
"\n");
|
2013-11-22 00:41:28 +04:00
|
|
|
|
2015-01-10 03:33:57 +03:00
|
|
|
implementationFile.append(
|
|
|
|
"#include \"GeneratedJNIWrappers.h\"\n" +
|
|
|
|
"#include \"mozilla/jni/Accessors.h\"\n" +
|
|
|
|
"\n" +
|
|
|
|
"namespace mozilla {\n" +
|
2016-07-21 20:49:04 +03:00
|
|
|
"namespace java {\n" +
|
2015-01-10 03:33:57 +03:00
|
|
|
"\n");
|
2013-11-19 08:31:35 +04:00
|
|
|
|
2015-07-11 06:41:35 +03:00
|
|
|
nativesFile.append(
|
|
|
|
"#ifndef " + getHeaderGuardName(NATIVES_FILE) + "\n" +
|
|
|
|
"#define " + getHeaderGuardName(NATIVES_FILE) + "\n" +
|
|
|
|
"\n" +
|
|
|
|
"#include \"GeneratedJNIWrappers.h\"\n" +
|
|
|
|
"#include \"mozilla/jni/Natives.h\"\n" +
|
|
|
|
"\n" +
|
|
|
|
"namespace mozilla {\n" +
|
2016-07-21 20:49:04 +03:00
|
|
|
"namespace java {\n" +
|
2015-07-11 06:41:35 +03:00
|
|
|
"\n");
|
|
|
|
|
2013-11-19 19:56:09 +04:00
|
|
|
while (jarClassIterator.hasNext()) {
|
Bug 1192079 - Support inner classes in generated JNI wrapper; r=snorp
Currently, when we generate JNI wrapper for an inner class, the
resulting C++ class will not actually be a nested class of the enclosing
class. As a result, the class can be confusing to use. For example,
wrapping Java class GeckoThread.State results in two unrelated C++
classes, GeckoThread and State, and it'd be confusing to use State by
itself.
This patch adds support for inner classes. We start by scanning only for
top-level classes, and when processing each top-level class, we
recursively scan for inner classes through
JarClassIterator.getInnerClasses() and CodeGenerator.generateClasses().
For each Java inner classes, the resulting C++ class will be a nested
class. For example, wrapping GeckoThread.State will produce
widget::GeckoThread and widget::GeckoThread::State.
2015-08-13 07:53:39 +03:00
|
|
|
generateClass(jarClassIterator.next());
|
2013-09-09 16:57:37 +04:00
|
|
|
}
|
|
|
|
|
2015-01-10 03:33:57 +03:00
|
|
|
implementationFile.append(
|
2016-08-05 20:23:44 +03:00
|
|
|
"} /* java */\n" +
|
2015-01-10 03:33:57 +03:00
|
|
|
"} /* mozilla */\n");
|
2013-11-22 00:41:28 +04:00
|
|
|
|
2015-01-10 03:33:57 +03:00
|
|
|
headerFile.append(
|
2016-08-05 20:23:44 +03:00
|
|
|
"} /* java */\n" +
|
2015-01-10 03:33:57 +03:00
|
|
|
"} /* mozilla */\n" +
|
2015-07-11 06:41:35 +03:00
|
|
|
"#endif // " + getHeaderGuardName(HEADER_FILE) + "\n");
|
|
|
|
|
|
|
|
nativesFile.append(
|
2016-08-05 20:23:44 +03:00
|
|
|
"} /* java */\n" +
|
2015-07-11 06:41:35 +03:00
|
|
|
"} /* mozilla */\n" +
|
|
|
|
"#endif // " + getHeaderGuardName(NATIVES_FILE) + "\n");
|
2013-11-22 00:41:28 +04:00
|
|
|
|
2015-07-11 06:41:35 +03:00
|
|
|
writeOutputFile(SOURCE_FILE, implementationFile);
|
|
|
|
writeOutputFile(HEADER_FILE, headerFile);
|
|
|
|
writeOutputFile(NATIVES_FILE, nativesFile);
|
Bug 1192079 - Support inner classes in generated JNI wrapper; r=snorp
Currently, when we generate JNI wrapper for an inner class, the
resulting C++ class will not actually be a nested class of the enclosing
class. As a result, the class can be confusing to use. For example,
wrapping Java class GeckoThread.State results in two unrelated C++
classes, GeckoThread and State, and it'd be confusing to use State by
itself.
This patch adds support for inner classes. We start by scanning only for
top-level classes, and when processing each top-level class, we
recursively scan for inner classes through
JarClassIterator.getInnerClasses() and CodeGenerator.generateClasses().
For each Java inner classes, the resulting C++ class will be a nested
class. For example, wrapping GeckoThread.State will produce
widget::GeckoThread and widget::GeckoThread::State.
2015-08-13 07:53:39 +03:00
|
|
|
|
2013-09-09 16:57:37 +04:00
|
|
|
long e = System.currentTimeMillis();
|
|
|
|
System.out.println("Annotation processing complete in " + (e - s) + "ms");
|
|
|
|
}
|
|
|
|
|
Bug 1192079 - Support inner classes in generated JNI wrapper; r=snorp
Currently, when we generate JNI wrapper for an inner class, the
resulting C++ class will not actually be a nested class of the enclosing
class. As a result, the class can be confusing to use. For example,
wrapping Java class GeckoThread.State results in two unrelated C++
classes, GeckoThread and State, and it'd be confusing to use State by
itself.
This patch adds support for inner classes. We start by scanning only for
top-level classes, and when processing each top-level class, we
recursively scan for inner classes through
JarClassIterator.getInnerClasses() and CodeGenerator.generateClasses().
For each Java inner classes, the resulting C++ class will be a nested
class. For example, wrapping GeckoThread.State will produce
widget::GeckoThread and widget::GeckoThread::State.
2015-08-13 07:53:39 +03:00
|
|
|
private static void generateClass(final ClassWithOptions annotatedClass) {
|
|
|
|
// Get an iterator over the appropriately generated methods of this class
|
|
|
|
final GeneratableElementIterator methodIterator
|
|
|
|
= new GeneratableElementIterator(annotatedClass);
|
|
|
|
final ClassWithOptions[] innerClasses = methodIterator.getInnerClasses();
|
|
|
|
|
|
|
|
if (!methodIterator.hasNext() && innerClasses.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final CodeGenerator generatorInstance = new CodeGenerator(annotatedClass);
|
|
|
|
generatorInstance.generateClasses(innerClasses);
|
|
|
|
|
|
|
|
// Iterate all annotated members in this class..
|
|
|
|
while (methodIterator.hasNext()) {
|
|
|
|
AnnotatableEntity aElementTuple = methodIterator.next();
|
|
|
|
switch (aElementTuple.mEntityType) {
|
|
|
|
case METHOD:
|
|
|
|
generatorInstance.generateMethod(aElementTuple);
|
|
|
|
break;
|
|
|
|
case NATIVE:
|
|
|
|
generatorInstance.generateNative(aElementTuple);
|
|
|
|
break;
|
|
|
|
case FIELD:
|
|
|
|
generatorInstance.generateField(aElementTuple);
|
|
|
|
break;
|
|
|
|
case CONSTRUCTOR:
|
|
|
|
generatorInstance.generateConstructor(aElementTuple);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
headerFile.append(generatorInstance.getHeaderFileContents());
|
|
|
|
implementationFile.append(generatorInstance.getWrapperFileContents());
|
|
|
|
nativesFile.append(generatorInstance.getNativesFileContents());
|
|
|
|
|
|
|
|
for (ClassWithOptions innerClass : innerClasses) {
|
|
|
|
generateClass(innerClass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-11 06:41:35 +03:00
|
|
|
private static String getHeaderGuardName(final String name) {
|
|
|
|
return name.replaceAll("\\W", "_");
|
|
|
|
}
|
2013-09-09 16:57:37 +04:00
|
|
|
|
2015-07-11 06:41:35 +03:00
|
|
|
private static void writeOutputFile(final String name,
|
|
|
|
final StringBuilder content) {
|
2013-11-22 00:41:28 +04:00
|
|
|
FileOutputStream outStream = null;
|
2013-09-09 16:57:37 +04:00
|
|
|
try {
|
2015-07-11 06:41:35 +03:00
|
|
|
outStream = new FileOutputStream(name);
|
|
|
|
outStream.write(content.toString().getBytes());
|
2013-09-09 16:57:37 +04:00
|
|
|
} catch (IOException e) {
|
2015-07-11 06:41:35 +03:00
|
|
|
System.err.println("Unable to write " + name + ". Perhaps a permissions issue?");
|
2013-09-09 16:57:37 +04:00
|
|
|
e.printStackTrace(System.err);
|
2013-11-22 00:41:28 +04:00
|
|
|
} finally {
|
|
|
|
if (outStream != null) {
|
|
|
|
try {
|
|
|
|
outStream.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
System.err.println("Unable to close outStream due to "+e);
|
|
|
|
e.printStackTrace(System.err);
|
|
|
|
}
|
|
|
|
}
|
2013-09-09 16:57:37 +04:00
|
|
|
}
|
|
|
|
}
|
2015-01-10 03:33:57 +03:00
|
|
|
}
|