reflection

Java Refelection : find field's name and value

家住魔仙堡 提交于 2019-12-25 18:59:10
问题 I have a class like below public class SampleReflection { public static final String TWO_name = "html"; public static final String TWO_find = "css"; public static final String ONE_KEY_java = "java"; public static final String ONE_KEY_jsp = "jsp"; public static final String ONE_KEY_oracle = "oracle"; public static final String ONE_KEY_spring = "spring"; public static final String ONE_KEY_struts = "struts"; } I would like to get all the fields which starts with ONE_KEY and their value. because

Java call method after class is initialized

廉价感情. 提交于 2019-12-25 18:53:49
问题 I have a class X which is extended by various classes. X needs to know each subtype that exists of it at runtime. So my idea was to create a static method "static init()" in X that takes a Class as parameter. The problem is that a subclass of X is required to be initialized via its static-init-blocks before the call to init() happens. X for example may discover some of the static fields of the subclass which may be of any declared type. So take this code for example: class X { X() { /*This

InvokeMember where the member is an array property with reflection

前提是你 提交于 2019-12-25 18:44:13
问题 So I asked something similar last week, but I think it was pretty confusing, so Ill try to simplify it. Say for instance I have a class that contains only properties like this: public class MyPropertyClass { public int IntegerProperty { get; set; } } Now suppose I have created another class with an array of MyPropertyClass like this: public class AnotherPropertyClass { public MyPropertyClass[] ArrayProperty { get; set; } } Now here is the complicated part. I need to dynamically create a

How to convert from string to object of any type?

喜欢而已 提交于 2019-12-25 18:38:52
问题 I need to convert from a string that contains data to an object of some type that is passed using reflection. I have a not-serializable object that contains properties of any type, and I want to load data to that object. For example, that object has a property, Color BgColor . When I am trying to set "Red" value to that property, I get that conversion is not possible from string to Color. I need general code. 回答1: Try Convert.ChangeType for general conversions. But in your case I think the

Creating a dynamic fake class inside of an assembly

不想你离开。 提交于 2019-12-25 18:32:29
问题 Probably what I am asking is impossible, but nevertheless here is my problem and question. First of all this is C# and .NET. I would like to define an empty implementation (empty methods, functions returning defaults) of a class to an interface in such a way if I change the interface I will not need to adapt the code. Unfortunately I can not generate the implementation containing assembly and I can not define a dynamical mock on it because instantiation is done automatically via reflection.

Java Reflection library that has a function to create NewInstance for any Class with any Constructor [closed]

对着背影说爱祢 提交于 2019-12-25 17:18:44
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I am looking for a Java library that provides a function like this, invokeConstructor. (don't want to import clojure.lang) 回答1: apache

Check Java Object Hierarchy Externally

偶尔善良 提交于 2019-12-25 16:45:30
问题 Say I have the string "Bar" and I want to know whether or not Bar is a valid object, and further, whether "Bar" extends "Foo". Would I be using Java reflection or is there a better way, like a database? and how would I do it? Cheers 回答1: In case you don't know the package - only the class name, you could try this, using spring framework: List<Class> classes = new LinkedList<Class>(); PathMatchingResourcePatternResolver scanner = new PathMatchingResourcePatternResolver(); // this should match

Check if method exists

房东的猫 提交于 2019-12-25 16:04:07
问题 I want to check if the method Camera.Parameters.getHorizontalViewAngle() exists on the device (it's only available from API 8 and my min SDK API is 7). I tried to use "reflection", as explained here, but it catches an error saying the number of arguments is wrong: java.lang.IllegalArgumentException: wrong number of arguments Anybody could help? Camera camera; camera = Camera.open(); Parameters params = camera.getParameters(); Method m = Camera.Parameters.class.getMethod(

How to get singleton instance from a class name as string

我只是一个虾纸丫 提交于 2019-12-25 15:16:31
问题 I am running into a strange problem. I have an interface, whose implementations tend to be stateless. So, I want them to be singletons. I get the implementation class names as strings. For example String clazz = "com.foo.Bar"; I have a rules factory to obtain instances of IRule implementations. public class RulesFactory { private static final Logger logger = LoggerFactory.getLogger(RulesFactory.class); @SuppressWarnings("unchecked") public static <T extends IRule> T getRuleInstance(String

How to tell the compiler that a property is only accessed if it exists?

坚强是说给别人听的谎言 提交于 2019-12-25 11:50:22
问题 I can use HasProperty to check if a property exists. Only if the property exists a method should be executed. How can the compiler successfully compile even if the property doesn't exist? E.g. if (UIApplication.SharedApplication.Delegate.HasProperty("Instance")) { AppDelegate customAppDelegate = UIApplication.SharedApplication.Delegate as AppDelegate; customAppDelegate.Instance.SomeMethod(true); // can't be compiled because Instance doesn't exist } The thing is this: First, I check if the