instanceof

Checking if an annotation is of a specific type

为君一笑 提交于 2019-12-20 09:37:41
问题 I am using reflection to see if an annotation that is attached to a property of a class, is of a specific type. Current I am doing: if("javax.validation.Valid".equals(annotation.annotationType().getName())) { ... } Which strikes me as a little kludgey because it relies on a string that is a fully-qualified class-name. If the namespace changes in the future, this could cause subtle errors. I would like to do: if(Class.forName(annotation.annotationType().getName()).isInstance( new javax

How to get all objects of a specific type in a list?

▼魔方 西西 提交于 2019-12-20 03:44:06
问题 If I have a list of fruits, containing all sort of Fruit implementations like Apple , Banana , etc. The list is necessary as other methods perform general actions on all fruits in the list. How can I get all objects of a specific type out of the list? Eg all apples? Doing instanceof/if-else checks is very ugly, especially when there are lots of classes to differ. How can the following be improved? class Fruit; class Apple extends Fruit; class Banana extends Fruit; class FruitStore { private

How is instanceof implemented in modern JVM implementations?

旧城冷巷雨未停 提交于 2019-12-20 03:37:11
问题 Due to the benchmarking done in other threads (cf. https://stackoverflow.com/a/397617/1408611) it was shown that instanceof in Java 6 is actually quite fast. How is this achieved? I know that for single inheritance, the fastest idea is having some nested interval encoding where each class maintains a [low,high] interval and an instanceof is simply an interval inclusion test, i.e. 2 integer comparisons. But how is it made for interfaces (as interval inclusion only works for single inheritance)

How to replace run-time instanceof check with compile-time generics validation

耗尽温柔 提交于 2019-12-20 03:35:10
问题 Got a little puzzle for a true Java Generics specialist... ;) Let's say I have the following two interfaces: interface Processor { void process(Foo foo); } interface Foo { Processor getProcessor(); } and, for example, the following two implementing classes: static class SomeProcessor implements Processor { static final SomeProcessor INSTANCE = new SomeProcessor(); @Override public void process(Foo foo) { if (foo instanceof SomeFoo) { // <-- GET RID OF THIS ? // process ((SomeFoo) foo) } } }

如何判断js中的数据类型

南楼画角 提交于 2019-12-19 04:11:10
如何判断js中的数据类型:typeof、instanceof、 constructor、 prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstring."; var b = 222; var c= [1,2,3]; var d = new Date(); var e = function(){alert(111);}; var f = function(){this.name="22";}; 最常见的判断方法:typeof alert(typeof a) ------------> string alert(typeof b) ------------> number alert(typeof c) ------------> object alert(typeof d) ------------> object alert(typeof e) ------------> function alert(typeof f) ------------> function 其中typeof返回的类型都是字符串形式,需注意,例如: alert(typeof a == "string") -------------> true alert(typeof a == String) ---------------> false 另外typeof

Alternative to instanceof approach in this case

断了今生、忘了曾经 提交于 2019-12-19 03:25:16
问题 Hello I'm wondering what would be some more elegant alternatives to something like this: class Base... class A extends Base... class B extends Base... //iterator of colection containing mixed As and Bs i want to remowe Bs and do omething with As while(iterator.hasNext()) { Base next = iterator.next(); if(next instanceof A) // do something if(next instanceof B) iterator.remove(); } Sow what are the alternatives... Thank you for advices. edit: Base class may have many subclasses not just two

如何判断js中的数据类型

喜夏-厌秋 提交于 2019-12-19 02:31:42
如何判断js中的数据类型:typeof、instanceof、 constructor、 prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstring."; var b = 222; var c= [1,2,3]; var d = new Date(); var e = function(){alert(111);}; var f = function(){this.name="22";}; 最常见的判断方法:typeof alert(typeof a) ------------> string alert(typeof b) ------------> number alert(typeof c) ------------> object alert(typeof d) ------------> object alert(typeof e) ------------> function alert(typeof f) ------------> function 其中typeof返回的类型都是字符串形式,需注意,例如: alert(typeof a == "string") -------------> true alert(typeof a == String) ---------------> false 另外typeof

typeof 运算符能判断哪些类型

て烟熏妆下的殇ゞ 提交于 2019-12-19 01:27:01
typeof 能判断所有的值类型 //typeof 能判断所有的值类型 let a ; console . log ( typeof a ) // undefined const a = 'string' console . log ( typeof a ) // string const a = 1 console . log ( typeof a ) // number const a = true console . log ( typeof a ) // boolean const a = Symbol ( 'a' ) console . log ( typeof a ) // Symbol typeof 能判断是否是函数 //typeof 能判断函数 typeof console . log ( 1 ) // function typeof function fn ( ) { } // function typeof 能判断出是否是引用类型(不可细分) //typeof 判断引用类型 const a = null typeof a //object const a = { a : 100 } typeof a // object const a = [ 'a' ] typeof a // object 结论 typeof 可以判断出所有的值类型 typeof

Why getClass returns the name of the class + $1 (or $*)

喜夏-厌秋 提交于 2019-12-18 15:51:48
问题 I'm writing a piece of code in which I have to cast an Object if it is an instance of a certain class. As usual I'm using instanceof for checking the compatibility. The problem is that the check is never satisfied because the objects belong to "strange" classes. For example; when I call the method getClass().getSimpleName() on this object it return me the name of the class + $* (e.g. ViewPart$1 instead of ViewPart ). What does this $* means? Is there a solution or a workaround? 回答1: That

Example of ´instanceof´

吃可爱长大的小学妹 提交于 2019-12-18 11:46:04
问题 public class TableModel2 extends TableModel1 { ... } TableModel2 tableModel = new TableModel2(); boolean t1 = tableModel instanceof TableModel1; boolean t2 = tableModel instanceof TableModel2; In the above example, t1 and t2 are true . So, how could I differentiate between TableModel1 and TableModel2 using instanceof ? 回答1: boolean t2 = tableModel.getClass().equals(TableModel1.class); //False boolean t2 = tableModel.getClass().equals(TableModel2.class); //True 回答2: You cannot do it with