anonymous-class

Inject anonymous classes with spring

爱⌒轻易说出口 提交于 2019-12-11 01:19:42
问题 I've read a lot about getting generic type at runtime and I've understood that to prevent full type erasure and get generic type without giving it to constructor I can use an anonymous class plus an utility method, i.e. interface Generic<T> { public Class<T> getGenericType(); } @Component class GenericImpl<T> extends AbstractGenericImpl<T> { } abstract class AbstractGenericImpl<T> implements Generic<T> { protected Class<T> klass; @SuppressWarnings("unchecked") public Class<T> getGenericType()

How to refer to enclosing anonymous class instance in Java?

妖精的绣舞 提交于 2019-12-10 20:44:57
问题 I'd like to know if it's possible (and how if it is) to refer to enclosing anonymous class instance in Java. Example code: final Handler handler = new Handler(); handler.post(new Runnable() { @Override public void run() { new Task() { @Override public void onTaskFinish() { handler.post(?); // what should go here? } }.execute() } }); 回答1: If you were also a JavaScript coder, I bet you wouldn't need to ask this :) There is a trivial way to achieve what you want (and happens to be an important

Using templates with anonymous classes scoped inside a function

坚强是说给别人听的谎言 提交于 2019-12-10 15:52:43
问题 Let's say I have the following snippet: template <class T> void f(T arg) { arg(); } void g() { struct { void operator()(void) { } } foo; f(foo); } Visual C++ accepts this. However, when I try GCC, I get: $ g++ --version # just in case this matters g++ (Debian 4.4.5-8) 4.4.5 ... $ g++ foo.cc foo.cc: In function 'void g()': foo.cc:7: error: no matching function for call to 'f(g()::<anonymous struct>&)' When foo is scoped globally and its type has a name, this works. But when the type is

Can anonymous modules and class be nested in Ruby?

蓝咒 提交于 2019-12-10 06:54:33
问题 I can define an anonymous class within an anonymous module: c = nil m = Module.new do c = Class.new end m #=> #<Module:0x007fad3a055660> c #=> #<Class:0x007fad3a0555e8> Is the above equivalent to: m = Module.new c = Class.new In other words: does the concept of "nesting" actually apply to anonymous modules? 回答1: It is not about being anonymous. Assigning a dynamically created class to a constant makes it named: Foo = Class.new # => Foo foo = Class.new # => #<Class:0x007fe5dd45d650> Yet it

Can final parameters be qualified in some way to resolve naming conflicts with anonymous class members?

我只是一个虾纸丫 提交于 2019-12-10 02:50:09
问题 "Why are you doing this what is wrong with you?" notwithstanding, is there any way to accomplish this without changing the final method parameter name? private Foo createAnonymousFoo(final Bar bar) { return new Foo() { private Bar bar = SomeUnknownScopeQualifier.bar; public Bar getBar() { return bar; } public void doSomethingThatReassignsBar() { bar = bar.createSomeDerivedInstanceOfBar(); } }; } Obviously without the doSomethingThatReassignsBar call, you wouldn't need the member Bar and so on

Is it possible to serialize anonymous class without outer class?

放肆的年华 提交于 2019-12-09 04:44:59
问题 I made a small research on web and reviewed related topics on this site, but the answers were contradictory: some people said it is not possible, others said it is possible, but dangerous. The goal is to pass an object of the anonymous class as a parameter of the RMI method. Due to RMI requirements, this class must be serializable. Here's no problem, it is easy to make class Serializable. But we know that instances of inner classes hold a reference to an outer class (and anonymous classes are

Anonymous extending a class and implementing an interface at the same time?

こ雲淡風輕ζ 提交于 2019-12-08 19:25:37
问题 Suppose I have an interface called Interface, and a concrete class called Base, which, to make thing a bit more complicated, has a ctor that requires some arguments. I'd like to create an anonymous class that would extend Base and implement Interface. Something like Interface get() { return new Base (1, "one") implements Interace() {}; } That looks reasonable to me, but it doesn't work! (P.S: Actually, the Interface and Base are also generic classes :D. But I'll ignore that for now) 回答1: This

Is Java lambda anonymous object reused?

半城伤御伤魂 提交于 2019-12-08 17:15:36
问题 As of the current JDK 1.8 implementation, it builds an anonymous object to hold the lambda function and calls the function on such object. Is this anonymous object reused in each call, or is an object re-created each time? 回答1: It may be re-used, it may not. From JLS 15.27.4: Either a new instance of a class with the properties below is allocated and initialized, or an existing instance of a class with the properties below is referenced. You cannot depend on it to be one or the other. The

Why group by key of anonymous objects does not behave the way expected?

て烟熏妆下的殇ゞ 提交于 2019-12-08 12:03:28
问题 I have a csv file of this formart A,B,value a1,b1,10 a2,b1,12 a2,b1,15 a2,b2,14 a1,b1,12 which I am converting as datatable in my application. Dim enumerable = _dt.AsEnumerable Dim groupedResults = enumerable.GroupBy( _ Function(x) _ New With { _ .A = x.Item("A").ToString, _ .B = x.Item("B").ToString _ } _ ) I expected the groupedResults count to 4 instead of the 5 which shows. Basically it does not group by 1st and 5 th row into one group. I expected object with the same value will be

(JAVA Enums) - Anonymous class inside enum constant

半腔热情 提交于 2019-12-08 08:47:01
问题 Good day! I have an interface which only implements one single method. I dont feel like making several class which all implement this one single method therefore I decided to use anonymous classes instead. I use enums for certain static items, these enums have instances of my interface. However, when I try to make an anonymous class inside my enum constants my IDE (eclipse) literally tells me nothing (as if it is outside a code block). My question is as follows: Can I use anonymous classes