use of java.security.CodeSource in project ratpack by ratpack.
the class ScriptEngine method createClassLoader.
private GroovyClassLoader createClassLoader(final Path scriptPath) {
final CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
if (!scriptBaseClass.equals(Script.class)) {
compilerConfiguration.setScriptBaseClass(scriptBaseClass.getName());
}
compilerConfiguration.addCompilationCustomizers(new CompilationCustomizer(CompilePhase.CONVERSION) {
@Override
public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException {
if (staticCompile) {
classNode.addAnnotation(new AnnotationNode(new ClassNode(CompileStatic.class)));
}
classNode.addAnnotation(new AnnotationNode(new ClassNode(InheritConstructors.class)));
if (scriptPath != null) {
AnnotationNode scriptPathAnnotation = new AnnotationNode(new ClassNode(ScriptPath.class));
scriptPathAnnotation.addMember("value", new ConstantExpression(scriptPath.toUri().toString()));
classNode.addAnnotation(scriptPathAnnotation);
}
}
});
return new GroovyClassLoader(parentLoader, compilerConfiguration) {
@Override
protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source) {
return new CompilationUnit(config, source, this) {
{
verifier = new Verifier() {
@Override
public void visitClass(ClassNode node) {
if (node.implementsInterface(ClassHelper.GENERATED_CLOSURE_Type)) {
AnnotationNode lineNumberAnnotation = new AnnotationNode(LINE_NUMBER_CLASS_NODE);
lineNumberAnnotation.addMember("value", new ConstantExpression(node.getLineNumber(), true));
node.addAnnotation(lineNumberAnnotation);
}
super.visitClass(node);
}
};
}
};
}
};
}
use of java.security.CodeSource in project robovm by robovm.
the class SecureClassLoaderTest method testGetPermissions.
public void testGetPermissions() throws Exception {
URL url = new URL("http://localhost");
CodeSource cs = new CodeSource(url, (Certificate[]) null);
MyClassLoader ldr = new MyClassLoader();
ldr.getPerms(null);
ldr.getPerms(cs);
}
use of java.security.CodeSource in project spring-boot by spring-projects.
the class ApplicationHome method findSource.
private File findSource(Class<?> sourceClass) {
try {
ProtectionDomain domain = (sourceClass == null ? null : sourceClass.getProtectionDomain());
CodeSource codeSource = (domain == null ? null : domain.getCodeSource());
URL location = (codeSource == null ? null : codeSource.getLocation());
File source = (location == null ? null : findSource(location));
if (source != null && source.exists() && !isUnitTest()) {
return source.getAbsoluteFile();
}
return null;
} catch (Exception ex) {
return null;
}
}
use of java.security.CodeSource in project gradle by gradle.
the class ClasspathUtil method getClasspathForClass.
public static File getClasspathForClass(Class<?> targetClass) {
URI location;
try {
CodeSource codeSource = targetClass.getProtectionDomain().getCodeSource();
if (codeSource != null && codeSource.getLocation() != null) {
location = toURI(codeSource.getLocation());
if (location.getScheme().equals("file")) {
return new File(location);
}
}
if (targetClass.getClassLoader() != null) {
String resourceName = targetClass.getName().replace('.', '/') + ".class";
URL resource = targetClass.getClassLoader().getResource(resourceName);
if (resource != null) {
return getClasspathForResource(resource, resourceName);
}
}
throw new GradleException(String.format("Cannot determine classpath for class %s.", targetClass.getName()));
} catch (URISyntaxException e) {
throw UncheckedException.throwAsUncheckedException(e);
}
}
use of java.security.CodeSource in project gradle by gradle.
the class TransformingClassLoader method findClass.
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
if (!shouldTransform(name)) {
return super.findClass(name);
}
String resourceName = name.replace('.', '/') + ".class";
URL resource = findResource(resourceName);
byte[] bytes;
CodeSource codeSource;
try {
if (resource != null) {
bytes = loadBytecode(resource);
bytes = transform(name, bytes);
URL codeBase = ClasspathUtil.getClasspathForResource(resource, resourceName).toURI().toURL();
codeSource = new CodeSource(codeBase, (Certificate[]) null);
} else {
bytes = generateMissingClass(name);
codeSource = null;
}
} catch (Exception e) {
throw new GradleException(String.format("Could not load class '%s' from %s.", name, resource), e);
}
if (bytes == null) {
throw new ClassNotFoundException(name);
}
String packageName = StringUtils.substringBeforeLast(name, ".");
Package p = getPackage(packageName);
if (p == null) {
definePackage(packageName, null, null, null, null, null, null, null);
}
return defineClass(name, bytes, 0, bytes.length, codeSource);
}
Aggregations