001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.lib.contract.lang.op.reflect;
028:
029: import java.util.*;
030:
031: import org.cougaar.lib.contract.lang.*;
032: import org.cougaar.lib.contract.lang.op.OpCodes;
033:
034: /**
035: * "this" <code>Op</code> -- returns the current <code>Object</code>/primitive.
036: */
037: public final class ThisOp extends OpImpl {
038:
039: private Class clazz;
040:
041: public ThisOp() {
042: }
043:
044: public final int getID() {
045: return OpCodes.THIS_ID;
046: }
047:
048: public final Op parse(final OpParser p) throws ParseException {
049: Op u1 = p.nextOp();
050: if (u1 != null) {
051: throw new ParseException("\"" + OpCodes.THIS_NAME
052: + "\" expecting zero arguments, but given "
053: + u1.getClass().toString());
054: }
055: // want a single non-"Not" type as the return class
056: // -- this is somewhat of a KLUDGE, since multiple interfaces might
057: // be specified. The real fix would be to allow a List of multiple
058: // "getReturnClass"es
059: TypeList sharedTypeList = p.getTypeList();
060: List knownTypes = sharedTypeList.getKnownTypes();
061: int nTypes = knownTypes.size();
062: Type ti = sharedTypeList.getWanted();
063: int i = -1;
064: while (true) {
065: if (!(ti.isNot())) {
066: Class tiClazz = ti.getClazz();
067: if ((clazz == null)
068: || (clazz.isAssignableFrom(tiClazz))) {
069: clazz = tiClazz;
070: } else {
071: throw new ParseException("\"" + OpCodes.THIS_NAME
072: + "\" unable to handle multiple types:\n"
073: + sharedTypeList + "--PARSER_ERROR");
074: }
075: }
076: if (++i >= nTypes) {
077: if (clazz == null) {
078: throw new ParseException("\"" + OpCodes.THIS_NAME
079: + "\" unable to determine type:\n"
080: + sharedTypeList + "--PARSER_ERROR");
081: }
082: break;
083: }
084: ti = (Type) knownTypes.get(i);
085: }
086: return this ;
087: }
088:
089: public final boolean isReturnBoolean() {
090: return (clazz == Boolean.TYPE);
091: }
092:
093: public final Class getReturnClass() {
094: return clazz;
095: }
096:
097: public final boolean execute(final Object o) {
098: return ((Boolean) o).booleanValue();
099: }
100:
101: public final Object operate(final Object o) {
102: return o;
103: }
104:
105: public final void accept(TreeVisitor visitor) {
106: // (this)
107: visitor.visitWord(OpCodes.THIS_NAME);
108: visitor.visitEnd();
109: }
110: }
|