instanceof

How to know if a class is an @Entity (javax.persistence.Entity)?

江枫思渺然 提交于 2021-02-10 18:17:22
问题 How can I know if a class is annotated with javax.persistence.Entity ? Person (Entity) @Entity @Table(name = "t_person") public class Person { ... } PersonManager @Stateless public class PersonManager { @PersistenceContext protected EntityManager em; public Person findById(int id) { Person person = this.em.find(Person.class, id); return person; } I try to do it with instance of as the following @Inject PersonManager manager; Object o = manager.findById(1); o instanceof Entity // false however

Java中isAssignableFrom,instanceof

北城以北 提交于 2021-01-19 05:41:40
isAssignableFrom 示例:Class1.isAssignableFrom(Class2) 解释:① 判断Class1和Class2是否相同。 System.out.println(Object.class.isAssignableFrom(Object.class)); // true ②Class1是否是Class2的父类或者接口。 System.out.println(Object.class.isAssignableFrom(String.class)); // true System.out.println("Object类是String 类的父类:"+ Object.class.isAssignableFrom(String.class));//true System.out.println("AbstractList类是ArrayList 类的父类:"+ AbstractList.class.isAssignableFrom(new ArrayList<>().getClass()));//true System.out.println("List接口是ArrayList 类的父类:"+ List.class.isAssignableFrom(new ArrayList<>().getClass()));//true System.out.println(

Feasible way to avoid instanceof in equals method?

依然范特西╮ 提交于 2020-12-26 09:26:02
问题 Many people don't like to use instanceof , but I find that in many cases we have few other options when it comes to the equals method. Take a look at the class below: class A { int n; public A(int n) { this.n = n; } @Override public boolean equals(Object o) { return false; } public boolean equals(A o) { return n == o.n; } } I've never seen something like this done, but could it serve as a replacement for having to use instanceof to test if an Object is an A ? Or are there other problems that

What is a function for structs like Java's instanceof?

送分小仙女□ 提交于 2020-12-08 20:11:24
问题 I'm making an OOP chat client in Rust. The module messages.rs creates and handles messages to other modules as structs: SimpleMessage and ComplexMessage structs: //! # Messages use time::SteadyTime; /// Represents a simple text message pub struct SimpleMessage<'a> { pub user: ... pub time: &'a SteadyTime<'a>, pub content: &'a str, } /// Represents attachments, like text or multimedia files. pub struct ComplexMessage<'a> { pub user: ... pub time: &'a SteadyTime<'a>, //pub content: PENDING }

What is a function for structs like Java's instanceof?

冷暖自知 提交于 2020-12-08 20:07:47
问题 I'm making an OOP chat client in Rust. The module messages.rs creates and handles messages to other modules as structs: SimpleMessage and ComplexMessage structs: //! # Messages use time::SteadyTime; /// Represents a simple text message pub struct SimpleMessage<'a> { pub user: ... pub time: &'a SteadyTime<'a>, pub content: &'a str, } /// Represents attachments, like text or multimedia files. pub struct ComplexMessage<'a> { pub user: ... pub time: &'a SteadyTime<'a>, //pub content: PENDING }

What is a function for structs like Java's instanceof?

寵の児 提交于 2020-12-08 20:05:04
问题 I'm making an OOP chat client in Rust. The module messages.rs creates and handles messages to other modules as structs: SimpleMessage and ComplexMessage structs: //! # Messages use time::SteadyTime; /// Represents a simple text message pub struct SimpleMessage<'a> { pub user: ... pub time: &'a SteadyTime<'a>, pub content: &'a str, } /// Represents attachments, like text or multimedia files. pub struct ComplexMessage<'a> { pub user: ... pub time: &'a SteadyTime<'a>, //pub content: PENDING }

Get type of a variable in Kotlin

女生的网名这么多〃 提交于 2020-06-10 02:09:51
问题 How can I find the variable type in Kotlin? In Java there is instanceof , but Kotlin does not exist: val properties = System.getProperties() // Which type? 回答1: You can use the is operator to check whether an object is of a specific type: val number = 5 if(number is Int) { println("number is of type Int") } You can also get the type as String using reflection: println("${number::class.simpleName}") // "Int" println("${number::class.qualifiedName}") // "kotlin.Int" Please note: On the Java

Check if variable belongs to custom type in Typescript

懵懂的女人 提交于 2020-05-17 07:36:06
问题 I'm trying to check whether a variable belongs to a certain type or not. Code: type GeneralType = SubTypeA | SubTypeB; type SubTypeA = 'type1' | 'type2'; type SubTypeB = 'type3' | 'type4'; function someFunction(arg1: GeneralType) { if (arg1 instanceof SubTypeA) { // Do something } // Continue function return arg1; } Of course this code fails in line 6 because instanceof is not usable for types. Is there an alternative option I could use without needing to check explicitly every posible value