KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jicengine > operation > ArrayConstructionOperation


1 package org.jicengine.operation;
2
3
4 /**
5  *
6  *
7  *
8  * <p>
9  * Copyright (C) 2004 Timo Laitinen
10  * </p>
11  * @author Timo Laitinen
12  * @created 2004-09-20
13  * @since JICE-0.10
14  *
15  */

16
17 public class ArrayConstructionOperation extends InvocationOperation {
18
19     public ArrayConstructionOperation(String JavaDoc signature, Operation componentType, Operation length)
20     {
21         super(signature, componentType, new Operation[]{length});
22     }
23
24     protected Object JavaDoc execute(Object JavaDoc componentType, Object JavaDoc[] arguments) throws OperationException
25     {
26         try {
27             return constructArray((Class JavaDoc) componentType, arguments[0]);
28         } catch (RuntimeException JavaDoc e){
29             throw e;
30         } catch (Exception JavaDoc e){
31             throw new OperationException(e.toString(), e);
32         }
33     }
34
35     /**
36      * instantiates a class.
37      */

38     private Object JavaDoc constructArray(Class JavaDoc componentType, Object JavaDoc size) throws Exception JavaDoc {
39         int intSize;
40         if( size instanceof Integer JavaDoc ){
41             intSize = ((Integer JavaDoc)size).intValue();
42         }
43         else {
44             throw new OperationException("Can't create array, expected the size argument '" + size + "' to be an integer, was '" + size.getClass().getName() + "'");
45         }
46
47         return java.lang.reflect.Array.newInstance(componentType, intSize);
48     }
49
50 }
51
Popular Tags