📄 ctclasstype.java
字号:
InnerClassesAttribute ica = (InnerClassesAttribute)cf.getAttribute(InnerClassesAttribute.tag); if (ica == null) return new CtClass[0]; String thisName = cf.getName(); int n = ica.tableLength(); ArrayList list = new ArrayList(n); for (int i = 0; i < n; i++) { String outer = ica.outerClass(i); /* * If a nested class is local or anonymous, * the outer_class_info_index is 0. */ if (outer == null || outer.equals(thisName)) { String inner = ica.innerClass(i); if (inner != null) list.add(classPool.get(inner)); } } return (CtClass[])list.toArray(new CtClass[list.size()]); } public void setModifiers(int mod) { ClassFile cf = getClassFile2(); if (Modifier.isStatic(mod)) { int flags = cf.getInnerAccessFlags(); if (flags != -1 && (flags & AccessFlag.STATIC) != 0) mod = mod & ~Modifier.STATIC; else throw new RuntimeException("cannot change " + getName() + " into a static class"); } checkModify(); int acc = AccessFlag.of(mod) | AccessFlag.SUPER; cf.setAccessFlags(acc); } public Object[] getAnnotations() throws ClassNotFoundException { return getAnnotations(false); } public Object[] getAvailableAnnotations(){ try { return getAnnotations(true); } catch (ClassNotFoundException e) { throw new RuntimeException("Unexpected exception ", e); } } private Object[] getAnnotations(boolean ignoreNotFound) throws ClassNotFoundException { ClassFile cf = getClassFile2(); AnnotationsAttribute ainfo = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.invisibleTag); AnnotationsAttribute ainfo2 = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.visibleTag); return toAnnotationType(ignoreNotFound, getClassPool(), ainfo, ainfo2); } static Object[] toAnnotationType(boolean ignoreNotFound, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2) throws ClassNotFoundException { Annotation[] anno1, anno2; int size1, size2; if (a1 == null) { anno1 = null; size1 = 0; } else { anno1 = a1.getAnnotations(); size1 = anno1.length; } if (a2 == null) { anno2 = null; size2 = 0; } else { anno2 = a2.getAnnotations(); size2 = anno2.length; } if (!ignoreNotFound){ Object[] result = new Object[size1 + size2]; for (int i = 0; i < size1; i++) result[i] = toAnnoType(anno1[i], cp); for (int j = 0; j < size2; j++) result[j + size1] = toAnnoType(anno2[j], cp); return result; } else{ ArrayList annotations = new ArrayList(); for (int i = 0 ; i < size1 ; i++){ try{ annotations.add(toAnnoType(anno1[i], cp)); } catch(ClassNotFoundException e){} } for (int j = 0; j < size2; j++) { try{ annotations.add(toAnnoType(anno2[j], cp)); } catch(ClassNotFoundException e){} } return annotations.toArray(); } } static Object[][] toAnnotationType(boolean ignoreNotFound, ClassPool cp, ParameterAnnotationsAttribute a1, ParameterAnnotationsAttribute a2, MethodInfo minfo) throws ClassNotFoundException { int numParameters = 0; if (a1 != null) numParameters = a1.numParameters(); else if (a2 != null) numParameters = a2.numParameters(); else numParameters = Descriptor.numOfParameters(minfo.getDescriptor()); Object[][] result = new Object[numParameters][]; for (int i = 0; i < numParameters; i++) { Annotation[] anno1, anno2; int size1, size2; if (a1 == null) { anno1 = null; size1 = 0; } else { anno1 = a1.getAnnotations()[i]; size1 = anno1.length; } if (a2 == null) { anno2 = null; size2 = 0; } else { anno2 = a2.getAnnotations()[i]; size2 = anno2.length; } if (!ignoreNotFound){ result[i] = new Object[size1 + size2]; for (int j = 0; j < size1; ++j) result[i][j] = toAnnoType(anno1[j], cp); for (int j = 0; j < size2; ++j) result[i][j + size1] = toAnnoType(anno2[j], cp); } else{ ArrayList annotations = new ArrayList(); for (int j = 0 ; j < size1 ; j++){ try{ annotations.add(toAnnoType(anno1[j], cp)); } catch(ClassNotFoundException e){} } for (int j = 0; j < size2; j++){ try{ annotations.add(toAnnoType(anno2[j], cp)); } catch(ClassNotFoundException e){} } result[i] = annotations.toArray(); } } return result; } private static Object toAnnoType(Annotation anno, ClassPool cp) throws ClassNotFoundException { try { ClassLoader cl = cp.getClassLoader(); return anno.toAnnotationType(cl, cp); } catch (ClassNotFoundException e) { ClassLoader cl2 = cp.getClass().getClassLoader(); return anno.toAnnotationType(cl2, cp); } } 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 CtClass getSuperclass() throws NotFoundException { String supername = getClassFile2().getSuperclass(); if (supername == null) return null; else return classPool.get(supername); } public void setSuperclass(CtClass clazz) throws CannotCompileException { checkModify(); if (isInterface()) addInterface(clazz); else getClassFile2().setSuperclass(clazz.getName()); } public CtClass[] getInterfaces() throws NotFoundException { String[] ifs = getClassFile2().getInterfaces(); int num = ifs.length; CtClass[] ifc = new CtClass[num]; for (int i = 0; i < num; ++i) ifc[i] = classPool.get(ifs[i]); return ifc; } public void setInterfaces(CtClass[] list) { checkModify(); String[] ifs; if (list == null) ifs = new String[0]; else { int num = list.length; ifs = new String[num]; for (int i = 0; i < num; ++i) ifs[i] = list[i].getName(); } getClassFile2().setInterfaces(ifs); } public void addInterface(CtClass anInterface) { checkModify(); if (anInterface != null) getClassFile2().addInterface(anInterface.getName()); } public CtClass getDeclaringClass() throws NotFoundException { ClassFile cf = getClassFile2(); InnerClassesAttribute ica = (InnerClassesAttribute)cf.getAttribute( InnerClassesAttribute.tag); if (ica == null) return null; String name = getName(); int n = ica.tableLength(); for (int i = 0; i < n; ++i) if (name.equals(ica.innerClass(i))) { String outName = ica.outerClass(i); if (outName != null) return classPool.get(outName); else { // maybe anonymous or local class. EnclosingMethodAttribute ema = (EnclosingMethodAttribute)cf.getAttribute( EnclosingMethodAttribute.tag); if (ema != null) return classPool.get(ema.className()); } } return null; } public CtMethod getEnclosingMethod() throws NotFoundException { ClassFile cf = getClassFile2(); EnclosingMethodAttribute ema = (EnclosingMethodAttribute)cf.getAttribute( EnclosingMethodAttribute.tag); if (ema != null) { CtClass enc = classPool.get(ema.className()); return enc.getMethod(ema.methodName(), ema.methodDescriptor()); } return null; } public CtClass makeNestedClass(String name, boolean isStatic) { if (!isStatic) throw new RuntimeException( "sorry, only nested static class is supported"); checkModify(); CtClass c = classPool.makeNestedClass(getName() + "$" + name); ClassFile cf = getClassFile2(); ClassFile cf2 = c.getClassFile2(); InnerClassesAttribute ica = (InnerClassesAttribute)cf.getAttribute( InnerClassesAttribute.tag); if (ica == null) { ica = new InnerClassesAttribute(cf.getConstPool()); cf.addAttribute(ica); } ica.append(c.getName(), this.getName(), name, (cf2.getAccessFlags() & ~AccessFlag.SUPER) | AccessFlag.STATIC); cf2.addAttribute(ica.copy(cf2.getConstPool(), null)); return c; } public CtField[] getFields() { ArrayList alist = new ArrayList(); getFields(alist, this); return (CtField[])alist.toArray(new CtField[alist.size()]); } 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 cf = ((CtClassType)cc).getFieldsCache(); while (cf != null) { if (!Modifier.isPrivate(cf.getModifiers())) alist.add(cf); cf = cf.next; } } public CtField getField(String name) throws NotFoundException { CtField f = getField2(name); if (f == null) throw new NotFoundException("field: " + name + " in " + getName()); else return f; } CtField getField2(String name) { CtField df = getDeclaredField2(name); if (df != null) return df; try { CtClass[] ifs = getInterfaces(); int num = ifs.length; for (int i = 0; i < num; ++i) { CtField f = ifs[i].getField2(name); if (f != null) return f; } CtClass s = getSuperclass(); if (s != null)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -