Re: I need to know if a java class import a package
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