📄 ctclasstype.java
字号:
return s.getField2(name); } catch (NotFoundException e) {} return null; } public CtField[] getDeclaredFields() { CtMember cf = getFieldsCache(); int num = CtField.count(cf); CtField[] cfs = new CtField[num]; int i = 0; while (cf != null) { cfs[i++] = (CtField)cf; cf = cf.next; } return cfs; } protected CtMember getFieldsCache() { if (fieldsCache == null) { List list = getClassFile2().getFields(); int n = list.size(); CtMember allFields = null; CtField tail = null; for (int i = 0; i < n; ++i) { FieldInfo finfo = (FieldInfo)list.get(i); CtField newTail = new CtField(finfo, this); allFields = CtMember.append(allFields, tail, newTail); tail = newTail; } fieldsCache = allFields; } return fieldsCache; } public CtField getDeclaredField(String name) throws NotFoundException { CtField f = getDeclaredField2(name); if (f == null) throw new NotFoundException("field: " + name + " in " + getName()); else return f; } private CtField getDeclaredField2(String name) { CtMember cf = getFieldsCache(); while (cf != null) { if (cf.getName().equals(name)) return (CtField)cf; cf = cf.next; } return null; } public CtBehavior[] getDeclaredBehaviors() { CtMember cc = getConstructorsCache(); CtMember cm = getMethodsCache(); int num = CtMember.count(cm) + CtMember.count(cc); CtBehavior[] cb = new CtBehavior[num]; int i = 0; while (cc != null) { cb[i++] = (CtBehavior)cc; cc = cc.next; } while (cm != null) { cb[i++] = (CtBehavior)cm; cm = cm.next; } return cb; } public CtConstructor[] getConstructors() { CtConstructor[] cons = getDeclaredConstructors(); if (cons.length == 0) return cons; int n = 0; int i = cons.length; while (--i >= 0) if (!Modifier.isPrivate(cons[i].getModifiers())) ++n; CtConstructor[] result = new CtConstructor[n]; n = 0; i = cons.length; while (--i >= 0) { CtConstructor c = cons[i]; if (!Modifier.isPrivate(c.getModifiers())) result[n++] = c; } return result; } public CtConstructor getConstructor(String desc) throws NotFoundException { CtConstructor cc = (CtConstructor)getConstructorsCache(); while (cc != null) { if (cc.getMethodInfo2().getDescriptor().equals(desc)) return cc; cc = (CtConstructor)cc.next; } return super.getConstructor(desc); } public CtConstructor[] getDeclaredConstructors() { CtMember cc = getConstructorsCache(); int num = CtMember.count(cc); CtConstructor[] ccs = new CtConstructor[num]; int i = 0; while (cc != null) { ccs[i++] = (CtConstructor)cc; cc = cc.next; } return ccs; } protected CtMember getConstructorsCache() { if (constructorsCache == null) { List list = getClassFile2().getMethods(); int n = list.size(); CtMember allConstructors = null; CtConstructor tail = null; for (int i = 0; i < n; ++i) { MethodInfo minfo = (MethodInfo)list.get(i); if (minfo.isConstructor()) { CtConstructor newTail = new CtConstructor(minfo, this); allConstructors = CtMember.append(allConstructors, tail, newTail); tail = newTail; } } constructorsCache = allConstructors; } return constructorsCache; } public CtConstructor getClassInitializer() { if (classInitializerCache == null) { MethodInfo minfo = getClassFile2().getStaticInitializer(); if (minfo != null) classInitializerCache = new CtConstructor(minfo, this); } return classInitializerCache; } public CtMethod[] getMethods() { HashMap h = new HashMap(); getMethods0(h, this); return (CtMethod[])h.values().toArray(new CtMethod[h.size()]); } 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 cm = ((CtClassType)cc).getMethodsCache(); while (cm != null) { if (!Modifier.isPrivate(cm.getModifiers())) h.put(((CtMethod)cm).getStringRep(), cm); cm = cm.next; } } } public CtMethod getMethod(String name, String desc) throws NotFoundException { CtMethod m = getMethod0(this, name, desc); if (m != null) return m; else throw new NotFoundException(name + "(..) is not found in " + getName()); } private static CtMethod getMethod0(CtClass cc, String name, String desc) { if (cc instanceof CtClassType) { CtMethod cm = (CtMethod)((CtClassType)cc).getMethodsCache(); while (cm != null) { if (cm.getName().equals(name) && cm.getMethodInfo2().getDescriptor().equals(desc)) return cm; cm = (CtMethod)cm.next; } } try { CtClass s = cc.getSuperclass(); if (s != null) { CtMethod m = getMethod0(s, name, desc); if (m != null) return m; } } catch (NotFoundException e) {} try { CtClass[] ifs = cc.getInterfaces(); int size = ifs.length; for (int i = 0; i < size; ++i) { CtMethod m = getMethod0(ifs[i], name, desc); if (m != null) return m; } } catch (NotFoundException e) {} return null; } public CtMethod[] getDeclaredMethods() { CtMember cm = getMethodsCache(); int num = CtMember.count(cm); CtMethod[] cms = new CtMethod[num]; int i = 0; while (cm != null) { cms[i++] = (CtMethod)cm; cm = cm.next; } return cms; } public CtMethod getDeclaredMethod(String name) throws NotFoundException { CtMember m = getMethodsCache(); while (m != null) { if (m.getName().equals(name)) return (CtMethod)m; m = m.next; } throw new NotFoundException(name + "(..) is not found in " + getName()); } public CtMethod getDeclaredMethod(String name, CtClass[] params) throws NotFoundException { String desc = Descriptor.ofParameters(params); CtMethod m = (CtMethod)getMethodsCache(); while (m != null) { if (m.getName().equals(name) && m.getMethodInfo2().getDescriptor().startsWith(desc)) return m; m = (CtMethod)m.next; } throw new NotFoundException(name + "(..) is not found in " + getName()); } protected CtMember getMethodsCache() { if (methodsCache == null) { List list = getClassFile2().getMethods(); int n = list.size(); CtMember allMethods = null; CtMethod tail = null; for (int i = 0; i < n; ++i) { MethodInfo minfo = (MethodInfo)list.get(i); if (minfo.isMethod()) { CtMethod newTail = new CtMethod(minfo, this); allMethods = CtMember.append(allMethods, tail, newTail); tail = newTail; } } methodsCache = allMethods; } return methodsCache; } public void addField(CtField f, String init) throws CannotCompileException { addField(f, CtField.Initializer.byExpr(init)); } public void addField(CtField f, CtField.Initializer init) throws CannotCompileException { checkModify(); if (f.getDeclaringClass() != this) throw new CannotCompileException("cannot add"); if (init == null) init = f.getInit(); if (init != null) { int mod = f.getModifiers(); if (Modifier.isStatic(mod) && Modifier.isFinal(mod)) try { ConstPool cp = getClassFile2().getConstPool(); int index = init.getConstantValue(cp, f.getType()); if (index != 0) { f.getFieldInfo2().addAttribute(new ConstantAttribute(cp, index)); init = null; } } catch (NotFoundException e) {} } getFieldsCache(); fieldsCache = CtField.append(fieldsCache, f); getClassFile2().addField(f.getFieldInfo2()); if (init != null) { FieldInitLink fil = new FieldInitLink(f, init); FieldInitLink link = fieldInitializers; if (link == null) fieldInitializers = fil; else { while (link.next != null) link = link.next; link.next = fil; } } } public void removeField(CtField f) throws NotFoundException { checkModify(); FieldInfo fi = f.getFieldInfo2(); ClassFile cf = getClassFile2(); if (cf.getFields().remove(fi)) { fieldsCache = CtMember.remove(fieldsCache, f); memberRemoved = true; } else throw new NotFoundException(f.toString()); } public CtConstructor makeClassInitializer() throws CannotCompileException { CtConstructor clinit = getClassInitializer(); if (clinit != null) return clinit; checkModify(); ClassFile cf = getClassFile2(); Bytecode code = new Bytecode(cf.getConstPool(), 0, 0); modifyClassConstructor(cf, code, 0, 0); return getClassInitializer(); } public void addConstructor(CtConstructor c) throws CannotCompileException { checkModify(); if (c.getDeclaringClass() != this)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -