在尝试编写测试用例时会出现这个问题.Foo是框架库中的一个类,我没有源代码访问权限.
public class Foo{
public final Object getX(){
...
}
}
Run Code Online (Sandbox Code Playgroud)
我的申请会
public class Bar extends Foo{
public int process(){
Object value = getX();
...
}
}
Run Code Online (Sandbox Code Playgroud)
由于其他依赖性,我无法创建Foo对象,因此单元测试用例无法初始化.当值为null时,BarTest抛出空指针.
public class BarTest extends TestCase{
public testProcess(){
Bar bar = new Bar();
int result = bar.process();
...
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法可以使用反射api将getX()设置为非final?或者我该如何进行测试?
任何人都可以帮我找到JAVA中方法的返回类型.我试过这个.但不幸的是它不起作用.请指导我.
Method testMethod = master.getClass().getMethod("getCnt");
if(!"int".equals(testMethod.getReturnType()))
{
System.out.println("not int ::" + testMethod.getReturnType());
}
Run Code Online (Sandbox Code Playgroud)
输出:
不是int :: int
我想使用反射获取属性类型.这是我的代码
var properties = type.GetProperties();
foreach (var propertyInfo in properties)
{
model.ModelProperties.Add(
new KeyValuePair<Type, string>
(propertyInfo.PropertyType.Name,
propertyInfo.Name)
);
}
Run Code Online (Sandbox Code Playgroud)
这段代码没问题propertyInfo.PropertyType.Name,但如果我的属性类型是Nullable我得到这个Nullable'1字符串,FullName如果得到这个搅拌System.Nullable1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
我需要在java中使用反射.据我所知,它Class clazz创建了一个表示Class对象的变量.但是,我试图Class从String使用该forName("aClassName")方法引用一个对象.我的IDE(Eclipse)似乎更喜欢Class<?> clazz用于声明变量的符号.我在其他地方多次看过这种表示法.这是什么意思?
编辑:删除了对三元运算符的引用,因为它与此问题无关.
这可能吗?
我发现的唯一解决方案通常将方法视为重写,如果Class B可以分配,Class A并且if Method A具有相同的签名Method B.但它并不涵盖所有情况!
例如,我想处理这种情况:
interface Foo<T> {
void doStuff(T arg);
}
class FooImpl implements Foo<String> {
public void doStuff(String args) {
//Is overriden!
}
}
Run Code Online (Sandbox Code Playgroud)
我还想检查Method B实际可见的Method A(包范围等).
您是否曾经历过一种可靠的方法来确定方法是否被覆盖?
谢谢
一个简单的界面:
interface Foo {
void myMethod(String arg);
}
class FooImpl implements Foo {
void myMethod(String arg){}
public static void main(String[] args) {
Class cls = FooImpl.class;
try {
for (Method method : cls.getMethods()) {
System.out.print(method.getName() + "\t");
for(Class paramCls : method.getParameterTypes()){
System.out.print(paramCls.getName() + ",");
}
System.out.println();
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出将是:
myMethod java.lang.String,
...//Other Method
Run Code Online (Sandbox Code Playgroud)
只打印一个myMethod.
但是,如果我将接口更改为通用接口:
interface Foo<T> {
void myMethod(T arg);
}
class FooImpl implements Foo<String> {
void …Run Code Online (Sandbox Code Playgroud) 我正在初始化一个整数变量,如下所示:
LocalBuilder a = ilGen.DeclareLocal(typeof(Int32));
Run Code Online (Sandbox Code Playgroud)
如何访问它并为其分配值?我想做这样的事情:
int a, b;
a = 5;
b = 6;
return a + b;
Run Code Online (Sandbox Code Playgroud) 使用decltype和std::is_const可以从外部检测变量的常量.但是对象是否也可以知道它自己的常量?用法应该是:
#include <type_traits>
#include <iostream>
#include <ios>
struct Test
{
Test() {}
bool print() const
{
// does not work as is explained in https://stackoverflow.com/q/9890218/819272
return std::is_const<decltype(*this)>::value; // <--- what will work??
}
};
int main()
{
Test t;
const Test s;
// external constness test
std::cout << std::boolalpha << std::is_const<decltype(t)>::value << "\n";
std::cout << std::boolalpha << std::is_const<decltype(s)>::value << "\n";
// internal constness test
std::cout << std::boolalpha << t.print() << "\n";
std::cout << std::boolalpha …Run Code Online (Sandbox Code Playgroud) 为什么这是错的:
Class<? extends Number> type = Integer.class;
ArrayList<type> = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
?
在给定类对象的情况下,是否无法实例化特定类型的类?
显然我永远不会直接这样做,这只是展示所需内容的一个例子.在我需要的实际代码中,我不知道该类型的名称.例如
public void createAList(Class<? extends Number> type)
{
ArrayList<type> toReturn = new ArrayList<>();
return toReturn;
}
Run Code Online (Sandbox Code Playgroud) 我想动态调用一个MethodInfo对象,并且从它内部抛出的任何异常向外传递,就像它被正常调用一样.
我看来有两种选择.它们概述如下.
选项1保持抛出的异常的类型MyStaticFunction,但是StackTrace因为它而被破坏throw.
选项2维护StackTrace异常,但异常的类型始终如此TargetInvocationException.我可以提取InnerException它的类型,但这意味着我不能写这个例子:
try { DoDynamicCall(); }
catch (MySpecialException e) { /* special handling */ }
Run Code Online (Sandbox Code Playgroud)
选项1:
void DoDynamicCall()
{
MethodInfo method = /*referencing MyClass method void MyStaticFunction(int x)*/;
try
{
method.Invoke(null, new object[] { 5 });
}
catch (TargetInvocationException e)
{
throw e.InnerException;
}
}
Run Code Online (Sandbox Code Playgroud)
选项2:
void DoDynamicCall()
{
MethodInfo method = /*referencing MyClass method void MyStaticFunction(int x)*/;
method.Invoke(null, new …Run Code Online (Sandbox Code Playgroud)