Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > I need to know if a java class import a package

Reply
Thread Tools

I need to know if a java class import a package

 
 
Daniel
Guest
Posts: n/a
 
      04-23-2007
Im writing a unit test class, and I want to check if a given class is
importing some package (ie: java.util.Collections) from the code, is
that posible?

With this code
Class a = myClass.getClass();
System.out.println(a.getPackage());

i can know the package of myClass, is there anything like this for
imports statements?

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      04-23-2007
Daniel wrote On 04/23/07 10:20,:
> Im writing a unit test class, and I want to check if a given class is
> importing some package (ie: java.util.Collections) from the code, is
> that posible?
>
> With this code
> Class a = myClass.getClass();
> System.out.println(a.getPackage());
>
> i can know the package of myClass, is there anything like this for
> imports statements?


Probably not, since `import' is just a compile-time
convenience. You get the same byte code from

class Foo {
java.util.Set s;
}

and from

import java.util.Set;
class Foo {
Set s;
}

and from

import java.util.*;
class Foo {
Set s;
}

and from

import java.util.*;
import javax.swing.*;
class Foo {
Set s;
}

Why should your test plan care whether the source
code uses fully-qualified class names or abbreviates
them with the help of `import'?

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Daniel
Guest
Posts: n/a
 
      04-23-2007
Its true, but is there a way to know if certain class is used inside
the tested class, despite if the developer declares them in the import
or with the fully-qualified name?


On Apr 23, 12:09 pm, Eric Sosman <(E-Mail Removed)> wrote:
> Daniel wrote On 04/23/07 10:20,:
>
> > Im writing a unit test class, and I want to check if a given class is
> > importing some package (ie: java.util.Collections) from the code, is
> > that posible?

>
> > With this code
> > Class a = myClass.getClass();
> > System.out.println(a.getPackage());

>
> > i can know the package of myClass, is there anything like this for
> > imports statements?

>
> Probably not, since `import' is just a compile-time
> convenience. You get the same byte code from
>
> class Foo {
> java.util.Set s;
> }
>
> and from
>
> import java.util.Set;
> class Foo {
> Set s;
> }
>
> and from
>
> import java.util.*;
> class Foo {
> Set s;
> }
>
> and from
>
> import java.util.*;
> import javax.swing.*;
> class Foo {
> Set s;
> }
>
> Why should your test plan care whether the source
> code uses fully-qualified class names or abbreviates
> them with the help of `import'?
>
> --
> (E-Mail Removed)



 
Reply With Quote
 
Daniel
Guest
Posts: n/a
 
      04-23-2007
Eric, thanks for your help
To clarify a little bit more my problem, I will try to add some
example of what I'm needing:

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.

The webpage is an open community JavaBlackBelt.com

Note: If this could be done, I imagine that also could be applied to
test if the developers are using some classes that are not specified
by the architecture. (eg: when developing J2ee code, sometimes the
developers could use some classes from the app server that could
decrease the portability)


On Apr 23, 12:09 pm, Eric Sosman <(E-Mail Removed)> wrote:
> Daniel wrote On 04/23/07 10:20,:
>
> > Im writing a unit test class, and I want to check if a given class is
> > importing some package (ie: java.util.Collections) from the code, is
> > that posible?

>
> > With this code
> > Class a = myClass.getClass();
> > System.out.println(a.getPackage());

>
> > i can know the package of myClass, is there anything like this for
> > imports statements?

>
> Probably not, since `import' is just a compile-time
> convenience. You get the same byte code from
>
> class Foo {
> java.util.Set s;
> }
>
> and from
>
> import java.util.Set;
> class Foo {
> Set s;
> }
>
> and from
>
> import java.util.*;
> class Foo {
> Set s;
> }
>
> and from
>
> import java.util.*;
> import javax.swing.*;
> class Foo {
> Set s;
> }
>
> Why should your test plan care whether the source
> code uses fully-qualified class names or abbreviates
> them with the help of `import'?
>
> --
> (E-Mail Removed)



 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      04-23-2007

Daniel wrote On 04/23/07 13:53,:
> Eric, thanks for your help
> To clarify a little bit more my problem, I will try to add some
> example of what I'm needing:
>
> 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.
>
> The webpage is an open community JavaBlackBelt.com


You could certainly inspect the compiled byte code for
references to "forbidden" classes. The java.lang.instrument
package might be helpful here.

However, I doubt such an approach will be all that useful
as a detector of cheating. To begin with, you need to make
a useful list of banned classes -- For example, if I were
told to sort an int[], I would use Arrays.sort() and never
touch the Collections class at all. Also, the sources for
Arrays (and Collections) are readily available; a cheater
could simply copy the source and change the package names,
and thus evade your detection.

Finally, JUnit is probably not a good framework for
this sort of thing. If I try to write an array-sorter but
make an error and introduce an infinite loop, you're going
to want some kind of timeout mechanism that prevents my
blunder (or my sabotage!) from monopolizing your page until
somebody notices the problem and restarts it. As far as
I'm aware, JUnit has no such facility; it just runs tests
until they finish or fail. If they do neither ...

Various organizations have public web pages that run
externally-submitted programs under controlled conditions,
with provision for catching runaways, guarding against hacks,
and so on. Instead of reinventing the wheel, I'd suggest
you look up a few of those sites and find out what they're
using for software; perhaps you can adapt something that
already exists as opposed to building it all from scratch.

> Note: If this could be done, I imagine that also could be applied to
> test if the developers are using some classes that are not specified
> by the architecture. (eg: when developing J2ee code, sometimes the
> developers could use some classes from the app server that could
> decrease the portability)


Although this shares some aspects with the programming-
assignment problem, I think it's fundamentally a different
issue. There's no "antagonist" to worry about, and the
body of code to be inspected is more or less "known," so I
think static analysis of byte code (or source code) may be
a more appropriate tool than black-box run-time testing.

--
(E-Mail Removed)
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      04-23-2007
Eric Sosman wrote:
> Daniel wrote On 04/23/07 13:53,:
>> Eric, thanks for your help
>> To clarify a little bit more my problem, I will try to add some
>> example of what I'm needing:
>>
>> 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.
>>
>> The webpage is an open community JavaBlackBelt.com

>
> You could certainly inspect the compiled byte code for
> references to "forbidden" classes. The java.lang.instrument
> package might be helpful here.
>
> However, I doubt such an approach will be all that useful
> as a detector of cheating. To begin with, you need to make
> a useful list of banned classes -- For example, if I were
> told to sort an int[], I would use Arrays.sort() and never
> touch the Collections class at all. Also, the sources for
> Arrays (and Collections) are readily available; a cheater
> could simply copy the source and change the package names,
> and thus evade your detection.


For completeness, note that a cheater could use reflection and run time
string calculation to invoke Arrays.sort without any mention of it that
would be visible to static analysis of the class. However, that would be
technically more difficult than writing a sort.

The most practical cheating method would indeed be to copy any known
working sort method, not necessarily Arrays.sort, followed by a few
minutes work with e.g. the Eclipse refactoring tools - change
identifiers to protect the guilty, extract some methods...

How about picking a programming task that does not have so many correct
implementations lying around on the web?

Patricia


 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      04-23-2007
Daniel wrote:
> Eric, thanks for your help
> To clarify a little bit more my problem, I will try to add some
> example of what I'm needing:
>
> 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).


I didn't read everything, but I wonder if some form of dependancy
injection would work here. You supply the object (let's call it
SortTest) to sort, rather than allow them to declare one internally.

Also, make each method call for SortTest, for a read or a write, record
what is being done. So if they are supposed to be implementing a shell
sort, you'll know if the reads and writes for the object are being done
in the correct order. This could be useful feedback for the student, to
point out common errors. If on the other hand they read each element
once, in order, then write them back once, in the sorted order, they are
probably cheating.

Rather than calling main, I think you'd have to define your own
Interface, which the user would then implement. Do the compile and run
both on your end, that way you can control which libraries are accessed.
You could also define your own classloader, to deny access to certain
classes by the user.
 
Reply With Quote
 
Philipp Taprogge
Guest
Posts: n/a
 
      04-23-2007
Hi!

Thus spake Patricia Shanahan on 04/23/2007 09:53 PM:
> For completeness, note that a cheater could use reflection and run time
> string calculation to invoke Arrays.sort without any mention of it that
> would be visible to static analysis of the class.


You could certainly try and grep your way through the submitted
source code to find those fragments.

> However, that would be
> technically more difficult than writing a sort.


However, that would be technically more difficult than coming up
with a more challenging task

> How about picking a programming task that does not have so many correct
> implementations lying around on the web?


But seriously... I always hated tasks like that which forced you to
reinvent the wheel... I think I might have used exactly the approach
you mentioned above... it might have been difficult, but I think I
would have liked the idea of having circumvented a seemingly useless
condition.

Regards,

Phil
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      04-24-2007
Philipp Taprogge wrote:
> Hi!
>
> Thus spake Patricia Shanahan on 04/23/2007 09:53 PM:
>> For completeness, note that a cheater could use reflection and run time
>> string calculation to invoke Arrays.sort without any mention of it that
>> would be visible to static analysis of the class.

>
> You could certainly try and grep your way through the submitted
> source code to find those fragments.


I'm not sure whether it would be equivalent to the Halting
Problem, but it certainly could be difficult:

StringBuffer buff =
new StringBuffer("kbwb/vujm/Bssbzt");
for (int i = 0; i < buff.length; ++i)
buff.setCharAt(i, buff.charAt(i) - 1);
Class c = Class.forName(buff.toString());
...

It's occurred to me that loading the "foreign" class with
a customized ClassLoader might be the start of a way to intercept
such dodges. It still wouldn't defend against the far simpler
cheat of copying the freely-available source code of Arrays.sort
and giving it a new name.

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      04-24-2007
Eric Sosman wrote:
> Philipp Taprogge wrote:
>> Hi!
>>
>> Thus spake Patricia Shanahan on 04/23/2007 09:53 PM:
>>> For completeness, note that a cheater could use reflection and run time
>>> string calculation to invoke Arrays.sort without any mention of it that
>>> would be visible to static analysis of the class.

>>
>> You could certainly try and grep your way through the submitted
>> source code to find those fragments.

>
> I'm not sure whether it would be equivalent to the Halting
> Problem, but it certainly could be difficult:
>
> StringBuffer buff =
> new StringBuffer("kbwb/vujm/Bssbzt");
> for (int i = 0; i < buff.length; ++i)
> buff.setCharAt(i, buff.charAt(i) - 1);
> Class c = Class.forName(buff.toString());
> ...


Or, really simple and effective, just xor "java.util.Arrays" with a
random bit pattern, and store both bit pattern and the result. The byte
code would contain two apparently random arrays of char, but their xor
would be the original string.

Patricia
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
on package import, have it conditionally import a subpackage Gabriel Rossetti Python 1 09-20-2009 09:18 PM
to use import java.lang.* or import java.lang.Math or none at all? JPractitioner Java 13 02-24-2006 08:48 PM
single package import v/s the entire package Parvinder Java 6 02-27-2005 02:02 PM
package module import name clash with global package George P Python 3 09-11-2004 01:19 PM
I know, I know, I don't know Andries Perl Misc 3 04-23-2004 02:17 AM



Advertisments