简体   繁体   English

Java - 防止集合框架用于分配的方法?

[英]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.).对于特定作业(在 Java 中),我让我的学生实现一些 ADT,并且我阻止他们使用我们之前介绍过的任何 Java 集合框架(列表、集合、链表、堆栈、双端队列等)。 )。

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?是否有编译器指令/开关/设置来完全阻止使用java.util I mean, that's some serious hobbling, but for this assignment so far it can get by on just java.lang .我的意思是,这是一些严重的障碍,但对于这个任务,到目前为止它只能通过java.lang I would have liked to used Iterator , and they need to have Generics.我本来想使用Iterator ,他们需要有泛型。

I was going to try to detect import statements, but reflection can't get those from the byte code.我打算尝试检测import语句,但反射无法从字节码中获取这些语句。 One post suggested QDox on source files for getting imports, but I didn't want to load an additional library for this.一篇文章建议在源文件上使用QDox来获取导入,但我不想为此加载额外的库。 Should I just write a script to scan their source files for java.util.x imports?我应该编写一个脚本来扫描它们的源文件以获取java.util.x导入吗?

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.有很多可用的依赖分析器,但如果您使用的是最新的 JDK, jdeps (JDK 8 的一部分)可能足以满足您的需求。

For example:例如:

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

lists the dependencies on java.util.* classes.列出对java.util.*类的依赖关系。 Should be easy to call it from a small shell script or similar for automatic checking.应该很容易从一个小的 shell 脚本或类似的脚本调用它以进行自动检查。

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.通过使用反射( Class.forName ...)来绕过检测并非不可能,但是如果您的任何学生管理/知道如何做到这一点,无论如何他们都应该得到加分。 ;-) ;-)

More info is available here: https://wiki.openjdk.java.net/display/JDK8/Java+Dependency+Analysis+Tool更多信息可在此处获得: 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.对此的一种解决方案是使用 checkstyle,并打开“IllegalImport”规则。 https://checkstyle.sourceforge.io/config_imports.html#IllegalImport 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.只需将您想要禁止的类添加到列表中,如果它们出现在代码中,构建就会失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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