Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

loading the inner classes using the custom class loaders

Posted on 2002-03-14
4
Medium Priority
?
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

715 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