/src/main/scala/de/tud/cs/st/bat/resolved/ClassFile.scala

http://github.com/Delors/BAT · Scala · 127 lines · 43 code · 21 blank · 63 comment · 5 complexity · 126168484b15da372c1aa305d33fe9e4 MD5 · raw file

  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. */
  33. package de.tud.cs.st.bat
  34. package resolved
  35. import de.tud.cs.st.bat.ACC_FINAL
  36. /**
  37. * Represents a single class file.
  38. *
  39. * @param minorVersion The minor part of this class file's version number.
  40. * @param majorVersion The major part of this class file's version number.
  41. * @param accessFlags This class' access flags. To further analyze the access flags
  42. * either use the corresponding convenience methods (e.g., isEnumDeclaration())
  43. * or the class [[de.tud.cs.st.bat.AccessFlagsIterator]] or the classes which
  44. * inherit from [[de.tud.cs.st.bat.AccessFlag]].
  45. * @param thisClass The type implemented by this class file.
  46. * @param superClass The class from which this class inherits. None, if this
  47. * class file represents java.lang.Object.
  48. * @param interfaces The set of implemented interfaces. May be empty.
  49. * @param fields The set of declared fields. May be empty.
  50. * @param methods The set of declared methods. May be empty.
  51. * @param attributes This class file's reified attributes. Which attributes
  52. * are reified depends on the configuration of the class file reader; e.g.,
  53. * [[de.tud.cs.st.bat.resolved.reader.Java6Framework]].
  54. *
  55. * @author Michael Eichberg
  56. */
  57. case class ClassFile(minorVersion: Int,
  58. majorVersion: Int,
  59. accessFlags: Int,
  60. thisClass: ObjectType,
  61. superClass: Option[ObjectType],
  62. interfaces: Seq[ObjectType],
  63. fields: Fields,
  64. methods: Methods,
  65. attributes: Attributes)
  66. extends CommonAttributes with SourceElement {
  67. import ClassFile._
  68. override def isClassFile = true
  69. override def asClassFile = this
  70. def isAbstract : Boolean = ACC_ABSTRACT element_of accessFlags
  71. def isFinal: Boolean = ACC_FINAL element_of accessFlags
  72. def isPublic: Boolean = ACC_PUBLIC element_of accessFlags
  73. def isClassDeclaration: Boolean = (accessFlags & classCategoryMask) == 0
  74. def isEnumDeclaration: Boolean = (accessFlags & classCategoryMask) == ACC_ENUM.mask
  75. def isInterfaceDeclaration: Boolean = (accessFlags & classCategoryMask) == ACC_INTERFACE.mask
  76. def isAnnotationDeclaration: Boolean = (accessFlags & classCategoryMask) == annotationMask
  77. def enclosingMethod: Option[EnclosingMethod] =
  78. attributes collectFirst { case em: EnclosingMethod ? em }
  79. def innerClasses: Option[InnerClasses] =
  80. attributes collectFirst { case InnerClassTable(ice) ? ice }
  81. /**
  82. * Each class file optionally defines a class signature.
  83. */
  84. def classSignature: Option[ClassSignature] =
  85. attributes collectFirst { case s: ClassSignature ? s }
  86. /**
  87. * The SourceFile attribute is an optional attribute [...]. There can be
  88. * at most one SourceFile attribute.
  89. */
  90. def sourceFile: Option[String] =
  91. attributes collectFirst { case SourceFile(s) ? s }
  92. def sourceDebugExtension: Option[String] =
  93. attributes collectFirst { case SourceDebugExtension(s) ? s }
  94. /**
  95. * All constructors/instance initialization methods defined by this class. (This does not include static initializers.)
  96. */
  97. def constructors: Seq[Method] = methods.view.filter(_.name == "<init>")
  98. def staticInitializer: Option[Method] =
  99. methods.collectFirst({
  100. case method @ Method(_, "<clinit>", MethodDescriptor(Seq(), VoidType), _) ? method
  101. })
  102. }
  103. object ClassFile {
  104. private val classCategoryMask: Int = ACC_INTERFACE.mask | ACC_ANNOTATION.mask | ACC_ENUM.mask
  105. private val annotationMask: Int = ACC_INTERFACE.mask | ACC_ANNOTATION.mask
  106. }