001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.tools.plugin;
020
021import java.lang.reflect.Method;
022
023import org.apache.maven.plugin.descriptor.PluginDescriptor;
024
025public class PluginDescriptorHelper {
026
027    private static final Method GET_REQUIRED_JAVA_VERSION_METHOD;
028    private static final Method SET_REQUIRED_JAVA_VERSION_METHOD;
029
030    static {
031        Method getMethod = null;
032        Method setMethod = null;
033        try {
034            getMethod = PluginDescriptor.class.getMethod("getRequiredJavaVersion");
035            setMethod = PluginDescriptor.class.getMethod("setRequiredJavaVersion", String.class);
036        } catch (NoSuchMethodException e) {
037            // Methods don't exist in this version of Maven
038        }
039        GET_REQUIRED_JAVA_VERSION_METHOD = getMethod;
040        SET_REQUIRED_JAVA_VERSION_METHOD = setMethod;
041    }
042
043    public static String getRequiredJavaVersion(PluginDescriptor descriptor) {
044        if (descriptor == null) {
045            return null;
046        }
047
048        // First try to use the direct method if available in Maven 4
049        if (GET_REQUIRED_JAVA_VERSION_METHOD != null) {
050            try {
051                return (String) GET_REQUIRED_JAVA_VERSION_METHOD.invoke(descriptor);
052            } catch (Exception e) {
053                // Fall back to the wrapper approach
054            }
055        }
056
057        // Fall back to the wrapper approach for Maven 3
058        if (descriptor instanceof ExtendedPluginDescriptor) {
059            return ((ExtendedPluginDescriptor) descriptor).getRequiredJavaVersion();
060        }
061
062        return null;
063    }
064
065    /**
066     * Sets the required Java version on a plugin descriptor.
067     * <p>
068     * This method works with both Maven 3 and Maven 4:
069     * <ul>
070     *   <li>In Maven 4, it uses the direct method on the PluginDescriptor class</li>
071     *   <li>In Maven 3, it uses the ExtendedPluginDescriptor wrapper</li>
072     * </ul>
073     *
074     * @param descriptor the plugin descriptor
075     * @param requiredJavaVersion the required Java version to set
076     * @return the modified plugin descriptor, or null if the input descriptor was null
077     */
078    public static PluginDescriptor setRequiredJavaVersion(PluginDescriptor descriptor, String requiredJavaVersion) {
079        if (descriptor == null) {
080            return null;
081        }
082
083        // First try to use the direct method if available in Maven 4
084        if (SET_REQUIRED_JAVA_VERSION_METHOD != null) {
085            try {
086                SET_REQUIRED_JAVA_VERSION_METHOD.invoke(descriptor, requiredJavaVersion);
087                return descriptor;
088            } catch (Exception e) {
089                // Fall back to the wrapper approach
090            }
091        }
092
093        // Fall back to the wrapper approach for Maven 3
094        if (!(descriptor instanceof ExtendedPluginDescriptor)) {
095            descriptor = new ExtendedPluginDescriptor(descriptor);
096        }
097        ((ExtendedPluginDescriptor) descriptor).setRequiredJavaVersion(requiredJavaVersion);
098        return descriptor;
099    }
100}