From 724b9b93c33f7f9c0fb6628261f43fe960014c17 Mon Sep 17 00:00:00 2001 From: chiba Date: Wed, 9 Nov 2005 06:11:13 +0000 Subject: [PATCH] updated Modifier class. git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@221 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- src/main/javassist/CtMember.java | 25 +++++++++++++++++++++++++ src/main/javassist/Modifier.java | 8 ++++++++ 2 files changed, 33 insertions(+) diff --git a/src/main/javassist/CtMember.java b/src/main/javassist/CtMember.java index 4e0154f0..38846601 100644 --- a/src/main/javassist/CtMember.java +++ b/src/main/javassist/CtMember.java @@ -103,6 +103,31 @@ public abstract class CtMember { */ public CtClass getDeclaringClass() { return declaringClass; } + /** + * 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; + } + } + /** * Obtains the modifiers of the member. * diff --git a/src/main/javassist/Modifier.java b/src/main/javassist/Modifier.java index e41c7259..7b4dbdc8 100644 --- a/src/main/javassist/Modifier.java +++ b/src/main/javassist/Modifier.java @@ -65,6 +65,14 @@ public class Modifier { return (mod & PROTECTED) != 0; } + /** + * Returns true if the modifiers do not include either + * public, protected, or private. + */ + public static boolean isPackage(int mod) { + return (mod & (PUBLIC | PRIVATE | PROTECTED)) == 0; + } + /** * Returns true if the modifiers include the static * modifier. -- 2.39.5