001: /*
002: * Spoon - http://spoon.gforge.inria.fr/
003: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
004: *
005: * This software is governed by the CeCILL-C License under French law and
006: * abiding by the rules of distribution of free software. You can use, modify
007: * and/or redistribute the software under the terms of the CeCILL-C license as
008: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
009: *
010: * This program is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
013: *
014: * The fact that you are presently reading this means that you have had
015: * knowledge of the CeCILL-C license and that you accept its terms.
016: */
017:
018: package spoon.support.reflect.declaration;
019:
020: import java.lang.annotation.Annotation;
021: import java.lang.annotation.Inherited;
022: import java.util.ArrayList;
023: import java.util.List;
024: import java.util.Set;
025: import java.util.TreeSet;
026:
027: import spoon.reflect.declaration.CtAnnotation;
028: import spoon.reflect.declaration.CtAnonymousExecutable;
029: import spoon.reflect.declaration.CtClass;
030: import spoon.reflect.declaration.CtConstructor;
031: import spoon.reflect.declaration.CtMethod;
032: import spoon.reflect.declaration.CtSimpleType;
033: import spoon.reflect.declaration.CtType;
034: import spoon.reflect.reference.CtTypeReference;
035: import spoon.reflect.visitor.CtVisitor;
036:
037: /**
038: * The implementation for {@link spoon.reflect.declaration.CtClass}.
039: *
040: * @author Renaud Pawlak
041: */
042: public class CtClassImpl<T extends Object> extends CtTypeImpl<T>
043: implements CtClass<T> {
044: private static final long serialVersionUID = 1L;
045:
046: List<CtAnonymousExecutable> anonymousExecutable = new ArrayList<CtAnonymousExecutable>();
047:
048: Set<CtConstructor<T>> constructors = new TreeSet<CtConstructor<T>>();
049:
050: CtTypeReference<?> super Class;
051:
052: public void accept(CtVisitor v) {
053: v.visitCtClass(this );
054: }
055:
056: // @Override
057: // public List<CtField<?>> getAllFields() {
058: // if (getSuperclass() != null && getSuperclass().getDeclaration() != null)
059: // {
060: // List<CtField<?>> fields = new ArrayList<CtField<?>>();
061: // fields.addAll(getSuperclass().getDeclaration().getAllFields());
062: // fields.addAll(getFields());
063: // return fields;
064: // }
065: // return super.getAllFields();
066: // }
067:
068: public Set<CtMethod<?>> getAllMethods() {
069: Set<CtMethod<?>> ret = new TreeSet<CtMethod<?>>();
070: ret.addAll(getMethods());
071:
072: if ((getSuperclass() != null)
073: && (getSuperclass().getDeclaration() != null)) {
074: CtType<?> t = (CtType<?>) getSuperclass().getDeclaration();
075: ret.addAll(t.getMethods());
076: }
077: return ret;
078: }
079:
080: public List<CtAnonymousExecutable> getAnonymousExecutables() {
081: return anonymousExecutable;
082: }
083:
084: public CtConstructor<T> getConstructor(
085: CtTypeReference<?>... parameterTypes) {
086: for (CtConstructor<T> c : constructors) {
087: boolean cont = c.getParameters().size() == parameterTypes.length;
088: for (int i = 0; cont && (i < c.getParameters().size())
089: && (i < parameterTypes.length); i++) {
090: if (!c.getParameters().get(i).getType()
091: .getQualifiedName().equals(
092: parameterTypes[i].getQualifiedName())) {
093: cont = false;
094: }
095: }
096: if (cont) {
097: return c;
098: }
099: }
100: return null;
101: }
102:
103: public Set<CtConstructor<T>> getConstructors() {
104: return constructors;
105: }
106:
107: // TODO : remove useless
108: // @Override
109: // @SuppressWarnings("unchecked")
110: // public List<CtField<?>> getFields() {
111: // return super.getFields();
112: // }
113:
114: public CtTypeReference<?> getSuperclass() {
115: return super Class;
116: }
117:
118: public void setAnonymousExecutables(List<CtAnonymousExecutable> e) {
119: anonymousExecutable.clear();
120: anonymousExecutable.addAll(e);
121: }
122:
123: public void setConstructors(Set<CtConstructor<T>> constructors) {
124: this .constructors = constructors;
125: }
126:
127: public void setSuperclass(CtTypeReference<?> super Class) {
128: this .super Class = super Class;
129: }
130:
131: @Override
132: public Set<CtAnnotation<? extends Annotation>> getAnnotations() {
133: Set<CtAnnotation<? extends Annotation>> annot = super
134: .getAnnotations();
135:
136: if (getSuperclass() != null) {
137: CtSimpleType<?> sup = getSuperclass().getDeclaration();
138: if (sup != null) {
139: for (CtAnnotation<? extends Annotation> a : sup
140: .getAnnotations()) {
141: if (a.getAnnotationType().getAnnotation(
142: Inherited.class) != null) {
143: annot.add(a);
144: }
145: }
146: }
147: }
148: return annot;
149: }
150:
151: public boolean isSubtypeOf(CtTypeReference<?> type) {
152: if ((getSuperclass() != null)
153: && getSuperclass().isSubtypeOf(type)) {
154: return true;
155: }
156: for (CtTypeReference<?> ref : getSuperInterfaces()) {
157: if (ref.isSubtypeOf(type)) {
158: return true;
159: }
160: }
161: return false;
162: }
163: }
|