Solved

loading the inner classes using the custom class loaders

Posted on 2002-03-14
4
198 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 50 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB Script to add site to Java Exception List 4 75
Java DateChooser? 3 36
SHA2 certs for IIS AND Java? 2 80
servlet example 17 29
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

805 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