Re: I need to know if a java class import a package



Daniel wrote:
I need that some users fill with code in page to solve a task, this
task involves the sorting of an array of integers, this could be
easily solved with Collections.sort, but I gave them as a
precondition, they couldn't use the Collections class (because I want
to test if they can iterate an order an array by themselves).
To correct this tasks, I have a Junit class that test against the
submitted code, so I was searching for some mechanism that could help
me with this.

You need to get the classloader used to tell you.

Here are a starting point:

import java.net.URL;
import java.net.URLClassLoader;

public class TraceClassLoader extends URLClassLoader {
public TraceClassLoader(String cp) throws Exception {
super(new URL[] { new URL(cp) });
}
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
System.out.println(name);
return super.loadClass(name, resolve);
}
public static void main(String[] args) throws Exception {
Class.forName(args[0], true, new TraceClassLoader(args[1])).getMethod("main", new Class[] { String[].class }).invoke(null, new Object[] { new String[0] });
}
}

import java.util.*;

public class Foobar {
public static void main(String[] args) throws Exception {
List lst = new ArrayList();
lst.add("CCC");
lst.add("BB");
lst.add("A");
for(int i = 0; i < lst.size(); i++) {
System.out.println(lst.get(i));
}
Collections.sort(lst);
for(int i = 0; i < lst.size(); i++) {
System.out.println(lst.get(i));
}
}
}

java TraceClassLoader Foobar file:/C:/

Foobar
java.lang.Object
java.util.List
java.lang.String
java.lang.Exception
java.util.ArrayList
java.lang.System
java.io.PrintStream
CCC
BB
A
java.util.Collections
A
BB
CCC

Arne

PS: It is important that Foobar.class is not in the classpath where
TraceClassLoader is running.
.



Relevant Pages

  • Re: [Newbie] Reading & Writing Objects To File
    ... | java.io.StreamCorruptedException exception. ... |>> public static void main(Stringargs) ... |>> new CardCreatorDump(); ...
    (microsoft.public.dotnet.vjsharp)
  • Re: crushing java
    ... your reaction just proved you to be sentimental and self blinded ... public static void Main(stringargs) ... }catch(Exception e) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: crushing java
    ... your reaction just proved you to be sentimental and self blinded ... public static void Main(stringargs) ... }catch(Exception e) ...
    (microsoft.public.dotnet.framework)
  • Re: Passing Unknown Number of Varaibles
    ... orange's signature is: public Int orange ... Are you really trying to pass a variable number of arguments to a method that takes an array? ... public static void main(Stringargs) { ...
    (comp.lang.java.help)
  • Re: noob question
    ... You've only created an array of 10 objects. ... error "Exception System.NullReferenceException was thrown in debuggee:Object ... class MyClass ... public static void Main ...
    (microsoft.public.dotnet.languages.csharp)