Bug 1024853 - Enhancements to "Programmatically Load Classes and Resources in a Deployment" doc
Summary: Enhancements to "Programmatically Load Classes and Resources in a Deployment"...
Keywords:
Status: CLOSED CURRENTRELEASE
Alias: None
Product: JBoss Enterprise Application Platform 6
Classification: JBoss
Component: Documentation
Version: 6.2.0
Hardware: Unspecified
OS: Unspecified
unspecified
unspecified
Target Milestone: GA
: EAP 6.2.0
Assignee: sgilda
QA Contact: Russell Dickenson
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2013-10-30 14:04 UTC by David M. Lloyd
Modified: 2014-09-29 18:31 UTC (History)
1 user (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Build Name: 14875, Development Guide-6.2-1 Build Date: 30-10-2013 10:11:06 Topic ID: 24376-549480 [Latest]
Last Closed: 2013-12-15 16:48:47 UTC
Type: Bug
Embargoed:


Attachments (Terms of Use)


Links
System ID Private Priority Status Summary Last Updated
Red Hat Bugzilla 1030262 0 unspecified CLOSED Code typo in "Load a Class File From the Class Loader" doc 2021-02-22 00:41:40 UTC

Internal Links: 1030262

Description David M. Lloyd 2013-10-30 14:04:19 UTC
Title: Programmatically Load Classes and Resources in a Deployment

Describe the issue:
A couple of minor problems.

Suggestions for improvement:

1. Class targetClass = Class.forName("TargetClass", true, this.getClass().getClassLoader()); <-- should include a package name.

2. In "Load All Instances of a Single Resource", the text "use the Class.getClassLoader().getResources(String resourceName) method, where resource name is the fully qualified path the the resource" should be changed to "use the Class.getClassLoader().getResources(String resourceName) method, where *resourceName* is the fully qualified path *of* the resource" (emphasis added) ("resourceName" is a literal parameter name)

3. In "Load All Instances of a Single Resource" ... " returns an Enumeration of URL objects" is correct but the code sample then uses an incorrect construct (probably my fault, sorry) consisting of a URL[].  Change this code:

  URL[] urls = MyClass.class.getClassLoader().getResources("full/path/to/resource");
  while (urls.hasMoreElements()) {
      URL url = urls.nextElement();
      ...

To:

  Enumeration<URL> urls = MyClass.class.getClassLoader().getResources("full/path/to/resource");
  while (urls.hasMoreElements()) {
      URL url = urls.nextElement();
      ...

4. The last section "Load a Class File From the Class Loader" is incorrect.  Change this code:

  InputStream inputStream = TargetClass.class.getResourceAsStream(TargetClass.class.getSimpleName() + ".class");

To:

  InputStream inputStream = TargetClass.class.getResourceAsStream(TargetClass.class.getSimpleName().replace('.', '/') + ".class");

5. In the "Load a Class File From the Class Loader" I also recommend changing this:

  InputStream inputStream = TargetClass.class.getClassLoader().getResourceAsStream("TargetClass".replace('.', '/') + ".class");

To:

  String className = "com.yourcompany.TargetClass";
  InputStream inputStream = TargetClass.class.getClassLoader().getResourceAsStream(className.replace('.', '/') + ".class");

...because in the given example the replacement is not necessary (the class name has no '.' in it), and in any case if you already have the class name it would be more logical to simply do the replacement by hand yielding "com/yourcompany/TargetClass.class" for example.

Comment 1 sgilda 2013-10-30 15:37:47 UTC
Good catches. Thanks!

Made the changes to topic 24376.

Comment 4 Pavel Jelinek 2013-11-14 09:13:10 UTC
Verified. 
There is one more typo and I created new BZ for it. See https://bugzilla.redhat.com/show_bug.cgi?id=1030262


Note You need to log in before you can comment on or make changes to this bug.