public boolean subclassOf(CtClass superclass) { if (superclass == null) return false; String superName = superclass.getName(); CtClass curr = this; try { while (curr != null) { if (curr.getName().equals(superName)) return true; curr = curr.getSuperclass(); } } catch (Exception ignored) {} return false; }
public static CtClass getSuperclass(CtClass c) throws CompileError { try { CtClass sc = c.getSuperclass(); if (sc != null) return sc; } catch (NotFoundException e) {} throw new CompileError("cannot find the super class of " + c.getName()); }
@Override public boolean subclassOf(CtClass superclass) { if (superclass == null) return false; String superName = superclass.getName(); CtClass curr = this; try { while (curr != null) { if (curr.getName().equals(superName)) return true; curr = curr.getSuperclass(); } } catch (Exception ignored) {} return false; }
public static CtClass getSuperclass(CtClass c) throws CompileError { try { CtClass sc = c.getSuperclass(); if (sc != null) return sc; } catch (NotFoundException e) {} throw new CompileError("cannot find the super class of " + c.getName()); }
Map getAllInterfaces(CtClass clazz, Map map) { if (map == null) map = new HashMap(); if (clazz.isInterface()) map.put(clazz.getName(), clazz); do { try { CtClass[] interfaces = clazz.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { CtClass intf = interfaces[i]; map.put(intf.getName(), intf); getAllInterfaces(intf, map); } clazz = clazz.getSuperclass(); } catch (NotFoundException e) { throw new RuntimeException(e); } } while (clazz != null); return map; }
private String fixTypes2(ArrayList scc, HashSet lowersSet, ClassPool cp) throws NotFoundException { Iterator it = lowersSet.iterator(); if (lowersSet.size() == 0) return null; // only NullType else if (lowersSet.size() == 1) return (String)it.next(); else { CtClass cc = cp.get((String)it.next()); while (it.hasNext()) cc = commonSuperClassEx(cc, cp.get((String)it.next())); if (cc.getSuperclass() == null || isObjectArray(cc)) cc = fixByUppers(scc, cp, new HashSet(), cc); if (cc.isArray()) return Descriptor.toJvmName(cc); else return cc.getName(); } }
private static boolean isObjectArray(CtClass cc) throws NotFoundException { return cc.isArray() && cc.getComponentType().getSuperclass() == null; }
private String fixTypes2(List<TypeData> scc, Set<String> lowersSet, ClassPool cp) throws NotFoundException { Iterator<String> it = lowersSet.iterator(); if (lowersSet.size() == 0) return null; // only NullType else if (lowersSet.size() == 1) return it.next(); else { CtClass cc = cp.get(it.next()); while (it.hasNext()) cc = commonSuperClassEx(cc, cp.get(it.next())); if (cc.getSuperclass() == null || isObjectArray(cc)) cc = fixByUppers(scc, cp, new HashSet<TypeData>(), cc); if (cc.isArray()) return Descriptor.toJvmName(cc); return cc.getName(); } }
private static boolean isObjectArray(CtClass cc) throws NotFoundException { return cc.isArray() && cc.getComponentType().getSuperclass() == null; }
/** * duplicated here to take CtClass instead of Class * @see org.hibernate.internal.util.ReflectHelper#locateField */ private static CtField findFieldOrNull(CtClass ctClass, String propertyName) { if ( ctClass == null ) { return null; } try { return ctClass.getField( propertyName ); } catch ( NotFoundException nsfe ) { try { return findFieldOrNull( ctClass.getSuperclass(), propertyName ); } catch (NotFoundException e) { return null; } } }
public static boolean isAssignable(CtClass thisCtClass, String targetClassName) { if ( thisCtClass == null ) { return false; } if ( thisCtClass.getName().equals( targetClassName ) ) { return true; } try { // check if extends if ( isAssignable( thisCtClass.getSuperclass(), targetClassName ) ) { return true; } // check if implements for ( CtClass interfaceCtClass : thisCtClass.getInterfaces() ) { if ( isAssignable( interfaceCtClass, targetClassName ) ) { return true; } } } catch (NotFoundException e) { // keep going } return false; }
/** * Adds a default constructor to the super classes. */ private void modifySuperclass(CtClass orgclass) throws CannotCompileException, NotFoundException { CtClass superclazz; for (;; orgclass = superclazz) { superclazz = orgclass.getSuperclass(); if (superclazz == null) break; try { superclazz.getDeclaredConstructor(null); break; // the constructor with no arguments is found. } catch (NotFoundException e) { } superclazz.addConstructor( CtNewConstructor.defaultConstructor(superclazz)); } } }
private Type mergeClasses(Type type) throws NotFoundException { CtClass superClass = findCommonSuperClass(this.clazz, type.clazz); // If its Object, then try and find a common interface(s) if (superClass.getSuperclass() == null) { Map interfaces = findCommonInterfaces(type); if (interfaces.size() == 1) return new Type((CtClass) interfaces.values().iterator().next()); if (interfaces.size() > 1) return new MultiType(interfaces); // Only Object is in common return new Type(superClass); } // Check for a common interface that is not on the found supertype Map commonDeclared = findExclusiveDeclaredInterfaces(type, superClass); if (commonDeclared.size() > 0) { return new MultiType(commonDeclared, new Type(superClass)); } return new Type(superClass); }
private static AccessType getAccessTypeOrNull(CtClass ctClass) { try { if ( ctClass.hasAnnotation( Access.class ) ) { return ( (Access) ctClass.getAnnotation( Access.class ) ).value(); } else { CtClass extendsClass = ctClass.getSuperclass(); return extendsClass == null ? null : getAccessTypeOrNull( extendsClass ); } } catch (ClassNotFoundException e) { return null; } catch (NotFoundException e) { return null; } }
CtClass s = cc.getSuperclass(); if (s != null) { CtMethod m = getMethod0(s, name, desc);
private static void getFields(ArrayList alist, CtClass cc) { int i, num; if (cc == null) return; try { getFields(alist, cc.getSuperclass()); } catch (NotFoundException e) {} try { CtClass[] ifs = cc.getInterfaces(); num = ifs.length; for (i = 0; i < num; ++i) getFields(alist, ifs[i]); } catch (NotFoundException e) {} CtMember.Cache memCache = ((CtClassType)cc).getMembers(); CtMember field = memCache.fieldHead(); CtMember tail = memCache.lastField(); while (field != tail) { field = field.next(); if (!Modifier.isPrivate(field.getModifiers())) alist.add(field); } }
orgclass.getSuperclass());
private static void getMethods0(HashMap h, CtClass cc) { try { CtClass[] ifs = cc.getInterfaces(); int size = ifs.length; for (int i = 0; i < size; ++i) getMethods0(h, ifs[i]); } catch (NotFoundException e) {} try { CtClass s = cc.getSuperclass(); if (s != null) getMethods0(h, s); } catch (NotFoundException e) {} if (cc instanceof CtClassType) { CtMember.Cache memCache = ((CtClassType)cc).getMembers(); CtMember mth = memCache.methodHead(); CtMember mthTail = memCache.lastMethod(); while (mth != mthTail) { mth = mth.next(); if (!Modifier.isPrivate(mth.getModifiers())) h.put(((CtMethod)mth).getStringRep(), mth); } } }
private synchronized CtClass javassistLoadClass( CtClass cc ) throws NotFoundException, CannotCompileException, BadBytecode { if( cc.isFrozen() ) return cc; // serialize parent javassistLoadClass(cc.getSuperclass()); // Serialize enums first, since we need the raw_enum function for this class for( CtField ctf : cc.getDeclaredFields() ) { CtClass base = ctf.getType(); while( base.isArray() ) base = base.getComponentType(); if( base.subclassOf(_enum) && base != cc ) javassistLoadClass(base); } CtClass ccr = addSerializationMethods(cc); ccr.freeze(); return ccr; }
/** * Creates a default (public) constructor. * * <p>The created constructor takes no parameter. It calls * <code>super()</code>. */ public static CtConstructor defaultConstructor(CtClass declaring) throws CannotCompileException { CtConstructor cons = new CtConstructor((CtClass[])null, declaring); ConstPool cp = declaring.getClassFile2().getConstPool(); Bytecode code = new Bytecode(cp, 1, 1); code.addAload(0); try { code.addInvokespecial(declaring.getSuperclass(), "<init>", "()V"); } catch (NotFoundException e) { throw new CannotCompileException(e); } code.add(Bytecode.RETURN); // no need to construct a stack map table. cons.getMethodInfo2().setCodeAttribute(code.toCodeAttribute()); return cons; }