Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Neurogami
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Neurogami/68399aac32bcd3097c5c to your computer and use it in GitHub Desktop.
Save Neurogami/68399aac32bcd3097c5c to your computer and use it in GitHub Desktop.
/**
* ##library.name##
* ##library.sentence##
* ##library.url##
*
* Copyright James Britt / Neurogami
*
* The MIT License (MIT)
*
* 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.
*
*
* @author ##author##
* @modified ##date##
* @version ##library.prettyVersion## (##library.version##)
*/
package com.neurogami.dynaosc;
import processing.core.*;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.ToolProvider;
import javax.tools.StandardJavaFileManager;
import javax.tools.JavaCompiler.CompilationTask;
import java.util.Arrays;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.MalformedURLException;
public class DynaOSC {
PApplet owner;
OscarInterface oscar;
String newCodeBaseName = "Oscar0";
public final static String VERSION = "##library.prettyVersion##";
/**
* a Constructor, usually called in the setup() method in your sketch to
* initialize and start the library.
*
*
* @example DynaOSC
* @param ownerP
*/
public DynaOSC(PApplet ownerP) {
owner = ownerP;
System.setProperty("java.home", "/home/users/james/ownCloud/repos/RuntimeCompiling/p5");
oscar = new OSCar();
}
public void d(String s) {
System.out.println("DEBUG: " + s );
}
public void genNewCode() {
String s = "package com.neurogami.dynaosc;\n";
s = s + "public class " + newCodeBaseName +" implements OscarInterface {\n";
s = s + "public "+newCodeBaseName+"() { }\n";
s = s + "public void sendSomeOSC() {\n";
s = s + "System.out.println(\"sendSomeOSC(): DNYAOSC!\");\n";
s = s + "}\n";
s = s + "}\n";
try {
PrintWriter writer = new PrintWriter(newCodeBaseName + ".java", "UTF-8");
writer.println(s);
writer.close();
} catch (FileNotFoundException fnfe ) {
System.err.println("FileNotFoundException " + fnfe);
}
catch (UnsupportedEncodingException uee ) {
System.err.println("UnsupportedEncodingException " + uee );
}
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null ) {
System.err.println("***************** compiler is null! *****************");
}
// We are getting a NPE here:
StandardJavaFileManager sjfm = compiler.getStandardFileManager(null, null, null);
String[] options = new String[] { "-d", "classes", "-cp", "classes" };
CompilationTask compilationTask = compiler.getTask(null, null, null,
Arrays.asList(options),
null,
sjfm.getJavaFileObjects(newCodeBaseName + ".java")
);
compilationTask.call();
File classesDir = new File("runtime");
try {
ClassLoader parentLoader = OSCar.class.getClassLoader();
URLClassLoader loader1 = new URLClassLoader( new URL[] { classesDir.toURI().toURL() }, parentLoader);
File f = new File("classes");
URL[] cp = {f.toURI().toURL()};
URLClassLoader urlcl = new URLClassLoader(cp);
try {
Class clazz = urlcl.loadClass("com.neurogami.dynaosc." + newCodeBaseName);
oscar = (OscarInterface) clazz.newInstance();
} catch (Exception ie) {
System.err.println("Error calling cls1.newInstance() " + ie);
};
} catch(MalformedURLException mye) {};
}
public void sendSomething() {
oscar.sendSomeOSC();
}
public void hello() {
System.out.println("Hello from DynaOSC.java!");
}
/**
* return the version of the library.
*
* @return String
*/
public static String version() {
return VERSION;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment