/** * @param to * @param name * @return <code>true</code> if the name is equal to the fromName, or if the name represents a * superclass or superinterface of the fromName, <code>false</code> otherwise */ private boolean isAssignableTo(DotName name, Class<?> to) { if (to.getName().equals(name.toString())) { return true; } if (OBJECT_NAME.equals(name)) { return false; // there's nothing assignable from Object.class except for Object.class } ClassInfo fromClassInfo = index.getClassByName(name); if (fromClassInfo == null) { // We reached a class that is not in the index. Let's use reflection. final Class<?> clazz = loadClass(name.toString()); return to.isAssignableFrom(clazz); } DotName superName = fromClassInfo.superName(); if (superName != null && isAssignableTo(superName, to)) { return true; } if (fromClassInfo.interfaces() != null) { for (DotName interfaceName : fromClassInfo.interfaces()) { if (isAssignableTo(interfaceName, to)) { return true; } } } return false; }
/** * Build new {@link Index} with mocked annotations from orm.xml. This method should be only called * once per {@org.hibernate.metamodel.source.annotations.xml.mocker.IndexBuilder IndexBuilder} * instance. * * @param globalDefaults Global defaults from <persistence-unit-metadata>, or null. * @return Index. */ Index build(EntityMappingsMocker.Default globalDefaults) { // merge annotations that not overrided by xml into the new Index for (ClassInfo ci : index.getKnownClasses()) { DotName name = ci.name(); if (indexedClassInfoAnnotationsMap.containsKey(name)) { // this class has been overrided by orm.xml continue; } if (ci.annotations() != null && !ci.annotations().isEmpty()) { Map<DotName, List<AnnotationInstance>> tmp = new HashMap<DotName, List<AnnotationInstance>>(ci.annotations()); DefaultConfigurationHelper.INSTANCE.applyDefaults(tmp, globalDefaults); mergeAnnotationMap(tmp, annotations); classes.put(name, ci); if (ci.superName() != null) { addSubClasses(ci.superName(), ci); } if (ci.interfaces() != null && ci.interfaces().length > 0) { addImplementors(ci.interfaces(), ci); } } } return Index.create(annotations, subclasses, implementors, classes); }
ClassInfo createClassInfo(String className) { if (StringHelper.isEmpty(className)) { throw new AssertionFailure("Class Name used to create ClassInfo is empty."); } DotName classDotName = DotName.createSimple(className); if (classes.containsKey(classDotName)) { // classInfoAnnotationsMap.put( classDotName, new HashMap<DotName, // List<AnnotationInstance>>(classes.get( classDotName ).annotations()) ); return classes.get(classDotName); } Class clazz = serviceRegistry.getService(ClassLoaderService.class).classForName(className); DotName superName = null; DotName[] interfaces = null; short access_flag; ClassInfo annClassInfo = index.getClassByName(classDotName); if (annClassInfo != null) { superName = annClassInfo.superName(); interfaces = annClassInfo.interfaces(); access_flag = annClassInfo.flags(); } else { Class superClass = clazz.getSuperclass(); if (superClass != null) { superName = DotName.createSimple(superClass.getName()); } Class[] classInterfaces = clazz.getInterfaces(); if (classInterfaces != null && classInterfaces.length > 0) { interfaces = new DotName[classInterfaces.length]; for (int i = 0; i < classInterfaces.length; i++) { interfaces[i] = DotName.createSimple(classInterfaces[i].getName()); } } access_flag = (short) (clazz.getModifiers() | 0x20); // (modifiers | ACC_SUPER) } Map<DotName, List<AnnotationInstance>> map = new HashMap<DotName, List<AnnotationInstance>>(); classInfoAnnotationsMap.put(classDotName, map); ClassInfo classInfo = ClassInfo.create(classDotName, superName, access_flag, interfaces, map); classes.put(classDotName, classInfo); addSubClasses(superName, classInfo); addImplementors(interfaces, classInfo); return classInfo; }