Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

loading the inner classes using the custom class loaders

Posted on 2002-03-14
4
Medium Priority
?
235 Views
Last Modified: 2010-03-31
i am trying to write my own loader for the classes i am using, i have a class with inner classes, how to load the inner classes and return the reference of the outer class?


hari

0
Comment
Question by:bhksharma
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
Venci75 earned 200 total points
ID: 6867660
import java.util.*;
import java.net.*;
import java.io.*;

public class CL extends ClassLoader {

  public class FilenamePattern implements FilenameFilter {
    String pattern;
    public FilenamePattern(String p) {pattern = p + "$";}
    public boolean accept(File dir, String name) { return name.startsWith(pattern);}
  }

  protected String path;
  public CL(String j) {
      path = j;
  }
  public File getPath(String name) {
      File file = new File(path);
      StringTokenizer tok = new StringTokenizer(name, ".");
      while (tok.hasMoreTokens()) {
          file = new File(file, tok.nextToken());
      }
      return file;
  }
  public String[] getFiles(String className) {
      File file = getPath(className);
      String[] result = file.getParentFile().list(new FilenamePattern(file.getName()));
      for (int i = 0; i < result.length; i++) {
          result[i] = result[i].substring(0, result[i].lastIndexOf(".class"));
      }
      return result;
  }
  public Class findClass(String name) {
      byte[] b = loadClassData(name);
      Class result =  defineClass(name, b, 0, b.length);
      String[] list = getFiles(name);
      String pkg = name.substring(0, name.lastIndexOf('.')+1);
      for (int i = 0; i < list.length; i++) {
          b = loadClassData(pkg + list[i]);
          defineClass(pkg + list[i], b, 0, b.length);
      }
      return result;
  }

  private byte[] loadClassData(String name) {
      // load the class data from the connection
      try {
          File file = getPath(name);
          file = new File(file.getParent(), file.getName() + ".class");
          InputStream is = new FileInputStream(file);
          int r;
          ByteArrayOutputStream os = new ByteArrayOutputStream();
          while ((r = is.read()) != -1)
              os.write(r);
          return os.toByteArray();
      } catch (Exception ex) {
          ex.printStackTrace();
      }
      return null;
  }
  public static void main(String args[]) throws Exception{
          // the path to your classes
          CL cl = new CL("c:/work/ClassLoser/classes/");
          Class test_class =  cl.findClass("class with inner class");
();
          Class test_inner_class = test_class.getClasses()[0];
          System.out.println(test_inner_class);
          System.out.println(test_inner_class.getClassLoader());
  }
}
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6935148
Your question posted twice, experts are participating with you in both of them.
Moondancer - EE Moderator
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7901665
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: Venci75
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
0
 
LVL 5

Expert Comment

by:Netminder
ID: 7962584
Per recommendation, force-accepted.

Netminder
EE Admin
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question