简体   繁体   中英

Java - way to prevent Collections Framework use for assignment?

For a particular assignment (in Java), I'm having my students implement some ADTs, and I am preventing them from using any of the Java Collections Framework that we have covered previously (List, Collections, LinkedList, Stack, Deque, etc.).

What are some good ways to enforce / detect this restriction to make sure they are not using these interfaces and classes for this particular assignment?

Is there a compiler directive / switch / setting to prevent usage of java.util entirely? I mean, that's some serious hobbling, but for this assignment so far it can get by on just java.lang . I would have liked to used Iterator , and they need to have Generics.

I was going to try to detect import statements, but reflection can't get those from the byte code. One post suggested QDox on source files for getting imports, but I didn't want to load an additional library for this. Should I just write a script to scan their source files for java.util.x imports?

I was thinking of looking at their class files with reflection to see if any members where of those list of types... but kinda messy :/

Most of my assignments are auto-graded / unit-tested because of the number of students, though for some assignments I peek at all the code to give them additional feedback. So for now, I have to do that on this one just to catch those who didn't follow directions.

Just curious what some of you would do for this.

There are a bunch of dependency analysers available, but if you are using a recent JDK, jdeps (part of the JDK since 8) might be sufficient for your needs.

For example:

$ jdeps -verbose:class -p java.util ~/opt/antlrworks/antlrworks-1.5.2-complete.jar

lists the dependencies on java.util.* classes. Should be easy to call it from a small shell script or similar for automatic checking.

It is not impossible to get around detection by using reflection ( Class.forName ...), but if any of your students manage/know how to do this, they deserve a bonus point anyway. ;-)

More info is available here: https://wiki.openjdk.java.net/display/JDK8/Java+Dependency+Analysis+Tool

One solution to this would be to make use of checkstyle, and turn on the 'IllegalImport' rule. https://checkstyle.sourceforge.io/config_imports.html#IllegalImport

Simply add the classes you want to disallow to the list, and if they are present in the code, the build will fail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM