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.util.ArrayList;
021: import java.util.HashSet;
022: import java.util.List;
023: import java.util.Set;
024: import java.util.TreeSet;
025:
026: import spoon.reflect.declaration.CtField;
027: import spoon.reflect.declaration.CtPackage;
028: import spoon.reflect.declaration.CtSimpleType;
029: import spoon.reflect.reference.CtArrayTypeReference;
030: import spoon.reflect.reference.CtTypeReference;
031: import spoon.reflect.visitor.Query;
032: import spoon.reflect.visitor.filter.ReferenceTypeFilter;
033: import spoon.support.builder.SnippetCompiler;
034:
035: public abstract class CtSimpleTypeImpl<T> extends CtNamedElementImpl
036: implements CtSimpleType<T> {
037:
038: public Set<CtTypeReference<?>> getUsedTypes(
039: boolean includeSamePackage) {
040: Set<CtTypeReference<?>> typeRefs = new HashSet<CtTypeReference<?>>();
041: for (CtTypeReference<?> typeRef : Query.getReferences(this ,
042: new ReferenceTypeFilter<CtTypeReference<?>>(
043: CtTypeReference.class))) {
044: if (!(typeRef.isPrimitive()
045: || (typeRef instanceof CtArrayTypeReference)
046: || typeRef.toString().equals(
047: CtTypeReference.NULL_TYPE_NAME) || ((typeRef
048: .getPackage() != null) && "java.lang"
049: .equals(typeRef.getPackage().toString())))
050: && !(!includeSamePackage && typeRef.getPackage()
051: .equals(this .getPackage().getReference()))) {
052: typeRefs.add(typeRef);
053: }
054: }
055: return typeRefs;
056: }
057:
058: List<CtField<?>> fields = new ArrayList<CtField<?>>();
059:
060: Set<CtSimpleType<?>> nestedTypes = new TreeSet<CtSimpleType<?>>();
061:
062: public Class<T> getActualClass() {
063: return getFactory().Type().createReference(this )
064: .getActualClass();
065: }
066:
067: public List<CtField<?>> getAllFields() {
068: return getFields();
069: }
070:
071: public CtSimpleType<?> getDeclaringType() {
072: if (parent instanceof CtSimpleType) {
073: return (CtSimpleType<?>) parent;
074: }
075: return null;
076: }
077:
078: public CtField<?> getField(String name) {
079: for (CtField<?> f : fields) {
080: if (f.getSimpleName().equals(name)) {
081: return f;
082: }
083: }
084: return null;
085: }
086:
087: public List<CtField<?>> getFields() {
088: return fields;
089: }
090:
091: public CtSimpleType<?> getNestedType(String name) {
092: for (CtSimpleType<?> t : nestedTypes) {
093: if (t.getSimpleName().equals(name)) {
094: return t;
095: }
096: }
097: return null;
098: }
099:
100: public Set<CtSimpleType<?>> getNestedTypes() {
101: return nestedTypes;
102: }
103:
104: public CtPackage getPackage() {
105: if (parent instanceof CtPackage) {
106: return (CtPackage) parent;
107: } else if (parent instanceof CtSimpleType) {
108: return ((CtSimpleType<?>) parent).getPackage();
109: } else {
110: return null;
111: }
112: }
113:
114: public String getQualifiedName() {
115: if ((getPackage() != null)
116: && !getPackage().getSimpleName().equals(
117: CtPackage.TOP_LEVEL_PACKAGE_NAME)) {
118: return getPackage().getQualifiedName() + "."
119: + getSimpleName();
120: }
121: return getSimpleName();
122: }
123:
124: @Override
125: public CtTypeReference<T> getReference() {
126: return getFactory().Type().createReference(this );
127: }
128:
129: public boolean isTopLevel() {
130: return (getDeclaringType() == null) && (getPackage() != null);
131: }
132:
133: public void setFields(List<CtField<?>> fields) {
134: this .fields = fields;
135: }
136:
137: public void setNestedTypes(Set<CtSimpleType<?>> nestedTypes) {
138: this .nestedTypes = nestedTypes;
139: }
140:
141: public void compileAndReplaceSnippets() {
142: SnippetCompiler.compileAndReplaceSnippetsIn(this);
143:
144: }
145:
146: }
|