private ClassLoader toClassLoader(FileCollection runtimeClasspath) { List<URL> urls = new ArrayList<URL>(); for ( File file : runtimeClasspath ) { try { urls.add( file.toURI().toURL() ); logger.debug( "Adding classpath entry for " + file.getAbsolutePath() ); } catch (MalformedURLException e) { throw new GradleException( "Unable to resolve classpath entry to URL : " + file.getAbsolutePath(), e ); } } return new URLClassLoader( urls.toArray( new URL[urls.size()] ), Enhancer.class.getClassLoader() ); }
protected String getTaskErrorMessage(SpotlessTask task) throws Exception { try { execute(task); throw new AssertionError("Expected a GradleException"); } catch (GradleException e) { return e.getMessage(); } }
@Override public String transform(GradleException e) { return e.getMessage(); } }), LINE_SEPARATOR + "AND" + LINE_SEPARATOR);
private byte[] doEnhancement(File root, File javaClassFile, Enhancer enhancer) { try { String className = javaClassFile.getAbsolutePath().substring( root.getAbsolutePath().length() + 1, javaClassFile.getAbsolutePath().length() - ".class".length() ).replace( File.separatorChar, '.' ); ByteArrayOutputStream originalBytes = new ByteArrayOutputStream(); FileInputStream fileInputStream = new FileInputStream( javaClassFile ); try { byte[] buffer = new byte[1024]; int length; while ( ( length = fileInputStream.read( buffer ) ) != -1 ) { originalBytes.write( buffer, 0, length ); } } finally { fileInputStream.close(); } return enhancer.enhance( className, originalBytes.toByteArray() ); } catch (Exception e) { throw new GradleException( "Unable to enhance class : " + javaClassFile, e ); } }
@Test public void testGetProject_nothingSet() { AppYamlDeployTargetResolver deployTargetResolver = new AppYamlDeployTargetResolver(cloudSdkOperations); try { deployTargetResolver.getProject(null); Assert.fail(); } catch (GradleException ex) { Assert.assertEquals(AppYamlDeployTargetResolver.PROJECT_ERROR, ex.getMessage()); } }
throw new GradleException( "Error writing to enhanced class [" + file.getName() + "] to file [" + file.getAbsolutePath() + "]", e ); throw new GradleException( "Error opening class file for writing : " + file.getAbsolutePath(), e );
@Test public void testGetVersion_appengineConfig() { AppYamlDeployTargetResolver deployTargetResolver = new AppYamlDeployTargetResolver(cloudSdkOperations); try { deployTargetResolver.getVersion(ConfigReader.APPENGINE_CONFIG); Assert.fail(); } catch (GradleException ex) { Assert.assertEquals(AppYamlDeployTargetResolver.VERSION_ERROR, ex.getMessage()); } }
throw new GradleException("Couldn't find schema files for the variant " + Utils.capitalize(variant) + ". Please" + " ensure a valid schema.json exists under the varian't source sets"); throw new GradleException("Found an ancestor directory to a schema file that contains another schema file." + " Please ensure no schema files exist on the path to another one");
@Test public void testGetVersion_nothingSet() { AppYamlDeployTargetResolver deployTargetResolver = new AppYamlDeployTargetResolver(cloudSdkOperations); try { deployTargetResolver.getVersion(null); Assert.fail(); } catch (GradleException ex) { Assert.assertEquals(AppYamlDeployTargetResolver.VERSION_ERROR, ex.getMessage()); } } }
throw new GradleException("Provided schema file path doesn't exists: " + schemaFilePath + ". Please ensure a valid schema file exists"); if (schemaFile != null) { if (outputPackageName == null || outputPackageName.trim().isEmpty()) { throw new GradleException("Missing explicit outputPackageName option. Please ensure a valid package name is provided"); } else { targetPackageFolder = new File(outputFolder.getAbsolutePath()
private void verifyGradleVersion() { if (GradleVersion.current().compareTo(GradleVersion.version("4.4")) < 0) { throw new GradleException("SOFA Boot plugin requires Gradle 4.4 or later." + " The current version is " + GradleVersion.current()); } }
@SuppressWarnings("unchecked") private <T extends FormatExtension> T maybeCreate(String name, Class<T> clazz) { FormatExtension existing = formats.get(name); if (existing != null) { if (!existing.getClass().equals(clazz)) { throw new GradleException("Tried to add format named '" + name + "'" + " of type " + clazz + " but one has already been created of type " + existing.getClass()); } else { return (T) existing; } } else { try { Constructor<T> constructor = clazz.getConstructor(SpotlessExtension.class); T newlyCreated = constructor.newInstance(this); formats.put(name, newlyCreated); return newlyCreated; } catch (NoSuchMethodException e) { throw new GradleException("Must have a constructor " + clazz.getSimpleName() + "(SpotlessExtension root)", e); } catch (Exception e) { throw Errors.asRuntime(e); } } } }
/** Replaces the given step. */ protected void replaceStep(FormatterStep replacementStep) { FormatterStep existing = getExistingStep(replacementStep.getName()); if (existing == null) { throw new GradleException("Cannot replace step '" + replacementStep.getName() + "' for spotless format '" + formatName() + "' because it hasn't been added yet."); } int index = steps.indexOf(existing); steps.set(index, replacementStep); }
/** Adds a new step. */ public void addStep(FormatterStep newStep) { Objects.requireNonNull(newStep); FormatterStep existing = getExistingStep(newStep.getName()); if (existing != null) { throw new GradleException("Multiple steps with name '" + newStep.getName() + "' for spotless format '" + formatName() + "'"); } steps.add(newStep); }
static GradleException youShouldTurnOnPaddedCell(SpotlessTask task) { Path rootPath = task.getProject().getRootDir().toPath(); return new GradleException(StringPrinter.buildStringFromLines( "You have a misbehaving rule which can't make up its mind.", "This means that spotlessCheck will fail even after spotlessApply has run.", "", "This is a bug in a formatting rule, not Spotless itself, but Spotless can", "work around this bug and generate helpful bug reports for the broken rule", "if you add 'paddedCell()' to your build.gradle as such: ", "", " spotless {", " format 'someFormat', {", " ...", " paddedCell()", " }", " }", "", "The next time you run spotlessCheck, it will put helpful bug reports into", "'" + rootPath.relativize(diagnoseDir(task).toPath()) + "', and spotlessApply", "and spotlessCheck will be self-consistent from here on out.", "", "For details see " + URL)); }
@TaskAction public void performAction(IncrementalTaskInputs inputs) throws Exception { if (target == null) { throw new GradleException("You must specify 'Iterable<File> toFormat'"); throw new GradleException("Don't call " + getName() + " directly, call " + getName() + SpotlessPlugin.CHECK + " or " + getName() + SpotlessPlugin.APPLY);
/** Returns an exception which indicates problem files nicely. */ GradleException formatViolationsFor(Formatter formatter, List<File> problemFiles) { return new GradleException(DiffMessageFormatter.builder() .runToFix("Run 'gradlew spotlessApply' to fix these violations.") .isPaddedCell(paddedCell) .formatter(formatter) .problemFiles(problemFiles) .getMessage()); } }
/** If the user hasn't specified the files yet, we'll assume he/she means all of the kotlin files. */ @Override protected void setupTask(SpotlessTask task) { if (target == null) { JavaPluginConvention javaPlugin = getProject().getConvention().findPlugin(JavaPluginConvention.class); if (javaPlugin == null) { throw new GradleException("You must either specify 'target' manually or apply the 'scala' plugin."); } FileCollection union = getProject().files(); for (SourceSet sourceSet : javaPlugin.getSourceSets()) { union = union.plus(sourceSet.getAllSource().filter(file -> { String name = file.getName(); return name.endsWith(".scala") || name.endsWith(".sc"); })); } target = union; } super.setupTask(task); } }
/** If the user hasn't specified the files yet, we'll assume he/she means all of the kotlin files. */ @Override protected void setupTask(SpotlessTask task) { if (target == null) { JavaPluginConvention javaPlugin = getProject().getConvention().findPlugin(JavaPluginConvention.class); if (javaPlugin == null) { throw new GradleException("You must either specify 'target' manually or apply a kotlin plugin."); } FileCollection union = getProject().files(); for (SourceSet sourceSet : javaPlugin.getSourceSets()) { union = union.plus(sourceSet.getAllSource().filter(file -> { String name = file.getName(); return name.endsWith(".kt") || name.endsWith(".kts"); })); } target = union; } super.setupTask(task); } }
void setExitCode() { if (exitCode != 0) { throw new GradleException("Task failed"); } }