<s> package org . eclipse . jdt . core . util ; import java . io . File ; import java . io . IOException ; import java . util . Enumeration ; import java . util . HashMap ; import java . util . Iterator ; import java . util . LinkedHashSet ; import java . util . Map ; import java . util . PropertyResourceBundle ; import java . util . Set ; import org . eclipse . core . resources . IFile ; import org . eclipse . core . resources . IFolder ; import org . eclipse . core . resources . IProject ; import org . eclipse . core . runtime . CoreException ; import org . eclipse . core . runtime . IPath ; import org . eclipse . jdt . core . IClasspathEntry ; import org . eclipse . jdt . core . IJavaProject ; import org . eclipse . jdt . core . JavaCore ; import org . eclipse . jdt . core . JavaModelException ; import org . eclipse . jdt . internal . compiler . impl . CompilerOptions ; public class CompilerUtils { public static final int IsGrails = 0x0001 ; public static void configureOptionsBasedOnNature ( CompilerOptions compilerOptions , IJavaProject javaProject ) { IProject project = javaProject . getProject ( ) ; try { if ( isGroovyNaturedProject ( project ) ) { compilerOptions . storeAnnotations = true ; compilerOptions . buildGroovyFiles = 2 ; setGroovyClasspath ( compilerOptions , javaProject ) ; if ( isProbablyGrailsProject ( project ) ) { compilerOptions . groovyFlags = IsGrails ; } else { compilerOptions . groovyFlags = 0 ; } } else { compilerOptions . buildGroovyFiles = 1 ; compilerOptions . groovyFlags = 0 ; } } catch ( CoreException e ) { compilerOptions . buildGroovyFiles = 1 ; compilerOptions . groovyFlags = 0 ; } } public static void configureOptionsBasedOnNature ( Map optionMap , IJavaProject javaProject ) { IProject project = javaProject . getProject ( ) ; try { if ( isGroovyNaturedProject ( project ) ) { optionMap . put ( CompilerOptions . OPTIONG_BuildGroovyFiles , CompilerOptions . ENABLED ) ; setGroovyClasspath ( optionMap , javaProject ) ; if ( isProbablyGrailsProject ( project ) ) { optionMap . put ( CompilerOptions . OPTIONG_GroovyFlags , Integer . toString ( IsGrails ) ) ; } else { optionMap . put ( CompilerOptions . OPTIONG_GroovyFlags , "0" ) ; } } else { optionMap . put ( CompilerOptions . OPTIONG_BuildGroovyFiles , CompilerOptions . DISABLED ) ; optionMap . put ( CompilerOptions . OPTIONG_GroovyFlags , "0" ) ; } } catch ( CoreException e ) { e . printStackTrace ( ) ; optionMap . put ( CompilerOptions . OPTIONG_BuildGroovyFiles , CompilerOptions . DISABLED ) ; optionMap . put ( CompilerOptions . OPTIONG_GroovyFlags , "0" ) ; } } private static boolean isProbablyGrailsProject ( IProject project ) { try { IFolder folder = project . getFolder ( "grails-app" ) ; return folder . exists ( ) ; } catch ( Exception e ) { return false ; } } public static void setGroovyClasspath ( CompilerOptions compilerOptions , IJavaProject javaProject ) { Map newOptions = new HashMap ( ) ; setGroovyClasspath ( newOptions , javaProject ) ; compilerOptions . groovyProjectName = javaProject . getProject ( ) . getName ( ) ; if ( ! newOptions . isEmpty ( ) ) { compilerOptions . set ( newOptions ) ; } } public static void setGroovyClasspath ( Map optionMap , IJavaProject javaProject ) { IFile file = javaProject . getProject ( ) . getFile ( "" ) ; if ( file . exists ( ) ) { try { PropertyResourceBundle prb = new PropertyResourceBundle ( file . getContents ( ) ) ; Enumeration e = prb . getKeys ( ) ; while ( e . hasMoreElements ( ) ) { String k = ( String ) e . nextElement ( ) ; String v = ( String ) prb . getObject ( k ) ; v = fixup ( v , javaProject ) ; if ( k . equals ( CompilerOptions . OPTIONG_GroovyClassLoaderPath ) ) { optionMap . put ( CompilerOptions . OPTIONG_GroovyClassLoaderPath , v ) ; } } } catch ( IOException ioe ) { System . err . println ( "" ) ; ioe . printStackTrace ( ) ; } catch ( CoreException ce ) { System . err . println ( "" ) ; ce . printStackTrace ( ) ; } catch ( Throwable t ) { System . err . println ( "" ) ; t . printStackTrace ( ) ; } } else { try { String classpath = calculateClasspath ( javaProject ) ; optionMap . put ( CompilerOptions . OPTIONG_GroovyClassLoaderPath , classpath ) ; } catch ( Throwable t ) { System . err . println ( "" ) ; t . printStackTrace ( ) ; } } optionMap . put ( CompilerOptions . OPTIONG_GroovyProjectName , javaProject . getProject ( ) . getName ( ) ) ; } private static String fixup ( String someString , IJavaProject javaProject ) { if ( someString . startsWith ( "%projhome%" ) ) { someString = javaProject . getProject ( ) . getLocation ( ) . toOSString ( ) + File . separator + someString . substring ( "%projhome%" . length ( ) ) ; } if ( someString . equals ( "" ) ) { someString = calculateClasspath ( javaProject ) ; } return someString ; } private static boolean isGroovyNaturedProject ( IProject project ) throws CoreException { return project . hasNature ( "" ) ; } private static String pathToString ( IPath path , IProject project ) { String realLocation = null ; if ( path != null ) { String prefix = path . segment ( 0 ) ; if ( prefix . equals ( project . getName ( ) ) ) { if ( path . segmentCount ( ) == 1 ) { IPath rawPath = project . getRawLocation ( ) ; if ( rawPath == null ) { System . err . println ( "" + project ) ; } else { realLocation = project . getRawLocation ( ) . toOSString ( ) ; } } else { realLocation = project . getFile ( path . removeFirstSegments ( 1 ) ) . getRawLocation ( ) . toOSString ( ) ; } } else { realLocation = path . toOSString ( ) ; } } return realLocation ; } public static String calculateClasspath ( IJavaProject javaProject ) { try { Set accumulatedPathEntries = new LinkedHashSet ( ) ; IProject project = javaProject . getProject ( ) ; String projectName = project . getName ( ) ; IPath defaultOutputPath = javaProject . getOutputLocation ( ) ; String defaultOutputLocation = pathToString ( defaultOutputPath , project ) ; IClasspathEntry [ ] cpes = javaProject . getResolvedClasspath ( true ) ; if ( cpes != null ) { for ( int i = 0 , max = cpes . length ; i < max ; i ++ ) { IClasspathEntry cpe = cpes [ i ] ; if ( cpe . getEntryKind ( ) == IClasspathEntry . CPE_SOURCE ) { continue ; } IPath cpePath = cpe . getPath ( ) ; String pathElement = null ; String segmentZero = cpePath . segment ( 0 ) ; if ( segmentZero . equals ( projectName ) ) { pathElement = project . getFile ( cpePath . removeFirstSegments ( 1 ) ) . getRawLocation ( ) . toOSString ( ) ; } else { if ( cpe . getEntryKind ( ) == IClasspathEntry . CPE_LIBRARY ) { try { IProject iproject = project . getWorkspace ( ) . getRoot ( ) . getProject ( segmentZero ) ; if ( iproject != null ) { IFile ifile = iproject . getFile ( cpePath . removeFirstSegments ( 1 ) ) ; IPath ipath = ( ifile == null ? null : ifile . getRawLocation ( ) ) ; pathElement = ( ipath == null ? null : ipath . toOSString ( ) ) ; } } catch ( Throwable t ) { t . printStackTrace ( ) ; } } if ( cpe . getEntryKind ( ) == IClasspathEntry . CPE_PROJECT ) { computeDependenciesFromProject ( project , segmentZero , accumulatedPathEntries ) ; } else { if ( pathElement == null ) { pathElement = cpe . getPath ( ) . toOSString ( ) ; } } } if ( pathElement != null ) { accumulatedPathEntries . add ( pathElement ) ; } } accumulatedPathEntries . add ( defaultOutputLocation ) ; StringBuilder sb = new StringBuilder ( ) ; Iterator iter = accumulatedPathEntries . iterator ( ) ; while ( iter . hasNext ( ) ) { sb . append ( ( String ) iter . next ( ) ) ; sb . append ( File . pathSeparator ) ; } String classpath = sb . toString ( ) ; return classpath ; } } catch ( JavaModelException jme ) { System . err . println ( "" + javaProject . getProject ( ) . getName ( ) + ":" ) ; jme . printStackTrace ( ) ; } return "" ; } private static void computeDependenciesFromProject ( IProject baseProject , String otherProject , Set accumulatedPathEntries ) throws JavaModelException { IProject iproject = baseProject . getWorkspace ( ) . getRoot ( ) . getProject ( otherProject ) ; IJavaProject ijp = JavaCore . create ( iproject ) ; accumulatedPathEntries . add ( pathToString ( ijp . getOutputLocation ( ) , iproject ) ) ; IClasspathEntry [ ] cpes = ijp . getResolvedClasspath ( true ) ; if ( cpes != null ) { for ( int j = 0 ; j < cpes . length ; j ++ ) { IClasspathEntry cpe = cpes [ j ] ; if ( cpe . getEntryKind ( ) == IClasspathEntry . CPE_SOURCE ) { continue ; } if ( cpe . isExported ( ) ) { IPath cpePath = cpes [ j ] . getPath ( ) ; String segmentZero = cpePath . segment ( 0 ) ; if ( segmentZero != null && segmentZero . equals ( otherProject ) ) { accumulatedPathEntries . add ( iproject . getFile ( cpePath . removeFirstSegments ( 1 ) ) . getRawLocation ( ) . toOSString ( ) ) ; } else { if ( cpe . getEntryKind ( ) == IClasspathEntry . CPE_PROJECT ) { computeDependenciesFromProject ( baseProject , segmentZero , accumulatedPathEntries ) ; } else { String otherPathElement = null ; if ( segmentZero != null && segmentZero . equals ( iproject . getName ( ) ) ) { otherPathElement = iproject . getFile ( cpePath . removeFirstSegments ( 1 ) ) . getRawLocation ( ) . toOSString ( ) ; } else { otherPathElement = cpePath . toOSString ( ) ; } accumulatedPathEntries . add ( otherPathElement ) ; } } } } } } } </s>