Package org.codehaus.plexus.compiler

Examples of org.codehaus.plexus.compiler.CompilerResult


            if ( compiler == null )
            {
                CompilerMessage message = new CompilerMessage( "No compiler is provided in this environment. "
                                                                   + "Perhaps you are running on a JRE rather than a JDK?",
                                                               CompilerMessage.Kind.ERROR );
                return new CompilerResult( false, Collections.singletonList( message ) );
            }
            final String sourceEncoding = config.getSourceEncoding();
            final Charset sourceCharset = sourceEncoding == null ? null : Charset.forName( sourceEncoding );
            final DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<JavaFileObject>();
            final StandardJavaFileManager standardFileManager =
                compiler.getStandardFileManager( collector, null, sourceCharset );

            final Iterable<? extends JavaFileObject> fileObjects =
                standardFileManager.getJavaFileObjectsFromStrings( Arrays.asList( sourceFiles ) );

             /*(Writer out,
             JavaFileManager fileManager,
             DiagnosticListener<? super JavaFileObject> diagnosticListener,
             Iterable<String> options,
             Iterable<String> classes,
             Iterable<? extends JavaFileObject> compilationUnits)*/

            List<String> arguments = Arrays.asList( args );

            final JavaCompiler.CompilationTask task =
                compiler.getTask( null, standardFileManager, collector, arguments, null, fileObjects );
            final Boolean result = task.call();
            final ArrayList<CompilerMessage> compilerMsgs = new ArrayList<CompilerMessage>();
            for ( Diagnostic<? extends JavaFileObject> diagnostic : collector.getDiagnostics() )
            {
                CompilerMessage.Kind kind = convertKind(diagnostic);
                String baseMessage = diagnostic.getMessage( null );
                if ( baseMessage == null )
                {
                    continue;
                }
                JavaFileObject source = diagnostic.getSource();
                String longFileName = source == null ? null : source.toUri().getPath();
                String shortFileName = source == null ? null : source.getName();
                String formattedMessage = baseMessage;
                int lineNumber = Math.max( 0, (int) diagnostic.getLineNumber() );
                int columnNumber = Math.max( 0, (int) diagnostic.getColumnNumber() );
                if ( source != null && lineNumber > 0 )
                {
                    // Some compilers like to copy the file name into the message, which makes it appear twice.
                    String possibleTrimming = longFileName + ":" + lineNumber + ": ";
                    if ( formattedMessage.startsWith( possibleTrimming ) )
                    {
                        formattedMessage = formattedMessage.substring( possibleTrimming.length() );
                    }
                    else
                    {
                        possibleTrimming = shortFileName + ":" + lineNumber + ": ";
                        if ( formattedMessage.startsWith( possibleTrimming ) )
                        {
                            formattedMessage = formattedMessage.substring( possibleTrimming.length() );
                        }
                    }
                }
                compilerMsgs.add(
                    new CompilerMessage( longFileName, kind, lineNumber, columnNumber, lineNumber, columnNumber,
                                         formattedMessage ) );
            }
            if ( result != Boolean.TRUE && compilerMsgs.isEmpty() )
            {
                compilerMsgs.add(
                    new CompilerMessage( "An unknown compilation problem occurred", CompilerMessage.Kind.ERROR ) );
            }

            return new CompilerResult( result, compilerMsgs );
        }
        catch ( Exception e )
        {
            throw new CompilerException( e.getMessage(), e );
        }
View Full Code Here


        String[] sourceFiles = getSourceFiles( config );

        if ( ( sourceFiles == null ) || ( sourceFiles.length == 0 ) )
        {
            return new CompilerResult();
        }

        if ( ( getLogger() != null ) && getLogger().isInfoEnabled() )
        {
            getLogger().info( "Compiling " + sourceFiles.length + " " +
                                  "source file" + ( sourceFiles.length == 1 ? "" : "s" ) +
                                  " to " + destinationDir.getAbsolutePath() );
        }

        String[] args = buildCompilerArguments( config, sourceFiles );

        CompilerResult result;

        if ( config.isFork() )
        {
            String executable = config.getExecutable();
View Full Code Here

        {
            throw new CompilerException( "Error while executing the external compiler.", e );
        }

        boolean success = returnCode == 0;
        return new CompilerResult( success, messages );
    }
View Full Code Here

        {
            throw new CompilerException( "Error while executing the compiler.", e );
        }

        boolean success = ok.intValue() == 0;
        return new CompilerResult( success, messages );
    }
View Full Code Here

        catch ( IOException e )
        {
            throw new CompilerException( "An exception occurred while creating output file", e );
        }
       
        return new CompilerResult( !shouldFail,
            Collections.singletonList( new CompilerMessage( "message 1", CompilerMessage.Kind.OTHER ) ) );
    }
View Full Code Here

        {
            getLog().warn( "File encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING
                               + ", i.e. build is platform dependent!" );
        }

        CompilerResult compilerResult;


        if ( useIncrementalCompilation )
        {
            incrementalBuildHelperRequest.outputDirectory( getOutputDirectory() );

            incrementalBuildHelper.beforeRebuildExecution( incrementalBuildHelperRequest );

            getLog().debug( "incrementalBuildHelper#beforeRebuildExecution" );
        }

        try
        {
            try
            {
                compilerResult = compiler.performCompile( compilerConfiguration );
            }
            catch ( CompilerNotImplementedException cnie )
            {
                List<CompilerError> messages = compiler.compile( compilerConfiguration );
                compilerResult = convertToCompilerResult( messages );
            }
        }
        catch ( Exception e )
        {
            // TODO: don't catch Exception
            throw new MojoExecutionException( "Fatal error compiling", e );
        }

        if ( useIncrementalCompilation )
        {
            if ( incrementalBuildHelperRequest.getOutputDirectory().exists() )
            {
                getLog().debug( "incrementalBuildHelper#afterRebuildExecution" );
                // now scan the same directory again and create a diff
                incrementalBuildHelper.afterRebuildExecution( incrementalBuildHelperRequest );
            }
            else
            {
                getLog().debug(
                    "skip incrementalBuildHelper#afterRebuildExecution as the output directory doesn't exist" );
            }
        }

        List<CompilerMessage> warnings = new ArrayList<CompilerMessage>();
        List<CompilerMessage> errors = new ArrayList<CompilerMessage>();
        List<CompilerMessage> others = new ArrayList<CompilerMessage>();
        for ( CompilerMessage message : compilerResult.getCompilerMessages() )
        {
            if ( message.getKind() == CompilerMessage.Kind.ERROR )
            {
                errors.add( message );
            }
            else if ( message.getKind() == CompilerMessage.Kind.WARNING
                || message.getKind() == CompilerMessage.Kind.MANDATORY_WARNING )
            {
                warnings.add( message );
            }
            else
            {
                others.add( message );
            }
        }

        if ( failOnError && !compilerResult.isSuccess() )
        {
            for ( CompilerMessage message : others )
            {
                assert message.getKind() != CompilerMessage.Kind.ERROR
                    && message.getKind() != CompilerMessage.Kind.WARNING
                    && message.getKind() != CompilerMessage.Kind.MANDATORY_WARNING;
                getLog().info( message.toString() );
            }
            if ( !warnings.isEmpty() )
            {
                getLog().info( "-------------------------------------------------------------" );
                getLog().warn( "COMPILATION WARNING : " );
                getLog().info( "-------------------------------------------------------------" );
                for ( CompilerMessage warning : warnings )
                {
                    getLog().warn( warning.toString() );
                }
                getLog().info( warnings.size() + ( ( warnings.size() > 1 ) ? " warnings " : " warning" ) );
                getLog().info( "-------------------------------------------------------------" );
            }

            if ( !errors.isEmpty() )
            {
                getLog().info( "-------------------------------------------------------------" );
                getLog().error( "COMPILATION ERROR : " );
                getLog().info( "-------------------------------------------------------------" );
                for ( CompilerMessage error : errors )
                {
                    getLog().error( error.toString() );
                }
                getLog().info( errors.size() + ( ( errors.size() > 1 ) ? " errors " : " error" ) );
                getLog().info( "-------------------------------------------------------------" );
            }

            if ( !errors.isEmpty() )
            {
                throw new CompilationFailureException( errors );
            }
            else
            {
                throw new CompilationFailureException( warnings );
            }
        }
        else
        {
            for ( CompilerMessage message : compilerResult.getCompilerMessages() )
            {
                switch ( message.getKind() )
                {
                    case NOTE:
                    case OTHER:
View Full Code Here

    protected CompilerResult convertToCompilerResult( List<CompilerError> compilerErrors )
    {
        if ( compilerErrors == null )
        {
            return new CompilerResult();
        }
        List<CompilerMessage> messages = new ArrayList<CompilerMessage>( compilerErrors.size() );
        boolean success = true;
        for ( CompilerError compilerError : compilerErrors )
        {
            messages.add(
                new CompilerMessage( compilerError.getFile(), compilerError.getKind(), compilerError.getStartLine(),
                                     compilerError.getStartColumn(), compilerError.getEndLine(),
                                     compilerError.getEndColumn(), compilerError.getMessage() ) );
            if ( compilerError.isError() )
            {
                success = false;
            }
        }

        return new CompilerResult( success, messages );
    }
View Full Code Here

TOP

Related Classes of org.codehaus.plexus.compiler.CompilerResult

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.