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

warning: auxiliary class CompoundEnumeration in ClassLoader.java should not be accessed from outside its own source file #90

Closed
gayanW opened this issue Jun 16, 2018 · 5 comments

Comments

@gayanW
Copy link
Collaborator

gayanW commented Jun 16, 2018

Following compiler warning shows up in the build logs:

[javac] src/classes/modules/java.base/java/lang/ClassLoader.java:112: warning: auxiliary class CompoundEnumeration in ClassLoader.java should not be accessed from outside its own source file
[javac]     return new CompoundEnumeration<URL>(resEnum);
[javac]                ^

Travis log:
https://travis-ci.org/javapathfinder/jpf-core/builds/393023635#L2444-L2446

@Octarine-J
Copy link
Contributor

Octarine-J commented Jun 18, 2018

This warning is shown when accessing a non-public class from a .java file that contains multiple classes.

Although it's not recommended, it is possible to declare multiple classes in one .java file. However, only one of those classes can be public:

=== A.java ===

public class A {
// ...
}

class B {
// this is an auxiliary class
}

============

gayanW added a commit to gayanW/jpf-core that referenced this issue Jun 18, 2018
Our java.lang.ClassLoader model class references
java.lang.CompoundEnumeration which is an auxilary class declared within
the java standard class java/lang/ClassLoader.java

This includes CompoundEnumeration class within our model class itself,
so to prevent our ClassLoader model class from referencing an auxilary
class from outside its own source file.

This fixes:
    warning: auxiliary class CompoundEnumeration in ClassLoader.java should
             not be accessed from outside its own source file

Fixes: javapathfinder#90
@gayanW
Copy link
Collaborator Author

gayanW commented Jun 18, 2018

The current code expects to have java.lang.CompoundEnumeration public standard Java class. But due to changes in the JDK, there seems to have no such public class. In JDK 9/10 for example it is declared in the java/lang/ClassLoader.java itself.

Please see the PR.

public Enumeration<URL> getResources(String name) throws IOException {
Enumeration<URL>[] resEnum = new Enumeration[2];
if(parent == null) {
resEnum[0] = getSystemClassLoader().getResourcesURL(name);
} else{
resEnum[0] = parent.getResources(name);
}
resEnum[1] = findResources(name);
return new CompoundEnumeration<URL>(resEnum);
}

References:
http://hg.openjdk.java.net/jdk10/jdk10/jdk/rev/47e7d7363249

@Octarine-J
Copy link
Contributor

It seems that this class is a merely utility that allows continuous iteration over all values from an array of enumerations. The class name is probably not important; the essential part lies in the behaviour. Maybe we can rewrite this method to return an instance of Enumeration that contains all values from the arrays resEnum[0] and resEnum[1]?

By the way, this Enumeration class is from JDK 1.0, and probably is not used widely anymore; its name is a bit unfortunate as one can confuse it with Enum. This Enumeration class seems to be a less poverful equivalent of Iterator.

@gayanW
Copy link
Collaborator Author

gayanW commented Jun 19, 2018

Thanks for the tip. What about this implementation, though it is quite verbose?

  public Enumeration<URL> getResources(String name) throws IOException {
    Vector<URL> urlVector = new Vector<>(2);
    if(parent == null) {
      Iterator<URL> iterator = getSystemClassLoader().getResourcesURL(name).asIterator();
      while (iterator.hasNext()) {
        urlVector.add(iterator.next());
      }
    } else{
      Iterator<URL> iterator = parent.getResources(name).asIterator();
      while (iterator.hasNext()) {
        urlVector.add(iterator.next());
      }
    }
    Iterator<URL> iterator = findResources(name).asIterator();
    while (iterator.hasNext()) {
      urlVector.add(iterator.next());
    }

    return urlVector.elements();
  }

@Octarine-J
Copy link
Contributor

Octarine-J commented Jun 20, 2018

Well, using Vector is not recommended for new code (very inefficient), although it conveniently returns an Enumeration. I would add a helper adapter class like the one below, and then used Iterator::forEachRemaining to add elements to a collection such as ArrayList.

    public class EnumerationAdapter<E> implements Enumeration<E> {
        private final Iterator<E> iterator;

        public EnumerationProxy(Collection<E> collection) {
            iterator = collection.iterator();
        }

        @Override
        public boolean hasMoreElements() {
            return iterator.hasNext();
        }

        @Override
        public E nextElement() {
            return iterator.next();
        }
    }

gayanW added a commit to gayanW/jpf-core that referenced this issue Jun 20, 2018
java.lang.ClassLoader#getResources in the model class
java.lang.ClassLoader references java.lang.CompoundEnumeration which is
an auxilary class declared within the java standard class
java/lang/ClassLoader.java

This adds a new auxilary class named EnumerationAdapter within the model
class, as replacement for CompoundEnumeration class, so to prevent our
ClassLoader model class from referencing an auxilary class from outside
its own source file.

This fixes:
    warning: auxiliary class CompoundEnumeration in ClassLoader.java should
             not be accessed from outside its own source file

Fixes: javapathfinder#90
gayanW added a commit to gayanW/jpf-core that referenced this issue Jun 20, 2018
java.lang.ClassLoader#getResources in the model class
java.lang.ClassLoader references java.lang.CompoundEnumeration which is
an auxilary class declared within the java standard class
java/lang/ClassLoader.java

This adds a new auxilary class named EnumerationAdapter within the model
class, as replacement for CompoundEnumeration class, so to prevent our
ClassLoader model class from referencing an auxilary class from outside
its own source file.

This fixes:
    warning: auxiliary class CompoundEnumeration in ClassLoader.java should
             not be accessed from outside its own source file

Fixes: javapathfinder#90
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants