Solved

loading the inner classes using the custom class loaders

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now