1 // Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file 2 // for details. All rights reserved. Use of this source code is governed by a 3 // BSD-style license that can be found in the LICENSE file. 4 package com.android.tools.r8.graph; 5 6 import com.android.tools.r8.dex.Constants; 7 8 public class DexAccessFlags { 9 10 private static final String[] ACC_NAMES = { 11 "public", 12 "private", 13 "protected", 14 "static", 15 "final", 16 "synchronized", 17 "volatile(bridge)", 18 "transient(varargs)", 19 "native", 20 "interface", 21 "abstract", 22 "strictfp", 23 "synthetic", 24 "annotation", 25 "enum", 26 "<unused>", 27 "<init>", 28 "synchronized", 29 }; 30 31 private int flags; 32 DexAccessFlags(int flags)33 public DexAccessFlags(int flags) { 34 this.flags = flags; 35 } 36 DexAccessFlags(int... flags)37 public DexAccessFlags(int... flags) { 38 this(combineFlags(flags)); 39 } 40 combineFlags(int[] flags)41 private static int combineFlags(int[] flags) { 42 int combined = 0; 43 for (int flag : flags) { 44 combined |= flag; 45 } 46 return combined; 47 } 48 49 @Override equals(Object other)50 public boolean equals(Object other) { 51 if (other instanceof DexAccessFlags) { 52 return flags == ((DexAccessFlags) other).flags; 53 } 54 return false; 55 } 56 57 @Override hashCode()58 public int hashCode() { 59 return get(); 60 } 61 get()62 public int get() { 63 return flags; 64 } 65 containsAllOf(DexAccessFlags other)66 public boolean containsAllOf(DexAccessFlags other) { 67 return (flags & other.get()) == other.get(); 68 } 69 containsNoneOf(DexAccessFlags other)70 public boolean containsNoneOf(DexAccessFlags other) { 71 return (flags & other.get()) == 0; 72 } 73 isPublic()74 public boolean isPublic() { 75 return isSet(Constants.ACC_PUBLIC); 76 } 77 setPublic()78 public void setPublic() { 79 set(Constants.ACC_PUBLIC); 80 } 81 unsetPublic()82 public void unsetPublic() { 83 unset(Constants.ACC_PUBLIC); 84 } 85 isPrivate()86 public boolean isPrivate() { 87 return isSet(Constants.ACC_PRIVATE); 88 } 89 setPrivate()90 public void setPrivate() { 91 set(Constants.ACC_PRIVATE); 92 } 93 unsetPrivate()94 public void unsetPrivate() { 95 unset(Constants.ACC_PRIVATE); 96 } 97 isProtected()98 public boolean isProtected() { 99 return isSet(Constants.ACC_PROTECTED); 100 } 101 setProtected()102 public void setProtected() { 103 set(Constants.ACC_PROTECTED); 104 } 105 unsetProtected()106 public void unsetProtected() { 107 unset(Constants.ACC_PROTECTED); 108 } 109 isStatic()110 public boolean isStatic() { 111 return isSet(Constants.ACC_STATIC); 112 } 113 setStatic()114 public void setStatic() { 115 set(Constants.ACC_STATIC); 116 } 117 isFinal()118 public boolean isFinal() { 119 return isSet(Constants.ACC_FINAL); 120 } 121 setFinal()122 public void setFinal() { 123 set(Constants.ACC_FINAL); 124 } 125 unsetFinal()126 public void unsetFinal() { 127 unset(Constants.ACC_FINAL); 128 } 129 isSynchronized()130 public boolean isSynchronized() { 131 return isSet(Constants.ACC_SYNCHRONIZED); 132 } 133 setSynchronized()134 public void setSynchronized() { 135 set(Constants.ACC_SYNCHRONIZED); 136 } 137 unsetSynchronized()138 public void unsetSynchronized() { 139 unset(Constants.ACC_SYNCHRONIZED); 140 } 141 isVolatile()142 public boolean isVolatile() { 143 return isSet(Constants.ACC_VOLATILE); 144 } 145 setVolatile()146 public void setVolatile() { 147 set(Constants.ACC_VOLATILE); 148 } 149 isBridge()150 public boolean isBridge() { 151 return isSet(Constants.ACC_BRIDGE); 152 } 153 setBridge()154 public void setBridge() { 155 set(Constants.ACC_BRIDGE); 156 } 157 unsetBridge()158 public void unsetBridge() { 159 unset(Constants.ACC_BRIDGE); 160 } 161 isTransient()162 public boolean isTransient() { 163 return isSet(Constants.ACC_TRANSIENT); 164 } 165 setTransient()166 public void setTransient() { 167 set(Constants.ACC_TRANSIENT); 168 } 169 isVarargs()170 public boolean isVarargs() { 171 return isSet(Constants.ACC_VARARGS); 172 } 173 setVarargs()174 public void setVarargs() { 175 set(Constants.ACC_VARARGS); 176 } 177 isNative()178 public boolean isNative() { 179 return isSet(Constants.ACC_NATIVE); 180 } 181 setNative()182 public void setNative() { 183 set(Constants.ACC_NATIVE); 184 } 185 isInterface()186 public boolean isInterface() { 187 return isSet(Constants.ACC_INTERFACE); 188 } 189 setInterface()190 public void setInterface() { 191 set(Constants.ACC_INTERFACE); 192 } 193 unsetInterface()194 public void unsetInterface() { 195 unset(Constants.ACC_INTERFACE); 196 } 197 isAbstract()198 public boolean isAbstract() { 199 return isSet(Constants.ACC_ABSTRACT); 200 } 201 setAbstract()202 public void setAbstract() { 203 set(Constants.ACC_ABSTRACT); 204 } 205 unsetAbstract()206 public void unsetAbstract() { 207 unset(Constants.ACC_ABSTRACT); 208 } 209 isStrict()210 public boolean isStrict() { 211 return isSet(Constants.ACC_STRICT); 212 } 213 setStrict()214 public void setStrict() { 215 set(Constants.ACC_STRICT); 216 } 217 isSynthetic()218 public boolean isSynthetic() { 219 return isSet(Constants.ACC_SYNTHETIC); 220 } 221 setSynthetic()222 public void setSynthetic() { 223 set(Constants.ACC_SYNTHETIC); 224 } 225 unsetSynthetic()226 public void unsetSynthetic() { 227 unset(Constants.ACC_SYNTHETIC); 228 } 229 isAnnotation()230 public boolean isAnnotation() { 231 return isSet(Constants.ACC_ANNOTATION); 232 } 233 setAnnotation()234 public void setAnnotation() { 235 set(Constants.ACC_ANNOTATION); 236 } 237 isEnum()238 public boolean isEnum() { 239 return isSet(Constants.ACC_ENUM); 240 } 241 setEnum()242 public void setEnum() { 243 set(Constants.ACC_ENUM); 244 } 245 isConstructor()246 public boolean isConstructor() { 247 return isSet(Constants.ACC_CONSTRUCTOR); 248 } 249 setConstructor()250 public void setConstructor() { 251 set(Constants.ACC_CONSTRUCTOR); 252 } 253 unsetConstructor()254 public void unsetConstructor() { 255 unset(Constants.ACC_CONSTRUCTOR); 256 } 257 isDeclaredSynchronized()258 public boolean isDeclaredSynchronized() { 259 return isSet(Constants.ACC_DECLARED_SYNCHRONIZED); 260 } 261 setDeclaredSynchronized()262 public void setDeclaredSynchronized() { 263 set(Constants.ACC_DECLARED_SYNCHRONIZED); 264 } 265 promoteNonPrivateToPublic()266 public void promoteNonPrivateToPublic() { 267 if (!isPrivate()) { 268 flags &= ~Constants.ACC_PROTECTED; 269 flags |= Constants.ACC_PUBLIC; 270 } 271 } 272 promoteToPublic()273 public void promoteToPublic() { 274 flags &= ~Constants.ACC_PROTECTED & ~Constants.ACC_PRIVATE; 275 flags |= Constants.ACC_PUBLIC; 276 } 277 isSet(int flag)278 private boolean isSet(int flag) { 279 return (flags & flag) != 0; 280 } 281 set(int flag)282 private void set(int flag) { 283 flags |= flag; 284 } 285 unset(int flag)286 private void unset(int flag) { 287 flags &= ~flag; 288 } 289 290 @Override toString()291 public String toString() { 292 StringBuilder builder = new StringBuilder(); 293 int flags = this.flags; 294 flags &= ~Constants.ACC_CONSTRUCTOR; // Don't include the constructor flag in the string. 295 for (int i = 0; i < ACC_NAMES.length && flags != 0; i++, flags >>= 1) { 296 if ((flags & 0x1) != 0) { 297 if (builder.length() > 0) { 298 builder.append(' '); 299 } 300 builder.append(ACC_NAMES[i]); 301 } 302 } 303 assert flags == 0; 304 return builder.toString(); 305 } 306 toSmaliString()307 public String toSmaliString() { 308 return toString(); 309 } 310 } 311