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.core.ext.typeinfo;
017:
018: import java.lang.annotation.Annotation;
019: import java.util.Map;
020:
021: /**
022: * Represents a method declaration.
023: */
024: public class JMethod extends JAbstractMethod {
025:
026: private final JClassType enclosingType;
027:
028: private JType returnType;
029:
030: public JMethod(JClassType enclosingType, String name,
031: int declStart, int declEnd, int bodyStart, int bodyEnd) {
032: this (enclosingType, name, declStart, declEnd, bodyStart,
033: bodyEnd, null);
034: }
035:
036: public JMethod(
037: JClassType enclosingType,
038: String name,
039: int declStart,
040: int declEnd,
041: int bodyStart,
042: int bodyEnd,
043: Map<Class<? extends Annotation>, Annotation> declaredAnnotations) {
044: super (name, declStart, declEnd, bodyStart, bodyEnd,
045: declaredAnnotations);
046: this .enclosingType = enclosingType;
047: enclosingType.addMethod(this );
048: }
049:
050: JMethod(JClassType enclosingType, JMethod srcMethod) {
051: super (srcMethod);
052: this .enclosingType = enclosingType;
053: this .returnType = srcMethod.returnType;
054: }
055:
056: @Override
057: public JClassType getEnclosingType() {
058: return enclosingType;
059: }
060:
061: @Override
062: public String getReadableDeclaration() {
063: return getReadableDeclaration(getModifierBits());
064: }
065:
066: public String getReadableDeclaration(boolean noAccess,
067: boolean noNative, boolean noStatic, boolean noFinal,
068: boolean noAbstract) {
069: int bits = getModifierBits();
070: if (noAccess) {
071: bits &= ~(TypeOracle.MOD_PUBLIC | TypeOracle.MOD_PRIVATE | TypeOracle.MOD_PROTECTED);
072: }
073: if (noNative) {
074: bits &= ~TypeOracle.MOD_NATIVE;
075: }
076: if (noStatic) {
077: bits &= ~TypeOracle.MOD_STATIC;
078: }
079: if (noFinal) {
080: bits &= ~TypeOracle.MOD_FINAL;
081: }
082: if (noAbstract) {
083: bits &= ~TypeOracle.MOD_ABSTRACT;
084: }
085: return getReadableDeclaration(bits);
086: }
087:
088: public JType getReturnType() {
089: return returnType;
090: }
091:
092: public boolean isAbstract() {
093: return 0 != (getModifierBits() & TypeOracle.MOD_ABSTRACT);
094: }
095:
096: @Override
097: public JConstructor isConstructor() {
098: return null;
099: }
100:
101: public boolean isFinal() {
102: return 0 != (getModifierBits() & TypeOracle.MOD_FINAL);
103: }
104:
105: @Override
106: public JMethod isMethod() {
107: return this ;
108: }
109:
110: public boolean isNative() {
111: return 0 != (getModifierBits() & TypeOracle.MOD_NATIVE);
112: }
113:
114: public boolean isStatic() {
115: return 0 != (getModifierBits() & TypeOracle.MOD_STATIC);
116: }
117:
118: public void setReturnType(JType type) {
119: returnType = type;
120: }
121:
122: @Override
123: public String toString() {
124: return getReadableDeclaration();
125: }
126:
127: String getReadableDeclaration(int modifierBits) {
128: String[] names = TypeOracle.modifierBitsToNames(modifierBits);
129: StringBuilder sb = new StringBuilder();
130: for (int i = 0; i < names.length; i++) {
131: sb.append(names[i]);
132: sb.append(" ");
133: }
134: if (getTypeParameters().length > 0) {
135: toStringTypeParams(sb);
136: sb.append(" ");
137: }
138: sb.append(returnType.getParameterizedQualifiedSourceName());
139: sb.append(" ");
140: sb.append(getName());
141:
142: toStringParamsAndThrows(sb);
143:
144: return sb.toString();
145: }
146: }
|