11/*
2- * Copyright 2016-2021 DiffPlug
2+ * Copyright 2016-2023 DiffPlug
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1515 */
1616package com .diffplug .spotless .extra .cpp ;
1717
18- import java .lang .reflect .Method ;
18+ import java .lang .reflect .InvocationTargetException ;
1919import java .util .Properties ;
2020
2121import com .diffplug .spotless .FormatterFunc ;
2222import com .diffplug .spotless .Jvm ;
2323import com .diffplug .spotless .Provisioner ;
24- import com .diffplug .spotless .extra .EclipseBasedStepBuilder ;
25- import com .diffplug .spotless .extra .EclipseBasedStepBuilder .State ;
24+ import com .diffplug .spotless .extra .EquoBasedStepBuilder ;
25+
26+ import dev .equo .solstice .p2 .P2Model ;
2627
2728/**
2829 * Formatter step which calls out to the Eclipse CDT formatter.
@@ -36,25 +37,40 @@ public final class EclipseCdtFormatterStep {
3637 private EclipseCdtFormatterStep () {}
3738
3839 private static final String NAME = "eclipse cdt formatter" ;
39- private static final String FORMATTER_CLASS = "com.diffplug.spotless.extra.eclipse.cdt.EclipseCdtFormatterStepImpl" ;
40- private static final String FORMATTER_METHOD = "format" ;
41- private static final Jvm .Support <String > JVM_SUPPORT = Jvm .<String > support (NAME ).add (8 , "4.16.0" ).add (11 , "4.21.0" );
40+ private static final Jvm .Support <String > JVM_SUPPORT = Jvm .<String > support (NAME ).add (11 , "11.0" );
4241
4342 public static String defaultVersion () {
4443 return JVM_SUPPORT .getRecommendedFormatterVersion ();
4544 }
4645
4746 /** Provides default configuration */
48- public static EclipseBasedStepBuilder createBuilder (Provisioner provisioner ) {
49- return new EclipseBasedStepBuilder (NAME , provisioner , EclipseCdtFormatterStep ::apply );
47+ public static EquoBasedStepBuilder createBuilder (Provisioner provisioner ) {
48+ return new EquoBasedStepBuilder (NAME , provisioner , EclipseCdtFormatterStep ::apply ) {
49+ @ Override
50+ protected P2Model model (String version ) {
51+ var model = new P2Model ();
52+ addPlatformRepo (model , "4.26" );
53+ model .addP2Repo ("https://download.eclipse.org/tools/cdt/releases/" + version + "/" );
54+ model .getInstall ().add ("org.eclipse.cdt.core" );
55+ return model ;
56+ }
57+ };
5058 }
5159
52- private static FormatterFunc apply (State state ) throws Exception {
60+ private static FormatterFunc apply (EquoBasedStepBuilder . State state ) throws Exception {
5361 JVM_SUPPORT .assertFormatterSupported (state .getSemanticVersion ());
54- Class <?> formatterClazz = state .loadClass (FORMATTER_CLASS );
55- Object formatter = formatterClazz .getConstructor (Properties .class ).newInstance (state .getPreferences ());
56- Method method = formatterClazz .getMethod (FORMATTER_METHOD , String .class );
57- return JVM_SUPPORT .suggestLaterVersionOnError (state .getSemanticVersion (), input -> (String ) method .invoke (formatter , input ));
62+ Class <?> formatterClazz = state .getJarState ().getClassLoader ().loadClass ("com.diffplug.spotless.extra.glue.cdt.EclipseCdtFormatterStepImpl" );
63+ var formatter = formatterClazz .getConstructor (Properties .class ).newInstance (state .getPreferences ());
64+ var method = formatterClazz .getMethod ("format" , String .class );
65+ return JVM_SUPPORT .suggestLaterVersionOnError (state .getSemanticVersion (),
66+ input -> {
67+ try {
68+ return (String ) method .invoke (formatter , input );
69+ } catch (InvocationTargetException exceptionWrapper ) {
70+ Throwable throwable = exceptionWrapper .getTargetException ();
71+ Exception exception = (throwable instanceof Exception ) ? (Exception ) throwable : null ;
72+ throw (null == exception ) ? exceptionWrapper : exception ;
73+ }
74+ });
5875 }
59-
6076}
0 commit comments