Java – 防止将集合框架用于赋值的方法?

huangapple go评论78阅读模式
英文:

Java - way to prevent Collections Framework use for assignment?

问题

对于特定的作业(使用Java),我让我的学生实现一些抽象数据类型(ADTs),并且我不允许他们使用我们之前讲过的任何Java集合框架(如List、Collections、LinkedList、Stack、Deque等)。

有哪些好的方法来强制/检测这个限制,以确保他们在这个特定的作业中不使用这些接口和类?

是否有编译器指令/开关/设置可以完全阻止使用java.util?我的意思是,这会相当严格,但到目前为止,这个作业只需要使用java.lang就可以完成。我本来想使用Iterator,而且他们需要使用泛型。

我本来打算检测import语句,但反射无法从字节码中获取这些信息。有篇帖子建议在源文件中使用QDox来获取导入语句,但我不想为此加载额外的库。我是否应该编写一个脚本来扫描他们的源文件,查找java.util.x的导入?

我在考虑使用反射来查看他们的类文件,以查看是否有任何成员属于那些类型列表... 但有点混乱 :/

由于学生人数较多,我大部分作业都是自动评分/单元测试的,尽管在某些作业中我会查看所有代码,以给他们额外的反馈。所以目前,我必须在这个作业上这样做,只是为了找出那些没有遵循指示的人。

只是好奇你们中有些人会为此做些什么。

英文:

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.

答案1

得分: 4

有许多可用的依赖性分析工具,但如果您使用的是较新的JDK,jdeps(自JDK 8起的一部分)可能已经足够满足您的需求。

例如:

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

列出了对java.util.*类的依赖关系。可以很容易地从一个小的Shell脚本或类似脚本中调用它,以进行自动检查。

通过使用反射(Class.forName...)绕过检测并不是不可能,但是如果您的任何学生设法/知道如何做到这一点,无论如何都值得给予奖励积分。 Java – 防止将集合框架用于赋值的方法?

更多信息在这里:https://wiki.openjdk.java.net/display/JDK8/Java+Dependency+Analysis+Tool

英文:

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. Java – 防止将集合框架用于赋值的方法?

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

答案2

得分: 2

一种解决方法是利用checkstyle,并启用'IllegalImport'规则。https://checkstyle.sourceforge.io/config_imports.html#IllegalImport

只需将要禁止的类添加到列表中,如果它们出现在代码中,构建将失败。

英文:

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.

huangapple
  • 本文由 发表于 2020年9月14日 07:47:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63876579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定