KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > declaration > MethodDeclarationImpl


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 BEA Systems, Inc.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * tyeung@bea.com - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.apt.core.internal.declaration;
13
14 import com.sun.mirror.declaration.MethodDeclaration;
15 import com.sun.mirror.declaration.ParameterDeclaration;
16 import com.sun.mirror.type.TypeMirror;
17 import com.sun.mirror.util.DeclarationVisitor;
18
19 import org.eclipse.jdt.apt.core.internal.env.BaseProcessorEnv;
20 import org.eclipse.jdt.apt.core.internal.util.Factory;
21 import org.eclipse.jdt.core.dom.IMethodBinding;
22 import org.eclipse.jdt.core.dom.ITypeBinding;
23
24 public class MethodDeclarationImpl extends ExecutableDeclarationImpl implements MethodDeclaration
25 {
26     public MethodDeclarationImpl(final IMethodBinding binding,
27                                  final BaseProcessorEnv env)
28     {
29         super(binding, env);
30         assert !binding.isConstructor() : "binding does not represent a method."; //$NON-NLS-1$
31
}
32
33     public TypeMirror getReturnType()
34     {
35         final IMethodBinding methodBinding = getDeclarationBinding();
36         final ITypeBinding retType = methodBinding.getReturnType();
37         final TypeMirror type = Factory.createTypeMirror(retType, _env);
38         if(type == null )
39             return Factory.createErrorClassType(retType);
40         return type;
41     }
42
43     public void accept(DeclarationVisitor visitor)
44     {
45         visitor.visitMethodDeclaration(this);
46     }
47
48     public MirrorKind kind(){ return MirrorKind.METHOD; }
49
50     public String JavaDoc toString()
51     {
52         final StringBuilder JavaDoc buffer = new StringBuilder JavaDoc();
53         final IMethodBinding methodBinding = getDeclarationBinding();
54         final ITypeBinding[] typeParams = methodBinding.getTypeParameters();
55         if( typeParams != null && typeParams.length > 0 ){
56             buffer.append('<');
57             for(int i=0; i<typeParams.length; i++ ){
58                 if( i != 0 )
59                     buffer.append(", "); //$NON-NLS-1$
60
buffer.append(typeParams[i]);
61             }
62             buffer.append('>');
63         }
64
65         if( methodBinding.getReturnType() != null )
66             buffer.append(methodBinding.getReturnType().getName());
67         buffer.append(' ');
68         buffer.append(methodBinding.getName());
69         buffer.append('(');
70         int i=0;
71         for( ParameterDeclaration param : getParameters() ){
72             if( i++ != 0 )
73                 buffer.append(", "); //$NON-NLS-1$
74
buffer.append(param);
75         }
76         buffer.append(')');
77
78         return buffer.toString();
79     }
80 }
81
Popular Tags