2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.java.api.generator;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.MoreObjects;
13 import com.google.common.collect.HashBasedTable;
14 import com.google.common.collect.Table;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.function.Supplier;
20 import org.opendaylight.mdsal.binding.model.api.CodeGenerator;
21 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
22 import org.opendaylight.mdsal.binding.model.api.Type;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * Generates files with JAVA source codes for every specified type.
29 public final class GeneratorJavaFile {
30 public enum FileKind {
32 * Transient file. It should be generated in target/generated-sources/ directory or similar.
36 * Persistent file. It should be generated in src/main/java/ directory or similar.
41 private static final class GeneratorStringSupplier implements Supplier<String> {
42 private final CodeGenerator generator;
43 private final Type type;
45 GeneratorStringSupplier(final CodeGenerator generator, final Type type) {
46 this.generator = requireNonNull(generator);
47 this.type = requireNonNull(type);
52 return generator.generate(type);
56 public String toString() {
57 return MoreObjects.toStringHelper(this).add("generator", generator).add("type", type).toString();
61 private static final Logger LOG = LoggerFactory.getLogger(GeneratorJavaFile.class);
64 * List of <code>CodeGenerator</code> instances.
66 private final List<CodeGenerator> generators = new ArrayList<>();
69 * Set of <code>Type</code> instances for which the JAVA code is generated.
71 private final Collection<? extends Type> types;
74 * Creates instance of this class with the set of <code>types</code> for which the JAVA code is generated. Generator
75 * instantiated this way uses the default build context, e.g. it will re-generate any and all files. The instances
76 * of concrete JAVA code generator are created.
78 * @param types set of types for which JAVA code should be generated
80 public GeneratorJavaFile(final Collection<? extends Type> types) {
81 this.types = requireNonNull(types);
82 generators.add(new InterfaceGenerator());
83 generators.add(new TOGenerator());
84 generators.add(new EnumGenerator());
85 generators.add(new BuilderGenerator());
88 public Table<FileKind, String, Supplier<String>> generateFileContent(final boolean ignoreDuplicates) {
89 final Table<FileKind, String, Supplier<String>> result = HashBasedTable.create();
90 for (Type type : types) {
91 for (CodeGenerator generator : generators) {
92 if (!generator.isAcceptable(type)) {
96 final FileKind kind = type instanceof GeneratedTransferObject
97 && ((GeneratedTransferObject) type).isUnionTypeBuilder()
98 ? FileKind.PERSISTENT : FileKind.TRANSIENT;
99 final String file = type.getPackageName().replace('.', File.separatorChar)
100 + File.separator + generator.getUnitName(type) + ".java";
102 if (result.contains(kind, file)) {
103 if (ignoreDuplicates) {
104 LOG.warn("Naming conflict for type '{}': file with same name already exists and will not be "
105 + "generated.", type.getFullyQualifiedName());
108 throw new IllegalStateException("Duplicate " + kind + " file '" + file + "' for "
109 + type.getFullyQualifiedName());
112 result.put(kind, file, new GeneratorStringSupplier(generator, type));
120 * Creates the package directory path as concatenation of <code>parentDirectory</code> and parsed
121 * <code>packageName</code>. The parsing of <code>packageName</code> is realized as replacement of the package name
122 * dots with the file system separator.
124 * @param parentDirectory <code>File</code> object with reference to parent directory
125 * @param packageName string with the name of the package
126 * @return <code>File</code> object which refers to the new directory for package <code>packageName</code>
128 public static File packageToDirectory(final File parentDirectory, final String packageName) {
129 if (packageName == null) {
130 throw new IllegalArgumentException("Package Name cannot be NULL!");
133 return new File(parentDirectory, packageName.replace('.', File.separatorChar));