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

io.polaris.builder.code.Codes Maven / Gradle / Ivy

There is a newer version: 3.2.1
Show newest version
package io.polaris.builder.code;

import io.polaris.builder.code.annotation.*;
import io.polaris.builder.code.config.CodeEnvBuilder;
import io.polaris.builder.code.config.CodeGroupBuilder;
import io.polaris.builder.code.config.ConfigColumn;
import io.polaris.builder.code.config.TypeMapping;
import io.polaris.builder.code.reader.impl.JdbcTablesReader;
import io.polaris.builder.dbv.cfg.DatabaseCfg;
import io.polaris.core.collection.Iterables;
import io.polaris.core.concurrent.Executors;
import org.springframework.core.annotation.AnnotatedElementUtils;

import java.io.IOException;
import java.lang.reflect.AnnotatedElement;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Function;

/**
 * @author Qt
 * @since 1.8
 */
public class Codes {

	public static void generate(Class clazz, String... targets) throws IOException {
		Metadata metadata = parse(clazz);
		generate(metadata, targets);
	}

	public static void generate(Class clazz, int concurrent) throws IOException {
		Metadata metadata = parse(clazz);
		generate(metadata, concurrent);
	}

	private static void generate(Metadata metadata, int concurrent) throws IOException {
		if (concurrent <= 1) {
			generate(metadata);
		} else {
			CodeConfiguration code = metadata.codeConfiguration;
			ThreadPoolExecutor pool = Executors.create(concurrent, "CodeGenerator");
			CountDownLatch latch = new CountDownLatch(concurrent);
			try {
				final List errors = Collections.synchronizedList(new ArrayList<>());
				List[] tables = new List[concurrent];
				{
					int n = 0;
					for (Table table : code.tables()) {
						int idx = (n++) % concurrent;
						(tables[idx] == null ? tables[idx] = new ArrayList<>() : tables[idx]).add(table);
					}
				}
				for (List
list : tables) { if (list != null && !list.isEmpty()) { Table[] array = list.toArray(new Table[list.size()]); pool.execute(() -> { try { generate(metadata, array); } catch (IOException e) { errors.add(e); } finally { latch.countDown(); } }); } } for (; ; ) { try { latch.await(); break; } catch (InterruptedException e) { } } int errCount = errors.size(); if (errCount > 0) { IOException e = errors.get(0); for (int idx = 1; idx < errCount; idx++) { e.addSuppressed(errors.get(idx)); } throw e; } } finally { Executors.shutdown(pool); } } } private static void generate(Metadata metadata, String... targets) throws IOException { CodeConfiguration code = metadata.codeConfiguration; Table[] tables = code.tables(); generate(metadata, tables, targets); } @SuppressWarnings("AlibabaMethodTooLong") private static void generate(Metadata metadata, Table[] tables, String... targets) throws IOException { if (tables.length == 0) { return; } CodeConfiguration code = metadata.codeConfiguration; Map property = new LinkedHashMap<>(); { DefaultProperty defaultProperty = metadata.defaultProperty; if (defaultProperty != null) { for (Property p : defaultProperty.value()) { property.put(p.key(), p.value()); } } for (Property p : code.property()) { property.put(p.key(), p.value()); } } Set mappings = new HashSet<>(); { // 优先自定义映射 for (Mapping mapping : code.mapping()) { mappings.add(new TypeMapping(mapping.jdbcType(), mapping.javaType())); } DefaultMapping defaultMapping = metadata.defaultMapping; if (defaultMapping != null) { for (Mapping mapping : defaultMapping.value()) { mappings.add(new TypeMapping(mapping.jdbcType(), mapping.javaType())); } } } Template[] templates = code.templates(); { if (templates.length == 0) { DefaultTemplate defaultTemplate = metadata.defaultTemplate; if (defaultTemplate != null) { List