001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.dev.jjs.ast;
017:
018: import com.google.gwt.dev.jjs.InternalCompilerException;
019: import com.google.gwt.dev.jjs.SourceInfo;
020:
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: /**
025: * A Java method implementation.
026: */
027: public final class JMethod extends JNode implements HasEnclosingType,
028: HasName, HasType, HasSettableType, CanBeAbstract, CanBeFinal,
029: CanBeSetFinal, CanBeNative, CanBeStatic {
030:
031: /**
032: * References to any methods which this method overrides. This should be an
033: * EXHAUSTIVE list, that is, if C overrides B overrides A, then C's overrides
034: * list will contain both A and B.
035: */
036: public final List<JMethod> overrides = new ArrayList<JMethod>();
037:
038: public final ArrayList<JParameter> params = new ArrayList<JParameter>();
039: public final ArrayList<JClassType> thrownExceptions = new ArrayList<JClassType>();
040: private JAbstractMethodBody body = null;
041: private final JReferenceType enclosingType;
042: private final boolean isAbstract;
043: private boolean isFinal;
044: private final boolean isPrivate;
045: private final boolean isStatic;
046: private final String name;
047: private ArrayList<JType> originalParamTypes;
048: private JType returnType;
049:
050: /**
051: * These are only supposed to be constructed by JProgram.
052: */
053: public JMethod(JProgram program, SourceInfo info, String name,
054: JReferenceType enclosingType, JType returnType,
055: boolean isAbstract, boolean isStatic, boolean isFinal,
056: boolean isPrivate) {
057: super (program, info);
058: this .name = name;
059: this .enclosingType = enclosingType;
060: this .returnType = returnType;
061: this .isAbstract = isAbstract;
062: this .isStatic = isStatic;
063: this .isFinal = isFinal;
064: this .isPrivate = isPrivate;
065: }
066:
067: public void freezeParamTypes() {
068: if (originalParamTypes != null) {
069: throw new InternalCompilerException(
070: "Param types already frozen");
071: }
072: originalParamTypes = new ArrayList<JType>();
073: for (int i = 0; i < params.size(); ++i) {
074: JParameter param = params.get(i);
075: originalParamTypes.add(param.getType());
076: }
077: }
078:
079: public JAbstractMethodBody getBody() {
080: return body;
081: }
082:
083: public JReferenceType getEnclosingType() {
084: return enclosingType;
085: }
086:
087: public String getName() {
088: return name;
089: }
090:
091: public List<JType> getOriginalParamTypes() {
092: if (originalParamTypes == null) {
093: return null;
094: }
095: return originalParamTypes;
096: }
097:
098: public JType getType() {
099: return returnType;
100: }
101:
102: public boolean isAbstract() {
103: return isAbstract;
104: }
105:
106: public boolean isFinal() {
107: return isFinal;
108: }
109:
110: public boolean isNative() {
111: if (body == null) {
112: return false;
113: } else {
114: return body.isNative();
115: }
116: }
117:
118: public boolean isPrivate() {
119: return isPrivate;
120: }
121:
122: public boolean isStatic() {
123: return isStatic;
124: }
125:
126: public void setBody(JAbstractMethodBody body) {
127: if (body != null) {
128: body.setMethod(null);
129: }
130: this .body = body;
131: body.setMethod(this );
132: }
133:
134: public void setFinal(boolean b) {
135: isFinal = b;
136: }
137:
138: public void setType(JType newType) {
139: returnType = newType;
140: }
141:
142: public void traverse(JVisitor visitor, Context ctx) {
143: if (visitor.visit(this , ctx)) {
144: visitor.accept(params);
145: if (body != null) {
146: body = (JAbstractMethodBody) visitor.accept(body);
147: }
148: }
149: visitor.endVisit(this, ctx);
150: }
151:
152: }
|