KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005 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 package org.eclipse.jdt.apt.core.internal.declaration;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.jdt.apt.core.internal.env.BaseProcessorEnv;
19 import org.eclipse.jdt.core.dom.ASTNode;
20 import org.eclipse.jdt.core.dom.CompilationUnit;
21 import org.eclipse.jdt.core.dom.IBinding;
22
23 import com.sun.mirror.declaration.Modifier;
24
25 public abstract class DeclarationImpl extends EclipseDeclarationImpl {
26     
27     /** the type binding corresponding to this declaration */
28     protected final IBinding _binding;
29     DeclarationImpl(final IBinding binding, final BaseProcessorEnv env )
30     {
31         super(env);
32         assert binding != null : "binding cannot be null"; //$NON-NLS-1$
33
_binding = binding;
34     }
35     
36     public boolean equals(Object JavaDoc obj)
37     {
38         if(obj instanceof DeclarationImpl)
39             return _binding.isEqualTo( ((DeclarationImpl)obj)._binding );
40             
41         return false;
42     }
43     
44     public int hashCode(){
45         final String JavaDoc key = getDeclarationBinding().getKey();
46         return key == null ? 0 : key.hashCode();
47     }
48     
49      /**
50      * @return the binding that corresponds to the original declaration.
51      * For parameterized type or raw type, return the generic type declaration binding.
52      * For parameterized method, return the method declaration binding that has the
53      * type parameters not the one with the parameters substituted with type arguments.
54      * In other cases, simply return the same binding.
55      */

56     public abstract IBinding getDeclarationBinding();
57     
58     public Collection JavaDoc<Modifier> getModifiers()
59     {
60         final int modBits = getDeclarationBinding().getModifiers();
61         final List JavaDoc<Modifier> mods = new ArrayList JavaDoc<Modifier>(4);
62         if( org.eclipse.jdt.core.dom.Modifier.isAbstract(modBits) )
63             mods.add(Modifier.ABSTRACT);
64         if( org.eclipse.jdt.core.dom.Modifier.isFinal(modBits) )
65             mods.add(Modifier.FINAL);
66         if( org.eclipse.jdt.core.dom.Modifier.isNative(modBits) )
67             mods.add(Modifier.NATIVE);
68         if( org.eclipse.jdt.core.dom.Modifier.isPrivate(modBits) )
69             mods.add(Modifier.PRIVATE);
70         if( org.eclipse.jdt.core.dom.Modifier.isProtected(modBits) )
71             mods.add(Modifier.PROTECTED);
72         if( org.eclipse.jdt.core.dom.Modifier.isPublic(modBits) )
73             mods.add(Modifier.PUBLIC);
74         if( org.eclipse.jdt.core.dom.Modifier.isStatic(modBits) )
75             mods.add(Modifier.STATIC);
76         if( org.eclipse.jdt.core.dom.Modifier.isStrictfp(modBits) )
77             mods.add(Modifier.STRICTFP);
78         if( org.eclipse.jdt.core.dom.Modifier.isSynchronized(modBits) )
79             mods.add(Modifier.SYNCHRONIZED);
80         if( org.eclipse.jdt.core.dom.Modifier.isTransient(modBits) )
81             mods.add(Modifier.TRANSIENT);
82         if( org.eclipse.jdt.core.dom.Modifier.isVolatile(modBits) )
83             mods.add(Modifier.VOLATILE);
84         return mods;
85     }
86     
87     public boolean isBindingBased(){ return true; }
88  
89     ASTNode getAstNode(){
90         if( !isFromSource() ) return null;
91         return _env.getASTNodeForBinding(getDeclarationBinding());
92     }
93
94     CompilationUnit getCompilationUnit(){
95         if( !isFromSource() ) return null;
96         return _env.getCompilationUnitForBinding(getDeclarationBinding());
97     }
98
99     public IFile getResource(){
100         if( isFromSource() ){
101             final IBinding binding = getDeclarationBinding();
102             return _env.getDeclaringFileForBinding(binding);
103         }
104         return null;
105     }
106
107 }
108
Popular Tags