Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package.toString() not working in JPF #466

Merged
merged 5 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/classes/modules/java.base/java/lang/Class.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.HashMap;
import java.util.Map;

import jdk.internal.loader.BootLoader;
import jdk.internal.reflect.ConstantPool;
import sun.reflect.annotation.AnnotationType;

Expand Down Expand Up @@ -144,7 +145,8 @@ public URL getResource (String rname) {

return getClassLoader().getResource(resolvedName);
}


/*
public Package getPackage() {
// very very crude version for now, only supports the package name
String pkgName = null;
Expand All @@ -164,6 +166,19 @@ public Package getPackage() {
}

}
*/

public Package getPackage() {
// very very crude version for now, only supports the package name
if (isPrimitive() || isArray()) {
return null;
}
ClassLoader cl = getClassLoader0();
return cl != null ? cl.definePackage(this)
: BootLoader.definePackage(this);
}

ClassLoader getClassLoader0() { return classLoader; }

public String getPackageName() {
int idx = name.lastIndexOf('.');
Expand Down
51 changes: 48 additions & 3 deletions src/classes/modules/java.base/java/lang/ClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* The Java Pathfinder core (jpf-core) platform is 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.
*
* 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
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package java.lang;
Expand All @@ -27,6 +27,7 @@
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

/**
* @author Nastaran Shafiei <[email protected]>
Expand All @@ -42,6 +43,10 @@ public abstract class ClassLoader {

//--- internals

// This variable is to provide support for definePackage(String,Module) method
private ConcurrentHashMap<String, NamedPackage> packages
= new ConcurrentHashMap<>();

protected ClassLoader() {
// constructed on the native side
}
Expand Down Expand Up @@ -293,4 +298,44 @@ public E nextElement() {
public native final Package getDefinedPackage(final String name);

public native final Package[] getDefinedPackages();

/**
* some extra implementation to support for Class.getPackage() method
*/
Package definePackage(String name, Module m) {
if (name.isEmpty() && m.isNamed()) {
throw new InternalError("unnamed package in " + m);
}

if(packages == null)
packages = new ConcurrentHashMap<>();

// check if Package object is already defined
NamedPackage pkg = packages.get(name);
if (pkg instanceof Package)
return (Package)pkg;

return (Package)packages.compute(name, (n, p) -> toPackage(n, p, m));
}

private Package toPackage(String name, NamedPackage p, Module m) {
// define Package object if the named package is not yet defined
if (p == null)
return NamedPackage.toPackage(name, m);

// otherwise, replace the NamedPackage object with Package object
if (p instanceof Package)
return (Package)p;

return NamedPackage.toPackage(p.packageName(), p.module());
}

Package definePackage(Class<?> c) {
if (c.isPrimitive() || c.isArray()) {
return null;
}

return definePackage(c.getPackageName(), c.getModule());
}
}

15 changes: 15 additions & 0 deletions src/tests/gov/nasa/jpf/test/java/net/URLClassLoaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,19 @@ public void testThrownException() throws ClassNotFoundException, MalformedURLExc
}
movePkgBack();
}

@Test
public void getPackageInfo() throws MalformedURLException {
movePkgOut();
if(verifyNoPropertyViolation()) {
try {
assertNotNull(getClass().getPackage());
assertEquals(getClass().getPackage().getName(),"gov.nasa.jpf.test.java.net");
assertNotNull(getClass().getPackage().toString());
} catch (Exception e){
assertFalse(e instanceof NullPointerException);
}
}
movePkgBack();
}
}
Loading