2020-01-02 16:52:38 +01:00
|
|
|
/*
|
|
|
|
Copyright 2020 Volker Berlin (i-net software)
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
|
|
|
|
*/
|
|
|
|
package de.inetsoftware.jwebassembly.module;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2020-01-03 19:51:58 +01:00
|
|
|
import java.util.HashMap;
|
2020-01-02 16:52:38 +01:00
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
|
|
|
import de.inetsoftware.classparser.ClassFile;
|
|
|
|
import de.inetsoftware.classparser.WeakValueCache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache and manager for the loaded ClassFiles
|
|
|
|
*
|
|
|
|
* @author Volker Berlin
|
|
|
|
*/
|
|
|
|
public class ClassFileLoader {
|
|
|
|
|
2020-01-03 19:51:58 +01:00
|
|
|
private final HashMap<String, ClassFile> replace = new HashMap<>();
|
|
|
|
|
|
|
|
private final WeakValueCache<String, ClassFile> weakCache = new WeakValueCache<>();
|
2020-01-02 16:52:38 +01:00
|
|
|
|
|
|
|
private final ClassLoader loader;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance
|
|
|
|
*
|
|
|
|
* @param loader
|
|
|
|
* the classloader to find the *.class files
|
|
|
|
*/
|
|
|
|
public ClassFileLoader( ClassLoader loader ) {
|
|
|
|
this.loader = loader;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ClassFile from cache or load it.
|
|
|
|
*
|
|
|
|
* @param className
|
|
|
|
* the class name
|
|
|
|
* @return the ClassFile or null
|
|
|
|
* @throws IOException
|
|
|
|
* If any I/O error occur
|
|
|
|
*/
|
|
|
|
@Nullable
|
|
|
|
public ClassFile get( String className ) throws IOException {
|
2020-01-03 19:51:58 +01:00
|
|
|
ClassFile classFile = replace.get( className );
|
|
|
|
if( classFile != null ) {
|
|
|
|
return classFile;
|
|
|
|
}
|
|
|
|
classFile = weakCache.get( className );
|
2020-01-02 16:52:38 +01:00
|
|
|
if( classFile != null ) {
|
|
|
|
return classFile;
|
|
|
|
}
|
|
|
|
InputStream stream = loader.getResourceAsStream( className + ".class" );
|
|
|
|
if( stream != null ) {
|
|
|
|
classFile = new ClassFile( stream );
|
2020-01-03 19:51:58 +01:00
|
|
|
weakCache.put( className, classFile );
|
2020-01-02 16:52:38 +01:00
|
|
|
}
|
|
|
|
return classFile;
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:51:58 +01:00
|
|
|
/**
|
|
|
|
* Replace the class in the cache with the given instance.
|
|
|
|
*
|
|
|
|
* @param className
|
|
|
|
* the name of the class to replace
|
|
|
|
* @param classFile
|
|
|
|
* the replasing ClassFile
|
|
|
|
*/
|
|
|
|
public void replace( String className, ClassFile classFile ) {
|
|
|
|
classFile = new ClassFile( className, classFile );
|
|
|
|
replace.put( className, classFile );
|
|
|
|
}
|
2020-01-02 16:52:38 +01:00
|
|
|
}
|