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

com.marvelution.maven.components.migration.manager.phases.prepare.GetJdkRevision Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to Marvelution under one or more contributor license 
 * agreements.  See the NOTICE file distributed with this work 
 * for additional information regarding copyright ownership.
 * Marvelution licenses this file to you 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.marvelution.maven.components.migration.manager.phases.prepare;

import java.io.File;
import java.io.FileInputStream;

import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.JavaClass;
import org.codehaus.plexus.util.FileUtils;

import com.marvelution.maven.components.migration.manager.MigrationResult;
import com.marvelution.maven.components.migration.manager.configuration.BuildPlugin;
import com.marvelution.maven.components.migration.manager.configuration.MigrationDescriptor;
import com.marvelution.maven.components.migration.manager.environment.MigrationEnvironment;
import com.marvelution.maven.components.migration.manager.exception.MigrationExecutionException;
import com.marvelution.maven.components.migration.manager.exception.MigrationFailureException;
import com.marvelution.maven.components.migration.manager.phases.AbstractMigrationPhase;
import com.marvelution.utils.StringUtils;
import com.marvelution.utils.io.DirectoryUtils;

/**
 * {@link MigrationPhase} to get the JDK Revision used
 * 
 * @author Mark Rekveld
 */
public class GetJdkRevision extends AbstractMigrationPhase {

	/**
	 * JDK Revision 1.6 version double valua
	 */
	private static final double JAVA_1_6_CLASS_VERSION = 50.0D;

	/**
	 * JDK Revision 1.5 version double valua
	 */
	private static final double JAVA_1_5_CLASS_VERSION = 49.0D;

	/**
	 * JDK Revision 1.4 version double valua
	 */
	private static final double JAVA_1_4_CLASS_VERSION = 47.0D;

	/**
	 * JDK Revision 1.3 version double valua
	 */
	private static final double JAVA_1_3_CLASS_VERSION = 46.0D;

	/**
	 * JDK Revision 1.2 version double valua
	 */
	private static final double JAVA_1_2_CLASS_VERSION = 45.655360000000002D;

	/**
	 * JDK Revision 1.1 version double valua
	 */
	private static final double JAVA_1_1_CLASS_VERSION = 45.299999999999997D;

	/**
	 * {@inheritDoc}
	 */
	public MigrationResult execute(final MigrationDescriptor descriptor, final MigrationEnvironment environment)
			throws MigrationExecutionException, MigrationFailureException {
		final MigrationResult result = new MigrationResult();
		if (StringUtils.isNotEmpty(descriptor.getSourceDirectory())
			|| StringUtils.isNotEmpty(descriptor.getTestSourceDirectory())) {
			getLogger().info("Scanning for .class files to obtain JDK Revision used");
			final String[] classFiles =
				DirectoryUtils.scanDirectoryForFiles(descriptor.getWorkingDirectory(), new String[] {"**/**.class"});
			if (classFiles.length > 0) {
				double maxVersion = 0D;
				for (int i = 0; i < classFiles.length; i++) {
					try {
						final ClassParser classParser =
							new ClassParser(new FileInputStream(new File(descriptor.getWorkingDirectory(),
								classFiles[i])), FileUtils.filename(classFiles[i]));
						final JavaClass javaClass = classParser.parse();
						double classVersion = javaClass.getMajor();
						if (javaClass.getMinor() > 0) {
							classVersion += 1D / javaClass.getMinor();
						}
						if (classVersion > maxVersion) {
							maxVersion = classVersion;
						}
					} catch (Exception e) {
						getLogger().debug("Failed to get JDK revision from class file '" + classFiles[i] + "'", e);
					}
				}
				if (maxVersion >= JAVA_1_6_CLASS_VERSION) {
					descriptor.setJdkRevision("1.6");
				} else if (maxVersion >= JAVA_1_5_CLASS_VERSION) {
					descriptor.setJdkRevision("1.5");
				} else if (maxVersion > JAVA_1_4_CLASS_VERSION) {
					descriptor.setJdkRevision("1.4");
				} else if (maxVersion > JAVA_1_3_CLASS_VERSION) {
					descriptor.setJdkRevision("1.3");
				} else if (maxVersion > JAVA_1_2_CLASS_VERSION) {
					descriptor.setJdkRevision("1.2");
				} else if (maxVersion > JAVA_1_1_CLASS_VERSION) {
					descriptor.setJdkRevision("1.1");
				} else if (maxVersion > 0D) {
					descriptor.setJdkRevision("1.0");
				} else {
					descriptor.setJdkRevision("1.4");
				}
				getLogger().info("JDK Revision used: " + descriptor.getJdkRevision());
			} else {
				getLogger().info("No .class files found to analyse; Defaulting to 1.4");
				descriptor.setJdkRevision("1.4");
			}
			final BuildPlugin plugin = new BuildPlugin("org.apache.maven.plugins", "maven-compiler-plugin");
			plugin.addConfigurationItem("source", descriptor.getJdkRevision());
			plugin.addConfigurationItem("target", descriptor.getJdkRevision());
			descriptor.getBuildPlugins().add(plugin);
		} else {
			getLogger().info("No Source and Test Source directory configured. Skipping JDK Revision scan.");
		}
		result.setResultCode(MigrationResult.SUCCESS);
		return result;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy