/src/main/scala/de/tud/cs/st/bat/resolved/ClassFile.scala
Scala | 127 lines | 43 code | 21 blank | 63 comment | 5 complexity | 126168484b15da372c1aa305d33fe9e4 MD5 | raw file
Possible License(s): Apache-2.0
1/* License (BSD Style License): 2* Copyright (c) 2009, 2011 3* Software Technology Group 4* Department of Computer Science 5* Technische Universität Darmstadt 6* All rights reserved. 7* 8* Redistribution and use in source and binary forms, with or without 9* modification, are permitted provided that the following conditions are met: 10* 11* - Redistributions of source code must retain the above copyright notice, 12* this list of conditions and the following disclaimer. 13* - Redistributions in binary form must reproduce the above copyright notice, 14* this list of conditions and the following disclaimer in the documentation 15* and/or other materials provided with the distribution. 16* - Neither the name of the Software Technology Group or Technische 17* Universität Darmstadt nor the names of its contributors may be used to 18* endorse or promote products derived from this software without specific 19* prior written permission. 20* 21* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 25* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31* POSSIBILITY OF SUCH DAMAGE. 32*/ 33package de.tud.cs.st.bat 34package resolved 35import de.tud.cs.st.bat.ACC_FINAL 36 37/** 38 * Represents a single class file. 39 * 40 * @param minorVersion The minor part of this class file's version number. 41 * @param majorVersion The major part of this class file's version number. 42 * @param accessFlags This class' access flags. To further analyze the access flags 43 * either use the corresponding convenience methods (e.g., isEnumDeclaration()) 44 * or the class [[de.tud.cs.st.bat.AccessFlagsIterator]] or the classes which 45 * inherit from [[de.tud.cs.st.bat.AccessFlag]]. 46 * @param thisClass The type implemented by this class file. 47 * @param superClass The class from which this class inherits. None, if this 48 * class file represents java.lang.Object. 49 * @param interfaces The set of implemented interfaces. May be empty. 50 * @param fields The set of declared fields. May be empty. 51 * @param methods The set of declared methods. May be empty. 52 * @param attributes This class file's reified attributes. Which attributes 53 * are reified depends on the configuration of the class file reader; e.g., 54 * [[de.tud.cs.st.bat.resolved.reader.Java6Framework]]. 55 * 56 * @author Michael Eichberg 57 */ 58case class ClassFile(minorVersion: Int, 59 majorVersion: Int, 60 accessFlags: Int, 61 thisClass: ObjectType, 62 superClass: Option[ObjectType], 63 interfaces: Seq[ObjectType], 64 fields: Fields, 65 methods: Methods, 66 attributes: Attributes) 67 extends CommonAttributes with SourceElement { 68 69 import ClassFile._ 70 71 override def isClassFile = true 72 73 override def asClassFile = this 74 75 def isAbstract : Boolean = ACC_ABSTRACT element_of accessFlags 76 77 def isFinal: Boolean = ACC_FINAL element_of accessFlags 78 79 def isPublic: Boolean = ACC_PUBLIC element_of accessFlags 80 81 def isClassDeclaration: Boolean = (accessFlags & classCategoryMask) == 0 82 83 def isEnumDeclaration: Boolean = (accessFlags & classCategoryMask) == ACC_ENUM.mask 84 85 def isInterfaceDeclaration: Boolean = (accessFlags & classCategoryMask) == ACC_INTERFACE.mask 86 87 def isAnnotationDeclaration: Boolean = (accessFlags & classCategoryMask) == annotationMask 88 89 def enclosingMethod: Option[EnclosingMethod] = 90 attributes collectFirst { case em: EnclosingMethod ? em } 91 92 def innerClasses: Option[InnerClasses] = 93 attributes collectFirst { case InnerClassTable(ice) ? ice } 94 95 /** 96 * Each class file optionally defines a class signature. 97 */ 98 def classSignature: Option[ClassSignature] = 99 attributes collectFirst { case s: ClassSignature ? s } 100 101 /** 102 * The SourceFile attribute is an optional attribute [...]. There can be 103 * at most one SourceFile attribute. 104 */ 105 def sourceFile: Option[String] = 106 attributes collectFirst { case SourceFile(s) ? s } 107 108 def sourceDebugExtension: Option[String] = 109 attributes collectFirst { case SourceDebugExtension(s) ? s } 110 111 /** 112 * All constructors/instance initialization methods defined by this class. (This does not include static initializers.) 113 */ 114 def constructors: Seq[Method] = methods.view.filter(_.name == "<init>") 115 116 def staticInitializer: Option[Method] = 117 methods.collectFirst({ 118 case method @ Method(_, "<clinit>", MethodDescriptor(Seq(), VoidType), _) ? method 119 }) 120} 121 122object ClassFile { 123 124 private val classCategoryMask: Int = ACC_INTERFACE.mask | ACC_ANNOTATION.mask | ACC_ENUM.mask 125 126 private val annotationMask: Int = ACC_INTERFACE.mask | ACC_ANNOTATION.mask 127}