PageRenderTime 30ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/argouml-0.34/argouml-java/src/org/argouml/language/java/reveng/classfile/PackageContext.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 183 lines | 85 code | 16 blank | 82 comment | 31 complexity | 4d58ac1c1ebbc0779d74c4698312ce2a MD5 | raw file
  1. /* $Id: PackageContext.java 256 2010-01-12 19:02:27Z linus $
  2. *****************************************************************************
  3. * Copyright (c) 2009 Contributors - see below
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution, and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * thn
  11. *****************************************************************************
  12. *
  13. * Some portions of this file was previously release using the BSD License:
  14. */
  15. // Copyright (c) 2003-2007 The Regents of the University of California. All
  16. // Rights Reserved. Permission to use, copy, modify, and distribute this
  17. // software and its documentation without fee, and without a written
  18. // agreement is hereby granted, provided that the above copyright notice
  19. // and this paragraph appear in all copies. This software program and
  20. // documentation are copyrighted by The Regents of the University of
  21. // California. The software program and documentation are supplied "AS
  22. // IS", without any accompanying services from The Regents. The Regents
  23. // does not warrant that the operation of the program will be
  24. // uninterrupted or error-free. The end-user understands that the program
  25. // was developed for research purposes and is advised not to rely
  26. // exclusively on the program for any reason. IN NO EVENT SHALL THE
  27. // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
  28. // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
  29. // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
  30. // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
  31. // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
  32. // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  33. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
  34. // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
  35. // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
  36. // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  37. package org.argouml.language.java.reveng.classfile;
  38. import org.apache.log4j.Logger;
  39. import org.argouml.model.Facade;
  40. import org.argouml.model.Model;
  41. /**
  42. * This context is a package.
  43. *
  44. * @author Marcus Andersson
  45. */
  46. class PackageContext extends Context {
  47. static final Logger LOG = Logger.getLogger(PackageContext.class);
  48. /** The package this context represents. */
  49. private Object mPackage;
  50. /** The java style name of the package. */
  51. private String javaName;
  52. /**
  53. Create a new context from a package.
  54. @param base Based on this context.
  55. @param thePackage Represents this package.
  56. */
  57. public PackageContext(Context base, Object thePackage) {
  58. super(base);
  59. this.mPackage = thePackage;
  60. javaName = getJavaName(thePackage);
  61. }
  62. public Object getInterface(String name)
  63. throws ClassifierNotFoundException {
  64. return get(name, true);
  65. }
  66. /**
  67. * Get a classifier from the model. If it is not in the model, try
  68. * to find it with the CLASSPATH. If found, in the classpath, the
  69. * classifier is created and added to the model. If not found at
  70. * all, a datatype is created and added to the model.
  71. *
  72. * @param name The name of the classifier to find.
  73. * @return Found classifier.
  74. * @throws ClassifierNotFoundException if classifier couldn't be located
  75. */
  76. public Object get(String name)
  77. throws ClassifierNotFoundException {
  78. return get(name, false);
  79. }
  80. /**
  81. * Get a classifier from the model. If it is not in the model, try
  82. * to find it with the CLASSPATH. If found, in the classpath, the
  83. * classifier is created and added to the model. If not found at
  84. * all, a datatype is created and added to the model.
  85. *
  86. * @param name The name of the classifier to find.
  87. * @return Found classifier.
  88. * @throws ClassifierNotFoundException if classifier couldn't be located
  89. */
  90. public Object get(String name, boolean interfacesOnly)
  91. throws ClassifierNotFoundException {
  92. // Search in model
  93. Object mClassifier = Model.getFacade().lookupIn(mPackage, name);
  94. if (mClassifier == null) {
  95. Class classifier;
  96. String clazzName = name;
  97. // Special case for model
  98. if (!Model.getFacade().isAModel(mPackage)) {
  99. clazzName = javaName + "." + name;
  100. }
  101. classifier = findClass(clazzName, interfacesOnly);
  102. if (classifier != null) {
  103. if (classifier.isInterface()) {
  104. mClassifier = Model.getCoreFactory().buildInterface(name,
  105. mPackage);
  106. } else {
  107. mClassifier = Model.getCoreFactory().buildClass(name,
  108. mPackage);
  109. }
  110. if (mClassifier != null) {
  111. setGeneratedTag(mClassifier);
  112. }
  113. }
  114. }
  115. if (mClassifier == null) {
  116. // Continue the search through the rest of the model
  117. if (getContext() != null) {
  118. mClassifier = getContext().get(name, interfacesOnly);
  119. } else {
  120. // Check for java data types
  121. if (!interfacesOnly
  122. && name.equals("int")
  123. || name.equals("long")
  124. || name.equals("short")
  125. || name.equals("byte")
  126. || name.equals("char")
  127. || name.equals("float")
  128. || name.equals("double")
  129. || name.equals("boolean")
  130. || name.equals("void")
  131. // How do I represent arrays in UML?
  132. || name.indexOf("[]") != -1) {
  133. mClassifier =
  134. Model.getCoreFactory()
  135. .buildDataType(name, mPackage);
  136. }
  137. }
  138. }
  139. if (mClassifier == null) {
  140. throw new ClassifierNotFoundException(name);
  141. }
  142. return mClassifier;
  143. }
  144. // Historically this used the value "yes", but all existing
  145. // code only checks for the presence of the tag, not its value
  146. private static final String GENERATED_TAG_VALUE = "true";
  147. /**
  148. * Set the tagged value which indicates this element was
  149. * generated as a result of reverse engineering.
  150. *
  151. * @param element the ModelElement to set the tag on
  152. */
  153. private void setGeneratedTag(Object element) {
  154. Object tv =
  155. Model.getFacade().getTaggedValue(element, Facade.GENERATED_TAG);
  156. if (tv == null) {
  157. Model.getExtensionMechanismsHelper().addTaggedValue(
  158. element,
  159. Model.getExtensionMechanismsFactory().buildTaggedValue(
  160. Facade.GENERATED_TAG, GENERATED_TAG_VALUE));
  161. } else {
  162. Model.getExtensionMechanismsHelper().setValueOfTag(
  163. tv, GENERATED_TAG_VALUE);
  164. }
  165. }
  166. }