View Javadoc
Minimize
Table

Bug Overview

linebug prioritybugbug descriptionexisting sinceauthor
76mediumEmptyIfStmtLeeres if-Statement, bei Erfüllung der Bedingung wird keine Anweisung ausgeführt, dieser Code kann entfernt werden.  Details zum Fehler...18.06.2012-10:31Unbekannt
133mediumUselessOverridingMethodDas Überladen dieser Methode ist überflüssig, da die Methode die Implementierung einer Superklasse aufruft.  Details zum Fehler...18.06.2012-10:31UNKNOWN

1   /*
2    * Copyright 2003-2011 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.groovy.ast.expr;
17  
18  import org.codehaus.groovy.GroovyBugError;
19  import org.codehaus.groovy.ast.GroovyCodeVisitor;
20  import org.codehaus.groovy.syntax.Token;
21  
22  /**
23   * Represents one or more local variables. Typically it is a single local variable
24   * declared by name with an expression like "def foo" or with type "String foo". However, 
25   * the multiple assignment feature allows you to create two or more variables using
26   * an expression like: <code>def (x, y) = [1, 2]</code>.
27   * <p/>
28   * You can access the left hand side of a declaration using the
29   * "<code>Expression getLeftExpression()<code>" method. In which case you might then
30   * use <code>instanceof</code> and casting to perform operations specific to a
31   * single local variable (<code>VariableExpression</code>) or for the multiple
32   * assignment case (<code>TupleExpression</code>).
33   * <p/>
34   * Alternatively, if <code>isMultipleAssignmentDeclaration()</code> is <code>false</code>
35   * you can use the method "<code>VariableExpression getVariableExpression()</code>" method.
36   * Similarly, if <code>isMultipleAssignmentDeclaration()</code> is <code>true</code>
37   * you can use the method "<code>TupleExpression getTupleExpression()</code>" method.
38   * Calling either of these expression getters when the "isMultipleAssignment" condition
39   * is not appropriate is unsafe and will result in a <code>ClassCastException</code>.
40   *
41   * @author Jochen Theodorou
42   * @author Hamlet D'Arcy
43   * @version $Revision: 22700 $
44   */
45  public class DeclarationExpression extends BinaryExpression {
46      
47      /**
48       * Creates a DeclarationExpression for VariableExpressions like "def x" or "String y = 'foo'". 
49       * @param left
50       *      the left hand side of a variable declaration
51       * @param operation
52       *      the operation, typically an assignment operator
53       * @param right
54       *      the right hand side of a declaration
55       */ 
56      public DeclarationExpression(VariableExpression left, Token operation, Expression right) {
57          super(left,operation,right);
58      }
59      
60      /**
61       * Creates a DeclarationExpression for Expressions like "def (x, y) = [1, 2]"
62       * @param left
63       *      the left hand side of a declaration. Must be either a VariableExpression or 
64       *      a TupleExpression with at least one element.  
65       * @param operation
66       *       the operation, typically an assignment operator
67       * @param right
68       *       the right hand side of a declaration
69       */ 
70      public DeclarationExpression(Expression left, Token operation, Expression right) {
71          super(left,operation,right);
72          check(left);
73      }
74      
75      private void check(Expression left) {
76 bug overview next bug            if (left instanceof VariableExpression) {  HEALTH4J >>  :  EmptyIfStmt 
77              //nothing
78          } else if (left instanceof TupleExpression) {
79              TupleExpression tuple = (TupleExpression) left;
80              if (tuple.getExpressions().size()==0) throw new GroovyBugError("one element required for left side");
81          } else {
82              throw new GroovyBugError("illegal left expression for declaration: "+left);
83          }
84      }
85  
86      public void visit(GroovyCodeVisitor visitor) {
87          visitor.visitDeclarationExpression(this);
88      }
89  
90      /**
91       * This method returns the left hand side of the declaration cast to the VariableExpression type.
92       * This is an unsafe method to call. In a multiple assignment statement, the left hand side will
93       * be a TupleExpression and a ClassCastException will occur. If you invoke this method then
94       * be sure to invoke isMultipleAssignmentDeclaration() first to check that it is safe to do so. 
95       * If that method returns true then this method is safe to call. 
96       * @return
97       *      left hand side of normal variable declarations
98       * @throws ClassCastException 
99       *      if the left hand side is not a VariableExpression (and is probably a multiple assignment statement).
100      *
101      */
102     public VariableExpression getVariableExpression() {
103         return (VariableExpression) this.getLeftExpression();
104     }
105     
106     /**
107      * This method returns the left hand side of the declaration cast to the TupleExpression type.
108      * This is an unsafe method to call. In a single assignment statement, the left hand side will
109      * be a VariableExpression and a ClassCastException will occur. If you invoke this method then
110      * be sure to invoke isMultipleAssignmentDeclaration() first to check that it is safe to do so. 
111      * If that method returns true then this method is safe to call. 
112      * @return
113      *      left hand side of multiple assignment declarations
114      * @throws ClassCastException 
115      *      if the left hand side is not a TupleExpression (and is probably a VariableExpression).
116      *
117      */
118     public TupleExpression getTupleExpression() {
119         return (TupleExpression) this.getLeftExpression();
120     }
121     
122     /**
123      * This method sets the leftExpression for this BinaryExpression. The parameter must be
124      * either a VariableExpression or a TupleExpression with one or more elements. 
125      * @param leftExpression
126      *      either a VariableExpression or a TupleExpression with one or more elements. 
127      */ 
128     public void setLeftExpression(Expression leftExpression) {
129         check(leftExpression);
130         super.setLeftExpression(leftExpression);
131     }
132     
133 previous bug bug overview       public void setRightExpression(Expression rightExpression) {  HEALTH4J >>  :  UselessOverridingMethod 
134         super.setRightExpression(rightExpression);
135     }
136     
137     public Expression transformExpression(ExpressionTransformer transformer) {
138         Expression ret = new DeclarationExpression(transformer.transform(getLeftExpression()),
139                 getOperation(), transformer.transform(getRightExpression()));
140         ret.setSourcePosition(this);
141         ret.addAnnotations(getAnnotations());
142         ret.setDeclaringClass(getDeclaringClass());
143         return ret;
144     }
145     
146     /**
147      * This method tells you if this declaration is a multiple assignment declaration, which 
148      * has the form "def (x, y) = ..." in Groovy. If this method returns true, then the left
149      * hand side is an ArgumentListExpression. Do not call "getVariableExpression()" on this 
150      * object if this method returns true, instead use "getLeftExpression()". 
151      * @return
152      *      true if this declaration is a multiple assignment declaration, which means the
153      *      left hand side is an ArgumentListExpression. 
154      */ 
155     public boolean isMultipleAssignmentDeclaration() {
156         return getLeftExpression() instanceof TupleExpression;
157     }
158 }
159