r796 - in trunk/eugene/src/main/java/org/nuiton/eugene: . java
Author: fdesbois Date: 2010-01-17 21:28:34 +0100 (Sun, 17 Jan 2010) New Revision: 796 Added: trunk/eugene/src/main/java/org/nuiton/eugene/java/ImportsManager.java trunk/eugene/src/main/java/org/nuiton/eugene/java/package-info.java Modified: trunk/eugene/src/main/java/org/nuiton/eugene/ImportsManager.java Log: - Move ImportsManager (the old one is deprecated) - Add package-info for org.nuiton.eugene.java package Modified: trunk/eugene/src/main/java/org/nuiton/eugene/ImportsManager.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/ImportsManager.java 2010-01-17 18:35:57 UTC (rev 795) +++ trunk/eugene/src/main/java/org/nuiton/eugene/ImportsManager.java 2010-01-17 20:28:34 UTC (rev 796) @@ -34,8 +34,9 @@ * generated code. * * @author thimel, chemit - * TODO-TC20091217 Move this to java package, this is specific to java generation + * @deprecated This class was moved to {@link org.nuiton.eugene.java} package */ +@Deprecated public class ImportsManager { private static Set<String> primitiveTypes; Copied: trunk/eugene/src/main/java/org/nuiton/eugene/java/ImportsManager.java (from rev 795, trunk/eugene/src/main/java/org/nuiton/eugene/ImportsManager.java) =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/java/ImportsManager.java (rev 0) +++ trunk/eugene/src/main/java/org/nuiton/eugene/java/ImportsManager.java 2010-01-17 20:28:34 UTC (rev 796) @@ -0,0 +1,182 @@ +/* + * *##% + * EUGene :: EUGene + * Copyright (C) 2004 - 2009 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * ##%* + */ +package org.nuiton.eugene.java; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Class used in generators that allows to manage easily imports. A first-pass + * allow to register imports, and in a second-pass, returns the type to use in + * generated code. + * + * @author thimel, chemit + * @version $Revision$ + + * Mise a jour: $Date$ par : + * $Author$ + * @since 2.0.0 + */ +public class ImportsManager { + + private static Set<String> primitiveTypes; + + static { + primitiveTypes = new HashSet<String>(); + + primitiveTypes.add("byte"); + primitiveTypes.add("Byte"); + primitiveTypes.add("short"); + primitiveTypes.add("Short"); + primitiveTypes.add("int"); + primitiveTypes.add("Integer"); + primitiveTypes.add("long"); + primitiveTypes.add("Long"); + primitiveTypes.add("float"); + primitiveTypes.add("Float"); + primitiveTypes.add("double"); + primitiveTypes.add("Double"); + + primitiveTypes.add("char"); + primitiveTypes.add("Char"); + primitiveTypes.add("String"); + + primitiveTypes.add("boolean"); + primitiveTypes.add("Boolean"); + + primitiveTypes.add("void"); + + } + private Map<String, String> imports = new HashMap<String, String>(); + private State state = State.FILLING; + + /** + * From the given class, add it to the imports list. + * @param clazz the class to import + * @return true if import add was successful + * @see ImportsManager#addImport(String) + */ + public boolean addImport(Class<?> clazz) { + return addImport(clazz.getName()); + } + + /** + * From the given fqn (fully qualified name), add it to the imports list. + * If there is a conflict adding this import, will return false. + * If reading of the imports has started, this method will return false, + * unless type does not need to be imported. + * @param fqn the fully qualified name to import + * @return true if import add was successful + */ + public boolean addImport(String fqn) { + // if no package don't include it + if (fqn.indexOf(".") == -1) { + return true; + } + + // Exclude java.lang classes + if (fqn == null || fqn.trim().isEmpty() || + (fqn.startsWith("java.lang.") && fqn.lastIndexOf(".") == 9)) { + return true; + } + // Exclude primitive types + if (primitiveTypes.contains(fqn)) { + return true; + } + // Reject generics + if (fqn.indexOf("<") != -1 || fqn.indexOf(">") != -1) { + return false; + } + String name = fqn.substring(fqn.lastIndexOf(".") + 1); + String inPlaceFqn = imports.get(name); + if (inPlaceFqn == null) { + // Someone has started to read imports, impossible to add some more + if (state == State.READING) { + return false; + } else { + imports.put(name, fqn); + return true; + } + } + // if fqn is not the same, return false. Otherwise, no need to override. + return inPlaceFqn.equals(fqn); + } + + /** + * Accorging to the already added types, returns the type to write in file. + * If there is a conflict, returns the fully qualified name, otherwise + * returns the simple name + * @param fqn the fully qualified name to add + * @return the fqn or simple name according to in-place imports + */ + public String getType(String fqn) { + boolean importResult = addImport(fqn); + if (!importResult) { + // There is a conflict, do not use simple name + return fqn; + } else { + // No conflict, use simple name + int packageEndIndex = fqn.lastIndexOf("."); + if (packageEndIndex == -1) { + return fqn; + } else { + return fqn.substring(packageEndIndex + 1); + } + } + } + + /** + * List the imports. This method will remove the useless imports according + * to the given packageName (no need to import a class in the same package) + * @param packageName the current package name (to avoid useless imports) + * @return the imports alphabeticaly sorted + */ + public List<String> getImports(String packageName) { + state = State.READING; + List<String> result = new ArrayList<String>(); + for (String fqn : imports.values()) { + if (!(fqn.lastIndexOf(".") == packageName.length() && fqn.startsWith(packageName + "."))) { + result.add(fqn); + } + } + Collections.sort(result); + return result; + } + + /** + * Method to reset imports list. If imports has been listed, it becomes back + * possible to add imports. + */ + public void clearImports() { + imports.clear(); + state = State.FILLING; + } + + private enum State { + + FILLING, READING + } +} Property changes on: trunk/eugene/src/main/java/org/nuiton/eugene/java/ImportsManager.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:mergeinfo + /branches/1.0.1-Javabuilder/eugene/src/main/java/org/nuiton/eugene/ImportsManager.java:641-651 /branches/1.1.0-Javabuilder/eugene/src/main/java/org/nuiton/eugene/ImportsManager.java:652-681 /branches/eugene-2.0/eugene/src/main/java/org/nuiton/eugene/ImportsManager.java:682-754 Added: trunk/eugene/src/main/java/org/nuiton/eugene/java/package-info.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/java/package-info.java (rev 0) +++ trunk/eugene/src/main/java/org/nuiton/eugene/java/package-info.java 2010-01-17 20:28:34 UTC (rev 796) @@ -0,0 +1,31 @@ +/* + * *##% + * EUGene :: EUGene + * Copyright (C) 2004 - 2009 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * ##%* + */ +/** + * Eugene java package : all specific class for Java generation. + * <pre> + * - ObjectModel Extensions : {@link org.nuiton.eugene.java.AnnotationsManager } and {@link org.nuiton.eugene.java.ImportsManager } + * - Builder : {@link org.nuiton.eugene.java.JavaBuilder } used to fill an ObjectModel in a Java way + * - Transformer : {@link org.nuiton.eugene.java.ObjectModelTransformerToJava } used to transform an xmi ObjectModel to a java one + * - Generator : {@link org.nuiton.eugene.java.JavaGenerator } is a template used to write java files (processed by Nuiton-processor) + * </pre> + */ +package org.nuiton.eugene.java; + Property changes on: trunk/eugene/src/main/java/org/nuiton/eugene/java/package-info.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL"
participants (1)
-
fdesbois@users.nuiton.org