Coverage Report - org.argouml.notation.providers.java.OperationNotationJava
 
Classes in this File Line Coverage Branch Coverage Complexity
OperationNotationJava
0%
0/70
0%
0/38
4
 
 1  
 /* $Id: OperationNotationJava.java 18683 2010-08-25 20:38:46Z mvw $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009-2010 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  
  *    mvw
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 2006-2009 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies. This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason. IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.notation.providers.java;
 40  
 
 41  
 import java.util.ArrayList;
 42  
 import java.util.Collection;
 43  
 import java.util.Iterator;
 44  
 import java.util.List;
 45  
 
 46  
 import org.apache.log4j.Logger;
 47  
 import org.argouml.application.events.ArgoEventPump;
 48  
 import org.argouml.application.events.ArgoEventTypes;
 49  
 import org.argouml.application.events.ArgoHelpEvent;
 50  
 import org.argouml.model.Model;
 51  
 import org.argouml.notation.NotationSettings;
 52  
 import org.argouml.notation.providers.OperationNotation;
 53  
 
 54  
 /**
 55  
  * The notation for an operation or reception.
 56  
  * 
 57  
  * @author michiel
 58  
  */
 59  
 public class OperationNotationJava extends OperationNotation {
 60  
 
 61  
     /**
 62  
      * Logger.
 63  
      */
 64  0
     private static final Logger LOG = 
 65  
         Logger.getLogger(OperationNotationJava.class);
 66  
 
 67  
     /**
 68  
      * The constructor.
 69  
      * 
 70  
      * @param operation the operation we represent
 71  
      */
 72  
     public OperationNotationJava(Object operation) {
 73  0
         super(operation);
 74  0
     }
 75  
 
 76  
     /*
 77  
      * @see org.argouml.notation.providers.NotationProvider#parse(java.lang.Object, java.lang.String)
 78  
      */
 79  
     public void parse(Object modelElement, String text) {
 80  0
         ArgoEventPump.fireEvent(new ArgoHelpEvent(
 81  
                 ArgoEventTypes.HELP_CHANGED, this,
 82  
             "Parsing in Java not yet supported"));
 83  0
     }
 84  
 
 85  
     /*
 86  
      * @see org.argouml.notation.providers.NotationProvider#getParsingHelp()
 87  
      */
 88  
     public String getParsingHelp() {
 89  0
         return "Parsing in Java not yet supported";
 90  
     }
 91  
     
 92  
     @Override
 93  
     public String toString(Object modelElement, NotationSettings settings) {
 94  0
         return toString(modelElement);
 95  
     }
 96  
 
 97  
     private String toString(Object modelElement) {
 98  0
         StringBuffer sb = new StringBuffer(80);
 99  0
         String nameStr = null;
 100  0
         boolean constructor = false;
 101  
 
 102  0
         Iterator its = 
 103  
             Model.getFacade().getStereotypes(modelElement).iterator();
 104  0
         String name = "";
 105  0
         while (its.hasNext()) {
 106  0
             Object o = its.next();
 107  0
             name = Model.getFacade().getName(o);
 108  0
             if ("create".equals(name)) {
 109  0
                 break;
 110  
             }
 111  0
         }
 112  0
         if ("create".equals(name)) {
 113  
             // constructor
 114  0
             nameStr = Model.getFacade().getName(
 115  
                     Model.getFacade().getOwner(modelElement));
 116  0
             constructor = true;
 117  
         } else {
 118  0
             nameStr = Model.getFacade().getName(modelElement);
 119  
         }
 120  
         
 121  0
         boolean isReception = Model.getFacade().isAReception(modelElement);
 122  
 
 123  0
         if (! isReception) {
 124  0
             sb.append(generateConcurrency(modelElement));
 125  
         }
 126  0
         sb.append(generateAbstractness(modelElement));
 127  0
         sb.append(NotationUtilityJava.generateChangeability(modelElement));
 128  0
         sb.append(NotationUtilityJava.generateScope(modelElement));
 129  0
         sb.append(NotationUtilityJava.generateVisibility(modelElement));
 130  
 
 131  
         // pick out return type
 132  0
         Collection returnParams = 
 133  
             Model.getCoreHelper().getReturnParameters(modelElement);
 134  
         Object rp;
 135  0
         if (returnParams.size() == 0) {
 136  0
             rp = null;
 137  
         } else {
 138  0
             rp = returnParams.iterator().next();
 139  
         }
 140  0
         if (returnParams.size() > 1)  {
 141  0
             LOG.warn("Java generator only handles one return parameter"
 142  
                     + " - Found " + returnParams.size()
 143  
                     + " for " + Model.getFacade().getName(modelElement));
 144  
         }
 145  0
         if (rp != null && !constructor) {
 146  0
             Object returnType = Model.getFacade().getType(rp);
 147  0
             if (returnType == null) {
 148  0
                 sb.append("void ");
 149  
             } else {
 150  0
                 sb.append(NotationUtilityJava.generateClassifierRef(returnType))
 151  
                     .append(' ');
 152  
             }
 153  
         }
 154  
 
 155  
         // name and params
 156  0
         List params = new ArrayList(
 157  
                 Model.getFacade().getParameters(modelElement));
 158  0
         params.remove(rp);
 159  
 
 160  0
         sb.append(nameStr).append('(');
 161  
 
 162  0
         if (params != null) {
 163  0
             for (int i = 0; i < params.size(); i++) {
 164  0
                 if (i > 0) {
 165  0
                     sb.append(", ");
 166  
                 }
 167  0
                 sb.append(NotationUtilityJava.generateParameter(
 168  
                         params.get(i)));
 169  
             }
 170  
         }
 171  
 
 172  0
         sb.append(')');
 173  
 
 174  0
         Collection c = Model.getFacade().getRaisedSignals(modelElement);
 175  0
         if (!c.isEmpty()) {
 176  0
             Iterator it = c.iterator();
 177  0
             boolean first = true;
 178  0
             while (it.hasNext()) {
 179  0
                 Object signal = it.next();
 180  
 
 181  0
                 if (!Model.getFacade().isAException(signal)) {
 182  0
                     continue;
 183  
                 }
 184  
 
 185  0
                 if (first) {
 186  0
                     sb.append(" throws ");
 187  
                 } else {
 188  0
                     sb.append(", ");
 189  
                 }
 190  
 
 191  0
                 sb.append(Model.getFacade().getName(signal));
 192  0
                 first = false;
 193  0
             }
 194  
         }
 195  
 
 196  0
         return sb.toString();
 197  
     }
 198  
 
 199  
     /**
 200  
      * Generates "synchronized" keyword for guarded operations.
 201  
      * @param op The operation
 202  
      * @return String The synchronized keyword if the operation is guarded,
 203  
      *                else "".
 204  
      */
 205  
     private static String generateConcurrency(Object op) {
 206  0
         if (Model.getFacade().getConcurrency(op) != null
 207  
             && Model.getConcurrencyKind().getGuarded().equals(
 208  
                     Model.getFacade().getConcurrency(op))) {
 209  0
             return "synchronized ";
 210  
         }
 211  0
         return "";
 212  
     }
 213  
 
 214  
     /**
 215  
      * Generate "abstract" keyword for an abstract operation.
 216  
      *
 217  
      * @param op the operation
 218  
      * @return the generated string
 219  
      */
 220  
     private static String generateAbstractness(Object op) {
 221  0
         if (Model.getFacade().isAbstract(op)) {
 222  0
             return "abstract ";
 223  
         }
 224  0
         return "";
 225  
     }
 226  
 }