我想用泛型和varargs实现一个函数.
public class Question {
public static <A> void doNastyThingsToClasses(Class<A> parent, Class<? extends A>... classes) {
/*** something here ***/
}
public static class NotQuestion {
}
public static class SomeQuestion extends Question {
}
public static void main(String[] args) {
doNastyThingsToClasses(Object.class, Question.class, SomeQuestion.class); // OK
doNastyThingsToClasses(Question.class, SomeQuestion.class); // OK
doNastyThingsToClasses(Question.class, Object.class, SomeQuestion.class); // compilation failure
}
}
Run Code Online (Sandbox Code Playgroud)
这里的目的是断言传递给该函数的所有参数都是Class对象,扩展了作为第一个参数给出的Class.因此,main方法的两个第一行将编译,第三行将生成错误.
我的问题是:为什么我得到"类型安全:为varargs参数创建类的通用数组"消息前两行?
我在这里错过了什么吗?
附加问题:如何重新设计它以防止在每行调用"doNastyThingsToClasses"函数时显示此警告?我可以将其更改为"doNastyThingsToClasses(Class <A> parent,Class <?> ... classes)"并删除警告,但这也会删除编译时类型检查 - 如果我想要的话,那就太好了确保正确使用此功能.更好的解决方案?
我正在尝试在运行时切换类加载器:
public class Test {
public static void main(String[] args) throws Exception {
final InjectingClassLoader classLoader = new InjectingClassLoader();
Thread.currentThread().setContextClassLoader(classLoader);
Thread thread = new Thread("test") {
public void run() {
System.out.println("running...");
// approach 1
ClassLoader cl = TestProxy.class.getClassLoader();
try {
Class c = classLoader.loadClass("classloader.TestProxy");
Object o = c.newInstance();
c.getMethod("test", new Class[] {}).invoke(o);
} catch (Exception e) {
e.printStackTrace();
}
// approach 2
new TestProxy().test();
};
};
thread.setContextClassLoader(classLoader);
thread.start();
}
}
Run Code Online (Sandbox Code Playgroud)
和:
public class TestProxy {
public void test() {
ClassLoader …
Run Code Online (Sandbox Code Playgroud) 我有大约10个带有标头(.h)的源文件(.c)。只有两个源文件(.c)链接到可执行文件(ELF)中,并包含主要功能。大多数文件被编译为与可执行文件(.o)和(重新)使用(静态链接)的对象。
我试图定义一个通用规则来构建所有对象:
%.o : %.c
$(CC) $(CCFLAGS) -c -o $@ $<
Run Code Online (Sandbox Code Playgroud)
这对于编译成对象的源代码非常有用。每次添加新的源文件时,我都不必担心更新make文件。
但是我不知道为构建可执行文件创建规则的最佳方法是什么?
exec1 : object1.o object2.o object3.o
call the linker
exec2 : object1.o object2.o object4.o object5.o
call the linker
Run Code Online (Sandbox Code Playgroud)
这将起作用;但是,每当我包括一个新的标头(foo.h)时,我都还需要记住为此规则更新对象列表(添加foo.o)。
make有什么方法可以自动解析给定目标的必需对象(也许基于所包含的头文件吗?)?
有没有更好的方法来定义可执行文件的目标?
请注意,我不能使用通配符,因为并非所有可执行文件都需要所有对象。还是链接程序可以删除多余的对象?
谢谢!
以下代码是否清晰易读?
public void createDatabase() throws SQLException, IOException {
SQLiteDatabase database = dbStore.getDatabase();
LineNumberReader scriptInputReader = new LineNumberReader(new InputStreamReader(getClass().getResourceAsStream(SCRIPT_CREATE)));
for(String line; (line = scriptInputReader.readLine()) != null;) {
database.execSQL(line);
}
}
Run Code Online (Sandbox Code Playgroud)
我写了很多像上面那样的"for"循环.对我来说它看起来很清楚 - 它显示了循环中使用的临时变量("line"),限制了它的范围并指出循环结束时(当"readLine"返回"null"时).我想知道其他程序员是否会讨厌那些......
或者这个:
SQLiteDatabase database = dbStore.getDatabase();
Cursor cursor = database.query("PINS", new String [] {"ID", "X", "Y"}, null, null, null, null, "ID");
if(cursor.moveToFirst()) {
for(; !cursor.isAfterLast(); cursor.moveToNext()) {
(...)
}
}
cursor.close();
Run Code Online (Sandbox Code Playgroud)
像上面这样的东西只是"整洁"还是已经是Java难题?
java ×3
bcel ×1
c ×1
classloader ×1
coding-style ×1
generics ×1
makefile ×1
reflection ×1
types ×1