2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.jvm;
20 import gov.nasa.jpf.Config;
21 import gov.nasa.jpf.JPF;
22 import gov.nasa.jpf.util.JPFLogger;
23 import gov.nasa.jpf.vm.ClassFileContainer;
24 import gov.nasa.jpf.vm.ClassInfo;
25 import gov.nasa.jpf.vm.ClassLoaderInfo;
26 import gov.nasa.jpf.vm.ClassParseException;
27 import gov.nasa.jpf.vm.MethodInfo;
28 import gov.nasa.jpf.vm.SystemClassLoaderInfo;
29 import gov.nasa.jpf.vm.VM;
31 import java.io.IOException;
34 * a SystemClassLoaderInfo that reads standard Java classfiles from *.class and *.jar files, and creates code using a concrete
35 * value JVM instruction set
37 public class JVMSystemClassLoaderInfo extends SystemClassLoaderInfo {
39 static JPFLogger log = JPF.getLogger("class");
40 protected JVMCodeBuilder defaultCodeBuilder;
42 public JVMSystemClassLoaderInfo (VM vm, int appId) {
45 defaultCodeBuilder = createDefaultCodeBuilder(config, appId);
47 JVMClassInfo.init(config);
50 vm.registerClassLoader(this);
54 * override this if you need a different default CodeBuilder
56 protected JVMCodeBuilder createDefaultCodeBuilder (Config config, int appId) {
57 String key = config.getIndexableKey("jvm.insn_factory.class", appId);
58 JVMInstructionFactory insnFactory = config.getEssentialInstance(key, JVMInstructionFactory.class);
59 return new JVMCodeBuilder(insnFactory);
63 protected ClassFileContainer createClassFileContainer (String spec) {
64 int i = spec.indexOf(".jar");
68 int len = spec.length();
70 String pathPrefix = null;
73 // no path prefix, plain jar
76 if (spec.charAt(j) == '/') {
77 pathPrefix = spec.substring(j);
78 jarPath = spec.substring(0, j);
83 jarFile = new File(jarPath);
84 if (jarFile.isFile()) {
86 return new JarClassFileContainer(jarFile, pathPrefix);
87 } catch (IOException ix) {
96 File dir = new File(spec);
97 if (dir.isDirectory()) {
98 return new DirClassFileContainer(dir);
105 protected void addSystemBootClassPath () {
106 String v = System.getProperty("sun.boot.class.path");
108 for (String pn : v.split(File.pathSeparator)) {
109 if (pn != null && !pn.isEmpty()) {
110 ClassFileContainer cfc = createClassFileContainer(pn);
112 cp.addClassFileContainer(cfc);
117 // Hmm, maybe we are not executing on OpenJDK
122 * this is the main method to create the ClassPath, which is called from the ctor
125 protected void initializeSystemClassPath (VM vm, int appId) {
126 Config conf = vm.getConfig();
129 // explicit "classpath[.id]" settings have precedence
130 pathElements = getPathElements(conf, "classpath", appId);
131 if (pathElements != null) {
132 for (File f : pathElements) {
133 addClassPathElement(f.getAbsolutePath());
137 // we optionally append boot_classpath
138 pathElements = getPathElements(conf, "vm.boot_classpath", appId);
139 if (pathElements != null) {
140 for (File f : pathElements) {
141 if (f.getName().equals("<system>")) {
142 addSystemBootClassPath();
144 addClassPathElement( f.getAbsolutePath());
149 log.info("collected system classpath: ", cp);
153 * override this if you have different CodeBuilders for different types
154 * NOTE - this CodeBuilder is not completely initialized yet, clients still have to call startMethod(mi) on it
156 protected JVMCodeBuilder getCodeBuilder (String clsName) {
157 return defaultCodeBuilder;
161 * used for automatically created code such as AnnotationProxies, direct calls, native calls and run starts
162 * NOTE - this cannot be called recursively or concurrently
164 protected JVMCodeBuilder getSystemCodeBuilder (ClassFile cf, MethodInfo mi) {
165 defaultCodeBuilder.reset(cf, mi);
166 return defaultCodeBuilder;
170 protected ClassInfo createClassInfo (String clsName, String url, byte[] data, ClassLoaderInfo definingLoader) throws ClassParseException {
171 ClassFile cf = new ClassFile(data);
172 JVMCodeBuilder cb = getCodeBuilder(clsName);
173 ClassInfo ci = new JVMClassInfo(clsName, definingLoader, cf, url, cb);