All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.speedment.common.codegen.internal.java.view.ImportView Maven / Gradle / Ivy

Go to download

A Speedment bundle that shades all dependencies into one jar. This is useful when deploying an application on a server.

The newest version!
/*
 *
 * Copyright (c) 2006-2019, Speedment, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); You may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.speedment.common.codegen.internal.java.view;

import com.speedment.common.codegen.DependencyManager;
import com.speedment.common.codegen.Generator;
import com.speedment.common.codegen.Transform;
import static com.speedment.common.codegen.internal.util.CollectorUtil.joinIfNotEmpty;
import static com.speedment.common.codegen.internal.util.NullUtil.requireNonNulls;
import com.speedment.common.codegen.model.Import;
import static com.speedment.common.codegen.util.Formatting.packageName;
import static com.speedment.common.codegen.util.Formatting.stripGenerics;
import java.lang.reflect.Type;
import java.util.Optional;

/**
 * Transforms from an {@link Import} to java code.
 *
 * @author Emil Forslund
 */
public final class ImportView implements Transform {

    @Override
    public Optional transform(Generator gen, Import model) {
        requireNonNulls(gen, model);
        final String name = stripGenerics(model.getType().getTypeName())
            .replace('$', '.');

        if (!model.getModifiers().isEmpty()
        ||   shouldImport(gen, model.getType())) {
            return Optional.of(
                "import "
                + gen.onEach(model.getModifiers()).collect(joinIfNotEmpty(" ", "", " "))
                + name
                + model.getStaticMember().map(str -> "." + str).orElse("")
                + ";"
            ).filter(x -> {
                gen.getDependencyMgr().load(name);
                return true;
            });
        } else {
            return Optional.empty();
        }
    }

    /**
     * Returns true if the specified type requires an explicit
     * import. If the type has already been imported or is part of a package
     * that does not need to be imported, false is returned.
     *
     * @param gen the generator
     * @param type the type to import
     * @return      true if it should be imported explicitly
     */
    private boolean shouldImport(Generator gen, Type type) {
        final DependencyManager mgr = gen.getDependencyMgr();
        final String typeName = stripGenerics(type.getTypeName());
        
        if (mgr.isIgnored(typeName)) {
            return false;
        }

        if (mgr.isLoaded(typeName)) {
            return false;
        }

        final Optional current = mgr.getCurrentPackage();
        final Optional suggested = packageName(typeName);

        // TODO: Inner classes might still be imported explicitly.
            
        return !(current.isPresent()
            && suggested.isPresent()
            && current.get().equals(suggested.get()));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy