instanceof

How to check instanceof on an argument that is a Class object?

左心房为你撑大大i 提交于 2020-01-03 10:15:07
问题 I'm trying to build a generic class loader. I need to check classes that I load against a method argument to determine if they are of the same class. The code mostly explains what I'm trying to do. private static LinkedList<Object> loadObjectsInDirectory(Class class0, File dir) throws ClassNotFoundException { LinkedList<Feature> objects = new LinkedList<Object>(); ClassLoader cl = new GenericClassLoader(); for(String s : dir.list()) { Class class1 = cl.loadClass(s); try { Object x = class1

月薪过万必会的:双亲委托模型

橙三吉。 提交于 2020-01-03 09:56:09
类加载器简介 在介绍双亲委托模型之前,先介绍一下类加载器。类加载器通过一个类的全限定名来转换为描述这个类的二进制字节流。 对于任意一个类,被同一个类加载器加载后都是唯一的,但如果被不同加载器加载后,就不是唯一的了。即使是源于同一个Class文件、被同一个JVM加载,只要加载类的加载器不同,那么类就不同。 如何判断类是否相同,可以使用Class对象的equals()方法、isAssignableFrom()方法、isInstance()方法的返回结果进行判断,也可以使用instanceof关键字进行对象所属关系的判断。 下面我们写一个不同类加载器加载后的类,看一下对instanceof关键字运算有什么影响: public class OneMoreStudy { public static void main(String[] args) throws Exception { ClassLoader myLoader = new ClassLoader() { @Override public Class<?> loadClass(String name) throws ClassNotFoundException { try { String fileName = name.substring(name.lastIndexOf(".") + 1) + ".class";

instanceof yields inconsistent results for detecting interfaces?

守給你的承諾、 提交于 2020-01-03 07:32:09
问题 Is there anything tricky I should know about instanceof ? I'm passing a list of objects through a few methods and testing whether these objects implement a particular interface using instanceof . In some cases, instanceof correctly identifies objects as implementing the interface, in other cases it doesn't. It appears to be giving me inconsistent results on the same object in different places. Is there any trick/gotcha I should be aware of here? In anticipation of comments you might have: 1)

Java abstract method with abstract parameter and inheritance

大兔子大兔子 提交于 2020-01-02 08:13:59
问题 I recently fumbled into a problem with an API and an implementation where the following type of code appeared: The API is an abstract class: public abstract class A { public A sum(A a) { System.out.println("A.sum(A) called"); return null; } } The implementation is a simple class: public class B extends A { public B sum(B b) { System.out.println("B.sum(B) called"); return null; } } When it comes to using it I write: public class Main { public static void main(String args[]) { B b = new B(); A

IOC容器的依赖注入

折月煮酒 提交于 2020-01-01 12:25:05
IOC容器的初始化完成的主要工作是在IOC容器中建立BeanDefinition数据映射。在此过程中并没有看到IOC容器对Bean依赖关系进行注入。 注意到依赖注入的过程是用户第一次向IOC容器索要Bean时触发的。当然也有例外,也就是我们可以在BeanDefinition信息中通过控制lazy-init属性来让容器完成对Bean的预实例化。 BeanFactory中的getBean接口的实现就是触发依赖注入的地方。 AbstractBeanFactory的实现: final String beanName = transformedBeanName(name); Object bean; // Eagerly check singleton cache for manually registered singletons. Object sharedInstance = getSingleton(beanName); if (sharedInstance != null && args == null) { if (logger.isDebugEnabled()) { if (isSingletonCurrentlyInCreation(beanName)) { logger.debug("Returning eagerly cached instance of

Java - is there a “subclassof” like instanceof?

做~自己de王妃 提交于 2020-01-01 07:52:55
问题 Im overriding an equals() method and I need to know if the object is an instance of a Event's subclass (Event is the superclass). I want something like "obj subclassof Event". How can this be made? Thanks in advance! 回答1: instanceof can handle that just fine. 回答2: With the following code you can check if an object is a class that extends Event but isn't an Event class instance itself. if(myObject instanceof Event && myObject.getClass() != Event.class) { // then I'm an instance of a subclass

Java - is there a “subclassof” like instanceof?

若如初见. 提交于 2020-01-01 07:52:08
问题 Im overriding an equals() method and I need to know if the object is an instance of a Event's subclass (Event is the superclass). I want something like "obj subclassof Event". How can this be made? Thanks in advance! 回答1: instanceof can handle that just fine. 回答2: With the following code you can check if an object is a class that extends Event but isn't an Event class instance itself. if(myObject instanceof Event && myObject.getClass() != Event.class) { // then I'm an instance of a subclass

Reason behind using 'instanceof function() {}'?

血红的双手。 提交于 2020-01-01 04:11:05
问题 On Mozilla Developer Center, there is a page about the Function.prototype.bind function and provides a compatibility function for browsers which do not support this function. However, when analyzing this compatibility code I cannot find out why they use instanceof nop . nop has been set to function() {} . What part of the ECMA specification on bind does this correspond with? And what variables are an instance of function() {} ? The following returns false , so I don't completely know what it

avoid instanceof in Java

假如想象 提交于 2020-01-01 04:05:09
问题 I have been told at some stage at university (and have subsequently read in upteen places) that using instanceof should only be used as a 'last resort'. With this in mind, is anyone able to tell be if the following code I have is a last resort. I have had a look around on stack overflow but cannot quite find a similar scenario - perhaps I have missed it? private void allocateUITweenManager() { for(GameObject go:mGameObjects){ if (go instanceof GameGroup) ((GameGroup) go).setUITweenManager

spring 生命周期最详解

柔情痞子 提交于 2019-12-31 14:30:19
目的 在大三开始学习spring时,老师就说spring bean周期非常重要,当时也有仔细看,但是说实话搞不大懂,后面工作面试也问过,还是有点模糊,就是没有掌握好,进行深入理解,这次“老大”又问到了。不允许再回避了,所以这次坚决搞明白,理解生命周期作用,为啥要这样设计,我们能在生命周期做哪些更高层次的编程。 生命周期流程图 先总体看下spring的生命周期流程图,实现(继承)这些接口(抽象类)并在容器里注册,就可以看到bean的生命周期会按下面流程进行,后面会给出测试代码。 可以看出设计策略是“先顾大局”-类似的操作BeanFactory一般出现在Bean之前,操作完Bean之后,BeanFactory会进行“管理”;Bean操作的前提是应用了BeanPostProcessor。 测试代码 要被注册的Person类 package springBeanTest; import org.springframework.beans.BeansException; import org.springframework.beans.factory.*; public class Person implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean { private String name;