reflection

Loop through an object's properties and get the values for those of type DateTime

最后都变了- 提交于 2020-05-11 03:46:06
问题 I have a list of objects (Cars). For each car in the list I need to loop through it and find any properties of type DateTime . If I find a property of DateTime I need to get the value and do a time conversion. For now lets just print out the DateTime property value to the console. I am having issues understanding what I need to put in the first parameter of the prop.GetValue function. Any help would be appreciated! foreach (var car in carList) { foreach (PropertyInfo car in car.GetType()

How to get TYPE_USE annotations on a generic bound

£可爱£侵袭症+ 提交于 2020-05-10 18:48:10
问题 I have this case: public class SomeClass<T> { public <@A1 S extends @A2 T> @A3 S myMethod() { ...} } and I'm trying to get the @A2 annotation on the bound T . This is what I'm seeing, assuming myMethod is SomeClass.class.getDeclaredMethod("myMethod") . Type casts removed for readability. myMethod.getGenericReturnType().getAnnotations() returns @A1 (as expected, I guess) myMethod.getGenericReturnType().getBounds()[0].getAnnotations() returns nothing (?? shoudn't it be @A2 ??) myMethod

How to get TYPE_USE annotations on a generic bound

末鹿安然 提交于 2020-05-10 18:43:08
问题 I have this case: public class SomeClass<T> { public <@A1 S extends @A2 T> @A3 S myMethod() { ...} } and I'm trying to get the @A2 annotation on the bound T . This is what I'm seeing, assuming myMethod is SomeClass.class.getDeclaredMethod("myMethod") . Type casts removed for readability. myMethod.getGenericReturnType().getAnnotations() returns @A1 (as expected, I guess) myMethod.getGenericReturnType().getBounds()[0].getAnnotations() returns nothing (?? shoudn't it be @A2 ??) myMethod

Pattern matching on Class[_] type?

不羁岁月 提交于 2020-05-08 20:48:11
问题 I'm trying to use Scala pattern matching on Java Class[_] (in context of using Java reflection from Scala) but I'm getting some unexpected error. The following gives "unreachable code" on the line with case jLong def foo[T](paramType: Class[_]): Unit = { val jInteger = classOf[java.lang.Integer] val jLong = classOf[java.lang.Long] paramType match { case jInteger => println("int") case jLong => println("long") } } Any ideas why this is happening ? 回答1: The code works as expected if you change

Pattern matching on Class[_] type?

五迷三道 提交于 2020-05-08 20:47:11
问题 I'm trying to use Scala pattern matching on Java Class[_] (in context of using Java reflection from Scala) but I'm getting some unexpected error. The following gives "unreachable code" on the line with case jLong def foo[T](paramType: Class[_]): Unit = { val jInteger = classOf[java.lang.Integer] val jLong = classOf[java.lang.Long] paramType match { case jInteger => println("int") case jLong => println("long") } } Any ideas why this is happening ? 回答1: The code works as expected if you change

java.lang.StringIndexOutOfBoundsException: String index out of range when package name is changed

牧云@^-^@ 提交于 2020-04-30 06:30:37
问题 I want to read the annotations from .class file placed into random folder. I tried this: public static void main(String[] args) throws Exception { final File folder = new File("/opt/test"); processAnnotatedFiles(listLocalFilesAndDirsAllLevels(folder)); } public void processAnnotatedFiles(List<File> list) throws IOException, ClassNotFoundException { out.println("Directory files size " + list.size()); for(int i=0; i<list.size(); i++) { out.println("File " + list.get(i).getName()); File file =

How to find all objects that implement a interface and invoke a method

可紊 提交于 2020-04-16 06:05:09
问题 I have a UserControl that have a few child UserControl 's and those UserControl 's have child UserControl's. Consider this: MainUserControl TabControl TabItem UserControl UserControl UserControl : ISomeInterface TabItem UserControl UserControl UserControl : ISomeInterface TabItem UserControl UserControl UserControl : ISomeInterface TabItem UserControl UserControl UserControl : ISomeInterface This is what i have so far, but finds no ISomeInterface : PropertyInfo[] properties = MainUserControl

Comparing two objects with default IComparable implementation with F# Reflection

橙三吉。 提交于 2020-04-16 05:47:22
问题 Given two objects in F#, is there a way to use their IComparable method to compare them, assuming they are both of the same sub-type and that IComparable is implemented for their common sub-type. What I am trying to achieve in pseudo-code : let tycompare (o1 : obj) (o2 : obj) : int option = let (ty1, ty2) = (o1.GetType(), o2.GetType()) if ty1 <> ty2 then None else if IComparable is implemented on ty1 then o1.CompareTo(o2) |> Some else None I am aware of this post but I do not think it helps

Initializing (list) properties in constructor using reflection

谁说胖子不能爱 提交于 2020-04-14 03:37:13
问题 I am trying to initialize all properties in class (lists) with using reflection: public class EntitiesContainer { public IEnumerable<Address> Addresses { get; set; } public IEnumerable<Person> People { get; set; } public IEnumerable<Contract> Contracts { get; set; } public EntitiesContainer() { var propertyInfo = this.GetType().GetProperties(); foreach (var property in propertyInfo) { property.SetValue(property, Activator.CreateInstance(property.GetType()), null); } } } I am getting exception

java equivalent of c# typeof()

瘦欲@ 提交于 2020-04-13 17:07:40
问题 I'm very new to java. Say I've a xml parser and I'd create object from it. In c# i'd do like: parser p = new parser(typeof(myTargetObjectType)); In my parser class I need to know which object I'm making, so that i can throw exception if parsing is not possible. How to Do the same in jave? I mean how I can take arguments like typeof(myObject) I understand every language has it's own way of doing something. I'm asking what's way in java 回答1: Java has the Class class as the entry point for any