<commit_before>package org.apache.maven.plugins.enforcer;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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.
 */

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.enforcer.AbstractBanDependencies;

/**
 * Enforcer rule that will check the bytecode version of each class of each dependency. FIXME : Special jar like
 * Hibernate, that embeds .class files with many different compilers, but are only loaded under right condition, is
 * gonna difficult to handle here. Maybe a solution would be to introduce some kind of exclusion.
 * 
 * @see <a href="http://en.wikipedia.org/wiki/Java_class_file#General_layout">Java class file general layout</a>
 * @since 1.0-alpha-4
 */
public class EnforceBytecodeVersion
    extends AbstractBanDependencies
{
    private static final Map<String, Integer> jdkToMajorVersionNumberMapping = new HashMap<String, Integer>();
    static
    {
        jdkToMajorVersionNumberMapping.put( "1.1", 45 );
        jdkToMajorVersionNumberMapping.put( "1.2", 46 );
        jdkToMajorVersionNumberMapping.put( "1.3", 47 );
        jdkToMajorVersionNumberMapping.put( "1.4", 48 );
        jdkToMajorVersionNumberMapping.put( "1.5", 49 );
        jdkToMajorVersionNumberMapping.put( "1.6", 50 );
        jdkToMajorVersionNumberMapping.put( "1.7", 51 );
        jdkToMajorVersionNumberMapping.put( "1.8", 52 );
    }

    /**
     * JDK version as used for example in the maven-compiler-plugin : 1.5, 1.6 and so on. If in need of more precise
     * configuration please see {@link #maxJavaMajorVersionNumber} and {@link #maxJavaMinorVersionNumber} Mandatory if
     * {@link #maxJavaMajorVersionNumber} not specified.
     */
    private String maxJdkVersion;

    /**
     * If unsure, don't use that parameter. Better look at {@link #maxJdkVersion}. Mandatory if {@link #maxJdkVersion}
     * is not specified. see http://en.wikipedia.org/wiki/Java_class_file#General_layout
     */
    int maxJavaMajorVersionNumber = -1;

    /**
     * This parameter is here for potentially advanced use cases, but it seems like it is actually always 0.
     * 
     * @see #maxJavaMajorVersionNumber
     * @see http://en.wikipedia.org/wiki/Java_class_file#General_layout
     */
    int maxJavaMinorVersionNumber = 0;

    private EnforcerRuleHelper helper;

    @Override
    public void execute( EnforcerRuleHelper helper )
        throws EnforcerRuleException
    {
        computeParameters();

        this.helper = helper;
        super.execute( helper );
    }

    private void computeParameters()
        throws EnforcerRuleException
    {
        if ( maxJdkVersion != null && maxJavaMajorVersionNumber != -1 )
        {
            throw new IllegalArgumentException(
                                                "Only maxJdkVersion or maxJavaMajorVersionNumber configuration parameters should be set. Not both." );
        }
        if ( maxJdkVersion == null && maxJavaMajorVersionNumber == -1 )
        {
            throw new IllegalArgumentException(
                                                "Exactly one of maxJdkVersion or maxJavaMajorVersionNumber options should be set." );
        }
        if ( maxJdkVersion != null )
        {
            Integer needle = jdkToMajorVersionNumberMapping.get( maxJdkVersion );
            if ( needle == null )
            {
                throw new IllegalArgumentException( "Unknown JDK version given. Should be something like \"1.7\"" );
            }
            maxJavaMajorVersionNumber = needle;
        }
        if ( maxJavaMajorVersionNumber == -1 )
        {
            throw new EnforcerRuleException( "maxJavaMajorVersionNumber must be set in the plugin configuration" );
        }
    }

    @SuppressWarnings( "unchecked" )
    @Override
    protected Set<Artifact> checkDependencies( Set/* <Artifact> */dependencies, Log log )
        throws EnforcerRuleException
    {
        Set<Artifact> problematic = new LinkedHashSet<Artifact>();
        for ( Iterator<Artifact> it = dependencies.iterator(); it.hasNext(); )
        {
            Artifact artifact = it.next();
            getLog().debug( "Analyzing artifact " + artifact );
            if ( isBadArtifact( artifact ) )
            {
                getLog().info( "Artifact " + artifact + " contains .class compiled with incorrect version" );
                problematic.add( artifact );
            }
        }<commit_msg>Some checkstyle cleanup.<commit_after>package org.apache.maven.plugins.enforcer;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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.
 */

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.plugin.logging.Log;

/**
 * Enforcer rule that will check the bytecode version of each class of each dependency. FIXME : Special jar like
 * Hibernate, that embeds .class files with many different compilers, but are only loaded under right condition, is
 * gonna difficult to handle here. Maybe a solution would be to introduce some kind of exclusion.
 * 
 * @see <a href="http://en.wikipedia.org/wiki/Java_class_file#General_layout">Java class file general layout</a>
 * @since 1.0-alpha-4
 */
public class EnforceBytecodeVersion
    extends AbstractBanDependencies
{
    private static final Map<String, Integer> JDK_TO_MAJOR_VERSION_NUMBER_MAPPING = new HashMap<String, Integer>();
    static
    {
        JDK_TO_MAJOR_VERSION_NUMBER_MAPPING.put( "1.1", 45 );
        JDK_TO_MAJOR_VERSION_NUMBER_MAPPING.put( "1.2", 46 );
        JDK_TO_MAJOR_VERSION_NUMBER_MAPPING.put( "1.3", 47 );
        JDK_TO_MAJOR_VERSION_NUMBER_MAPPING.put( "1.4", 48 );
        JDK_TO_MAJOR_VERSION_NUMBER_MAPPING.put( "1.5", 49 );
        JDK_TO_MAJOR_VERSION_NUMBER_MAPPING.put( "1.6", 50 );
        JDK_TO_MAJOR_VERSION_NUMBER_MAPPING.put( "1.7", 51 );
        JDK_TO_MAJOR_VERSION_NUMBER_MAPPING.put( "1.8", 52 );
    }

    /**
     * JDK version as used for example in the maven-compiler-plugin : 1.5, 1.6 and so on. If in need of more precise
     * configuration please see {@link #maxJavaMajorVersionNumber} and {@link #maxJavaMinorVersionNumber} Mandatory if
     * {@link #maxJavaMajorVersionNumber} not specified.
     */
    private String maxJdkVersion;

    /**
     * If unsure, don't use that parameter. Better look at {@link #maxJdkVersion}. Mandatory if {@link #maxJdkVersion}
     * is not specified. see http://en.wikipedia.org/wiki/Java_class_file#General_layout
     */
    int maxJavaMajorVersionNumber = -1;

    /**
     * This parameter is here for potentially advanced use cases, but it seems like it is actually always 0.
     * 
     * @see #maxJavaMajorVersionNumber
     * @see http://en.wikipedia.org/wiki/Java_class_file#General_layout
     */
    int maxJavaMinorVersionNumber = 0;

    private EnforcerRuleHelper helper;

    @Override
    public void execute( EnforcerRuleHelper pHelper )
        throws EnforcerRuleException
    {
        computeParameters();

        this.helper = pHelper;
        super.execute( helper );
    }

    private void computeParameters()
        throws EnforcerRuleException
    {
        if ( maxJdkVersion != null && maxJavaMajorVersionNumber != -1 )
        {
            throw new IllegalArgumentException( "Only maxJdkVersion or maxJavaMajorVersionNumber "
                                              + "configuration parameters should be set. Not both." );
        }
        if ( maxJdkVersion == null && maxJavaMajorVersionNumber == -1 )
        {
            throw new IllegalArgumentException( "Exactly one of maxJdkVersion or "
                                              + "maxJavaMajorVersionNumber options should be set." );
        }
        if ( maxJdkVersion != null )
        {
            Integer needle = JDK_TO_MAJOR_VERSION_NUMBER_MAPPING.get( maxJdkVersion );
            if ( needle == null )
            {
                throw new IllegalArgumentException( "Unknown JDK version given. Should be something like \"1.7\"" );
            }
            maxJavaMajorVersionNumber = needle;
        }
        if ( maxJavaMajorVersionNumber == -1 )
        {
            throw new EnforcerRuleException( "maxJavaMajorVersionNumber must be set in the plugin configuration" );
        }
    }

    @SuppressWarnings( "unchecked" )
    @Override
    protected Set<Artifact> checkDependencies( Set/* <Artifact> */dependencies, Log log )
        throws EnforcerRuleException
    {
        Set<Artifact> problematic = new LinkedHashSet<Artifact>();
        for ( Iterator<Artifact> it = dependencies.iterator(); it.hasNext(); )
        {
            Artifact artifact = it.next();
            getLog().debug( "Analyzing artifact " + artifact );
            if ( isBadArtifact( artifact ) )
            {
                getLog().info( "Artifact " + artifact + " contains .class compiled with incorrect version" );
                problematic.add( artifact );
            }
        }
{'fraction_non_alphanumeric': 0.08855138111732835, 'fraction_numerical': 0.009059419131361577, 'mean_word_length': 2.800202497468782, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3875101', 'n_tokens_mistral': 3193, 'n_tokens_neox': 3030, 'n_words': 940}