reflection

How can I obtain the type parameter of a generic interface from an implementing class?

隐身守侯 提交于 2020-03-14 10:52:04
问题 I have this interface: public interface EventHandler<T extends Event> { void handle(T event); } And this class implementing it: public class MyEventHandler implements EventHandler<MyEvent> { @Override public void handle(MyEvent event) { //do something } } In this clase, T parameter is MyEvent , that is a concrete implementation of Event . How can obtain this using reflection? 回答1: Resolve the type of T by the generic interface. E.g. public interface SomeInterface<T> { } public class

Is there a way to access private method in inner-class with or without reflection

橙三吉。 提交于 2020-03-05 05:08:53
问题 I'm trying to make a simple example of a user and bank program where you must guarantee that money cannot be cheated by someone who can add, inherit, implement current existing classes but cannot edit the initial ones. So I ask if you somehow can set someone's account's balance without the provided function for money transfer. I've tried using reflection but you have to have public constructors to make an object on which you call the private methods but since everything is private I cant call

How to combine JUnit annotations into a custom annotation?

我怕爱的太早我们不能终老 提交于 2020-03-05 04:58:07
问题 (Using OpenJDK-13 and JUnit5-Jupiter) The problem is that my unit tests each make use of a not-small JUnit annotation system, something like this: @ParameterizedTest @MethodSource("myorg.ccrtest.testlogic.DataProviders#standardDataProvider") @Tags({@Tag("ccr"), @Tag("standard")}) This makes test authoring a little tedious, test code a little long and of course, when a change is needed, it's a chore! Was wondering if I could create my own JUnit annotation: @CcrStandardTest , which would imply

JUnit5-Jupiter: Composed (=“meta”) annotation does not resolve to annotation definition

感情迁移 提交于 2020-03-04 20:01:06
问题 I defined my own JUnit annotation: @ParameterizedTest @MethodSource("myorg.qa.ccrtesting.DataProviders#standardDataProvider") @Tags({@Tag("ccr"), @Tag("standard")}) public @interface CcrStandardTest { } Then, I was able to use that annotation in my tests: @CcrStandardTest public void E0010_contact_standard (String testData) { ... My run configuration: JVM options: -ea Class: myorg.qa.ccrtesting.ccrstandardtests.CcrStanConTest - This was suggested by the IDE (and is verified to point to the

Determine Which Parameter in Constructor was used to Set a Specific Field in Class?

元气小坏坏 提交于 2020-03-04 18:21:27
问题 I'm working on developing a custom Java object persistence framework as I recently discussed in this question. One issue I am trying to solve is to force uniform annotation values in constructor parameter and method. Is it possible to know which parameter in constructor was used to set a specific field in class via reflection? For example, if constructor contains parameter String textXYZ and class contains field String textABC and in constructor I do: textABC = textXYZ (so field/parameter

Exception on opening DataSet Visualizer due to the AssemblyResolve event

谁说胖子不能爱 提交于 2020-03-03 09:14:11
问题 When trying to use the magnifing glasson one of my DataSet or DataTable in my .Net Core 3.1 WPF Project I get a System.IO.FileLoadException with following text: Could not load file or assembly 'DataSetVisualizer.DebuggeeSide, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. General Exception (0x80131500) Stack trace: at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, RuntimeAssembly assemblyContext, StackCrawlMark& stackMark, Boolean

Exception on opening DataSet Visualizer due to the AssemblyResolve event

﹥>﹥吖頭↗ 提交于 2020-03-03 09:12:50
问题 When trying to use the magnifing glasson one of my DataSet or DataTable in my .Net Core 3.1 WPF Project I get a System.IO.FileLoadException with following text: Could not load file or assembly 'DataSetVisualizer.DebuggeeSide, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. General Exception (0x80131500) Stack trace: at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, RuntimeAssembly assemblyContext, StackCrawlMark& stackMark, Boolean

Diff Two Strings Line-By-Line in Java using Diff-Match-Patch

风格不统一 提交于 2020-02-29 07:13:25
问题 I was looking for an easy way to do a Myers Diff on two Strings line-by-line in Java. According to this, the Google diff-match-patch library has this feature. But, in the Java version, those methods referenced are protected and/or package-protected! I couldn't find another library that (1) does this, and (2) appears to be well-maintained. So I ended up using reflection to get the Google one to let me do this. I want to avoid anyone having to re-implement it, so I will post what I did as an

Java Security Manager completely disable reflection

こ雲淡風輕ζ 提交于 2020-02-27 05:03:31
问题 I've been reading quite a lot of questions on Stackoverflow about this question but couldn't quit find a solution or answer for my problem. If there is already one I would be grateful if somebody would give a hint ... My problem/question is if it is possible to completely disable reflection for not trustworthy code? Functions like getDeclaredMethods() (See test.java). I've already got a Java Security Manager which throws Security Exceptions if the code tries to write/read/etc. ... If it is

How do I get the values of the properties of a model object in ASP.NET MVC when passed in as an object?

旧街凉风 提交于 2020-02-25 04:36:12
问题 I would like to be able to write to a log the properties of various objects that I might pass to my LogEvent function. I have various objects defined, such as Town, Taxpayer, TaxedProperty, TaxAccount, etc. in my ASP.NET MVC application. I would like to write to some log passing in my object along with a bunch of other information. I am writing this to a database table and I have a large text field in which I can insert a representation of the object that was being processed when a bad thing