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.reflect.factory;
019:
020: import java.io.File;
021: import java.util.Map;
022: import java.util.TreeMap;
023:
024: import spoon.reflect.Factory;
025: import spoon.reflect.cu.CompilationUnit;
026: import spoon.reflect.cu.Import;
027: import spoon.reflect.reference.CtFieldReference;
028: import spoon.reflect.reference.CtPackageReference;
029: import spoon.reflect.reference.CtTypeReference;
030: import spoon.support.reflect.cu.ImportImpl;
031:
032: /**
033: * A factory to create some evaluation utilities on the Spoon metamodel.
034: */
035: public class CompilationUnitFactory extends SubFactory {
036:
037: private static final long serialVersionUID = 1L;
038:
039: /**
040: * Creates the evaluation factory.
041: */
042: public CompilationUnitFactory(Factory factory) {
043: super (factory);
044: }
045:
046: Map<String, CompilationUnit> compilationUnits = new TreeMap<String, CompilationUnit>();
047:
048: /**
049: * Creates a compilation unit with no associated files.
050: */
051: public CompilationUnit create() {
052: CompilationUnit cu = factory.Core().createCompilationUnit();
053: return cu;
054: }
055:
056: /**
057: * Creates or gets a compilation unit for a given file path.
058: */
059: public CompilationUnit create(String filePath) {
060: CompilationUnit cu = compilationUnits.get(filePath);
061: if (cu == null) {
062: if ("".equals(filePath)) {
063: cu = factory.Core().createVirtualCompilationUnit();
064: return cu;
065: }
066: cu = factory.Core().createCompilationUnit();
067: cu.setFile(new File(filePath));
068: compilationUnits.put(filePath, cu);
069: }
070: return cu;
071: }
072:
073: /**
074: * Creates an import for the given type.
075: */
076: public Import createImport(CtTypeReference<?> type) {
077: return new ImportImpl(type);
078: }
079:
080: /**
081: * Creates an import for the given type.
082: */
083: public Import createImport(Class<?> type) {
084: return new ImportImpl(getFactory().Type().createReference(type));
085: }
086:
087: /**
088: * Creates an import for the given field.
089: */
090: public Import createImport(CtFieldReference<?> field) {
091: return new ImportImpl(field);
092: }
093:
094: /**
095: * Creates an import for the given package.
096: */
097: public Import createImport(CtPackageReference pack) {
098: return new ImportImpl(pack);
099: }
100:
101: }
|