public CtMethod[] getDeclaredMethods(String name) throws NotFoundException { CtMember.Cache memCache = getMembers(); CtMember mth = memCache.methodHead(); CtMember mthTail = memCache.lastMethod(); ArrayList methods = new ArrayList(); while (mth != mthTail) { mth = mth.next(); if (mth.getName().equals(name)) methods.add((CtMethod)mth); } return (CtMethod[]) methods.toArray(new CtMethod[methods.size()]); }
/** * Returns the class declaring the field. */ public CtClass getDeclaringClass() { // this is redundant but for javadoc. return super.getDeclaringClass(); }
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); } }
private static <T extends Annotation> T getAnnotationOrNull(CtMember ctMember, Class<T> annotation) { try { if ( ctMember.hasAnnotation( annotation ) ) { return annotation.cast( ctMember.getAnnotation( annotation ) ); } } catch (ClassNotFoundException cnfe) { // should never happen } return null; }
public String toString() { StringBuffer buffer = new StringBuffer(getClass().getName()); buffer.append("@"); buffer.append(Integer.toHexString(hashCode())); buffer.append("["); buffer.append(Modifier.toString(getModifiers())); extendToString(buffer); buffer.append("]"); return buffer.toString(); }
private CtField getDeclaredField2(String name, String desc) { CtMember.Cache memCache = getMembers(); CtMember field = memCache.fieldHead(); CtMember tail = memCache.lastField(); while (field != tail) { field = field.next(); if (field.getName().equals(name) && (desc == null || desc.equals(field.getSignature()))) return (CtField)field; } return null; }
private static void fixMember(CtMember member, boolean useReflection) { //Make member public if (useReflection) { member.setModifiers(Modifier.setPublic(member.getModifiers())); AnnotationUtil.annotateReflectionAccessor(member, null, null, null); } //Create a RW alias for final fields if (member instanceof CtField && Modifier.isFinal(member.getModifiers())) { try { CtField rwField = new CtField((CtField) member, member.getDeclaringClass()); rwField.setName(member.getName() + "_rw"); rwField.setModifiers(Modifier.setPublic(rwField.getModifiers()) & ~Modifier.FINAL); AnnotationUtil.annotateReflectionAccessor(rwField, member.getName(), null, null); member.getDeclaringClass().addField(rwField); } catch (CannotCompileException e) { System.err.println("Cannot add field '" + member.getName() + "_rw' to " + member.getDeclaringClass().getName()); } } }
/** * Unwraps the joinpoint. * @param member the member associated with the joinpoint. * @param transformationIndex index of the transformation text which, along with * <code>member</code>, identifies the joinpoint to be unwrapped. */ public void unwrap(CtMember member, int transformationIndex) { byte wrapperStatus = getWrapperStatusAttribute(member, transformationIndex); if (wrapperStatus != WRAPPED) { String errorMessage = "Member not wrapped: " + member.getName() + "." + member.getDeclaringClass().getName(); errorMessage += "\nMember status: "; errorMessage += (wrapperStatus == NOT_PREPARED)? "NOT_PREPARED" : "UNWRAPPED"; throw new RuntimeException(errorMessage); } setWrapperStatusAttribute(member, transformationIndex, UNWRAPPED); }
private void nameReplaced() { CtMember.Cache cache = hasMemberCache(); if (cache != null) { CtMember mth = cache.methodHead(); CtMember tail = cache.lastMethod(); while (mth != tail) { mth = mth.next(); mth.nameReplaced(); } } }
/** * Returns true if this member is accessible from the given class. */ public boolean visibleFrom(CtClass clazz) { int mod = getModifiers(); if (Modifier.isPublic(mod)) return true; else if (Modifier.isPrivate(mod)) return clazz == declaringClass; else { // package or protected String declName = declaringClass.getPackageName(); String fromName = clazz.getPackageName(); boolean visible; if (declName == null) visible = fromName == null; else visible = declName.equals(fromName); if (!visible && Modifier.isProtected(mod)) return clazz.subclassOf(declaringClass); return visible; } }
public static Visibility effectiveVisibility(CtMember ctMember) { return leastVisible( effectiveVisibility(ctMember.getDeclaringClass()), fromModifiers(ctMember.getModifiers())); }
private void exToString(StringBuffer buffer, String msg, CtMember head, CtMember tail) { buffer.append(msg); while (head != tail) { head = head.next(); buffer.append(head); buffer.append(", "); } }
private String getName4Map(CtMember member) throws NotFoundException { StringBuffer name = new StringBuffer(); name.append(member.getName()); if ((member instanceof CtMethod) || (member instanceof CtConstructor)) { // Overloaded Methods should have different names CtClass[] param; if (member instanceof CtMethod) { param = ((CtMethod) member).getParameterTypes(); } else if (member instanceof CtConstructor) { param = ((CtConstructor) member).getParameterTypes(); } else { throw new Error("intenal test error"); } name.append("("); for (int i = 0; i < param.length; i++) { if (i != 0) { name.append(" ,"); } name.append(param[i].getName()); } name.append(")"); } return name.toString(); }
/** * Returns true if the class has the specified annotation type. * * @param clz the annotation type. * @return <code>true</code> if the annotation is found, otherwise <code>false</code>. * @since 3.11 */ public boolean hasAnnotation(Class clz) { return hasAnnotation(clz.getName()); }
private CtField getDeclaredField2(String name, String desc) { CtMember.Cache memCache = getMembers(); CtMember field = memCache.fieldHead(); CtMember tail = memCache.lastField(); while (field != tail) { field = field.next(); if (field.getName().equals(name) && (desc == null || desc.equals(field.getSignature()))) return (CtField)field; } return null; }
@Override public String toString() { StringBuffer buffer = new StringBuffer(getClass().getName()); buffer.append("@"); buffer.append(Integer.toHexString(hashCode())); buffer.append("["); buffer.append(Modifier.toString(getModifiers())); extendToString(buffer); buffer.append("]"); return buffer.toString(); }
/** * Wraps the joinpoint. * @param member the member associated with the joinpoint. * @param transformationIndex index of the transformation text which, along with * <code>member</code>, identifies the joinpoint to be wrapped. */ public void wrap(CtMember member, int transformationIndex) { byte wrapperStatus = getWrapperStatusAttribute(member, transformationIndex); if (wrapperStatus != UNWRAPPED) { String errorMessage = "Member not unwrapped: " + member.getName() + "." + member.getDeclaringClass().getName(); errorMessage += "\nMember status: "; errorMessage += (wrapperStatus == NOT_PREPARED)? "NOT_PREPARED" : "WRAPPED"; throw new RuntimeException(errorMessage); } setWrapperStatusAttribute(member, transformationIndex, WRAPPED); }
private void nameReplaced() { CtMember.Cache cache = hasMemberCache(); if (cache != null) { CtMember mth = cache.methodHead(); CtMember tail = cache.lastMethod(); while (mth != tail) { mth = mth.next(); mth.nameReplaced(); } } }
/** * Returns true if this member is accessible from the given class. */ public boolean visibleFrom(CtClass clazz) { int mod = getModifiers(); if (Modifier.isPublic(mod)) return true; else if (Modifier.isPrivate(mod)) return clazz == declaringClass; else { // package or protected String declName = declaringClass.getPackageName(); String fromName = clazz.getPackageName(); boolean visible; if (declName == null) visible = fromName == null; else visible = declName.equals(fromName); if (!visible && Modifier.isProtected(mod)) return clazz.subclassOf(declaringClass); return visible; } }
private void exToString(StringBuffer buffer, String msg, CtMember head, CtMember tail) { buffer.append(msg); while (head != tail) { head = head.next(); buffer.append(head); buffer.append(", "); } }