Close

Java Compiler API - SimpleJavaFileObject Examples

Java Compiler API Java 

import javax.tools.SimpleJavaFileObject;
import java.io.IOException;
import java.net.URI;

public class JavaStringObject extends SimpleJavaFileObject {
private final String source;

protected JavaStringObject(String name, String source) {
super(URI.create("string:///" + name.replaceAll("\\.", "/") +
Kind.SOURCE.extension), Kind.SOURCE);
this.source = source;
}

@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors)
throws IOException {
return source;
}
}
Original Post




import javax.tools.SimpleJavaFileObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;

public class JavaByteObject extends SimpleJavaFileObject {
private ByteArrayOutputStream outputStream;

protected JavaByteObject(String name) throws URISyntaxException {
super(URI.create("bytes:///"+name + name.replaceAll("\\.", "/")), Kind.CLASS);
outputStream = new ByteArrayOutputStream();
}

//overriding this to provide our OutputStream to which the
// bytecode can be written.
@Override
public OutputStream openOutputStream() throws IOException {
return outputStream;
}

public byte[] getBytes() {
return outputStream.toByteArray();
}
}
Original Post




import javax.tools.SimpleJavaFileObject;
import java.io.IOException;
import java.net.URI;

public class JavaStringObject extends SimpleJavaFileObject {
private final String source;

protected JavaStringObject(String name, String source) {
super(URI.create("string:///" + name.replaceAll("\\.", "/") +
Kind.SOURCE.extension), Kind.SOURCE);
this.source = source;
}

@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors)
throws IOException {
return source;
}
}
Original Post




import javax.tools.SimpleJavaFileObject;
import java.io.IOException;
import java.net.URI;

public class JavaStringObject extends SimpleJavaFileObject {
private final String source;

protected JavaStringObject(String name, String source) {
super(URI.create("string:///" + name.replaceAll("\\.", "/") +
Kind.SOURCE.extension), Kind.SOURCE);
this.source = source;
}

@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors)
throws IOException {
return source;
}
}
Original Post




See Also