Solved

loading the inner classes using the custom class loaders

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wordmultiple challenge 12 116
java. non-English characters encoding problem. intellij idea 3 73
mockito example issue 8 62
Java Server Faces parameter pass? 6 37
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

932 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

11 Experts available now in Live!

Get 1:1 Help Now