oms3.compiler.MemorySourceJavaFileObject Maven / Gradle / Ivy
package oms3.compiler;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.tools.SimpleJavaFileObject;
/**
* A Java source file that exists in memory.
*
* @author prunge
*/
public class MemorySourceJavaFileObject extends SimpleJavaFileObject {
String code;
/**
* Constructs a MemoryJavaFileObject
.
*
* @param name the name of the source file.
* @param code the source code.
*
* @throws IllegalArgumentException if name
is not valid.
* @throws NullPointerException if any parameter is null.
*/
public MemorySourceJavaFileObject(String name, String code) {
super(createUriFromName(name), Kind.SOURCE);
if (code == null) {
throw new NullPointerException("code");
}
this.code = code;
}
/**
* Creates a URI from a source file name.
* @param name the source file name.
* @return the URI.
* @throws NullPointerException if name
* is null.
* @throws IllegalArgumentException if name
* is invalid.
*/
private static URI createUriFromName(String name) {
if (name == null) {
throw new NullPointerException("name");
}
try {
return new URI(name);
} catch (final URISyntaxException e) {
throw new IllegalArgumentException("Invalid name: " + name, e);
}
}
@Override
public CharSequence getCharContent(boolean ignoreEncErrors) throws IOException {
return code;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy