1 30 package org.jruby.runtime; 31 32 import java.io.Serializable ; 33 34 import org.jruby.util.IdUtil; 35 36 49 public final class Visibility implements Serializable { 50 private static final short PUBLIC_VALUE = (short) 1; 51 private static final short PROTECTED_VALUE = (short) 2; 52 private static final short PRIVATE_VALUE = (short) 4; 53 private static final short MODULE_FUNCTION_VALUE = (short) 8; 54 public static final Visibility PUBLIC = new Visibility(PUBLIC_VALUE); 55 public static final Visibility PROTECTED = new Visibility(PROTECTED_VALUE); 56 public static final Visibility PRIVATE = new Visibility(PRIVATE_VALUE); 57 public static final Visibility MODULE_FUNCTION = new Visibility(MODULE_FUNCTION_VALUE); 58 public static final Visibility PUBLIC_PROTECTED = 59 new Visibility((short) (PUBLIC_VALUE | PROTECTED_VALUE)); 60 61 private final short restore; 62 63 static final long serialVersionUID = 2002102900L; 64 65 68 private Visibility(short restore) { 69 this.restore = restore; 70 } 71 72 public boolean isPublic() { 73 return (restore & PUBLIC_VALUE) != 0; 74 } 75 76 public boolean isProtected() { 77 return (restore & PROTECTED_VALUE) != 0; 78 } 79 80 public boolean isPrivate() { 81 return (restore & PRIVATE_VALUE) != 0; 82 } 83 84 public boolean isModuleFunction() { 85 return (restore & MODULE_FUNCTION_VALUE) != 0; 86 } 87 88 public boolean is(Visibility other) { 89 return (restore & other.restore) != 0; 90 } 91 92 public String toString() { 93 switch (restore) { 96 case 1: 97 return "public"; 98 case 2: 99 return "protected"; 100 case 4: 101 return "private"; 102 case 8: 103 return "module_function"; 104 default: 105 return "mixed mask: " + restore; 106 } 107 } 108 109 public String errorMessageFormat(CallType callType, String name) { 110 String format = "undefined method `%s' for %s%s%s"; 111 if (callType == CallType.VARIABLE) { 112 if (IdUtil.isLocal(name)) { 113 format = "undefined local variable or method '%s' for %s%s%s"; 114 } 115 } else if (this == Visibility.PRIVATE && callType == CallType.NORMAL) { 116 format = "private method '%s' called for %s%s%s"; 117 } else if (this == Visibility.PROTECTED) { 118 format = "protected method '%s' called for %s%s%s"; 119 } else { 120 } 121 122 return format; 123 } 124 } 125 | Popular Tags |