Skip to content

Commit abc6805

Browse files
committed
Refactor join point conversion to include JavaWeaver instance
- Updated FunctionalClassGenerator to accept JavaWeaver as a parameter for method generation. - Modified ActionUtils to pass JavaWeaver instance during code insertion and expression replacement. - Enhanced SelectUtils to utilize JavaWeaver in join point conversions for statements and expressions. - Refactored CtElement2JoinPoint and related converters to support JavaWeaver, ensuring all join point creations are consistent with the new structure. - Adjusted converters for executable, expression, statement, type, and XML node to include JavaWeaver, improving integration and functionality.
1 parent a29df71 commit abc6805

77 files changed

Lines changed: 726 additions & 1604 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

JavaWeaver/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ src-weaved/*
33
package.json
44

55
src/weaver/kadabra/abstracts/
6+
!src/weaver/kadabra/abstracts/AJavaWeaverJoinPoint.java
67
src/weaver/kadabra/exceptions/
78
src/weaver/kadabra/enums/
89
*.dotty

JavaWeaver/src/weaver/kadabra/JavaWeaver.java

Lines changed: 27 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@
4646
/**
4747
* LARA Weaving Engine that uses Spoon as the Java compiler/processor <br>
4848
* Weaver Implementation for JavaWeaver<br>
49-
* Since the generated abstract classes are always overwritten, their implementation should be done by extending those
50-
* abstract classes with user-defined classes.<br>
51-
* The abstract class {@link weaver.abstracts.AJoinPoint} can be used to add user-defined methods and fields which the
52-
* user intends to add for all join points and are not intended to be used in LARA aspects.
49+
* Since the generated abstract classes are always overwritten, their
50+
* implementation should be done by extending those abstract classes with
51+
* user-defined classes.<br>
52+
* The abstract class {@link weaver.abstracts.AJoinPoint} can be used to add
53+
* user-defined methods and fields which the user intends to add for all join
54+
* points and are not intended to be used in LARA aspects.
5355
*
5456
* @author Lara C.
5557
*/
@@ -58,7 +60,6 @@ public class JavaWeaver extends AJavaWeaver {
5860
KadabraLog.setDebug(true);
5961
}
6062

61-
// private static final Set<String> WEAVER_NAMES = SpecsCollections.asSet("kadabra");
6263
private static final Set<String> LANGUAGES = SpecsCollections.asSet("java");
6364

6465
// Fields
@@ -76,7 +77,7 @@ public class JavaWeaver extends AJavaWeaver {
7677
private boolean noClassPath = false; // Continues even if an error of missing lib occurs
7778
private OutputType outType = OutputType.COMPILATION_UNITS;
7879
private final Report reportGear;
79-
// private final JavaWeaverGear dependeciesGear = new JavaWeaverGear();
80+
8081
private File temp;
8182

8283
public JavaWeaver() {
@@ -86,7 +87,8 @@ public JavaWeaver() {
8687
}
8788

8889
/**
89-
* Set a file/folder in the weaver if it is valid file/folder type for the weaver.
90+
* Set a file/folder in the weaver if it is valid file/folder type for the
91+
* weaver.
9092
*
9193
* @param sources the file with the source code
9294
* @param outputDir output directory for the generated file(s)
@@ -102,34 +104,22 @@ protected boolean begin(List<File> sources, File outputDir, DataStore args) {
102104
reset();
103105
parseOptions(args);
104106

105-
// if (prettyPrint) {
106-
// tempOutFolder = new File(IoUtils.getCanonicalPath(outputDir) + "_temp");
107-
// outputDir = tempOutFolder;
108-
// }
109-
110107
if (prettyPrint) {
111108

112109
this.outputDir.mkdirs();
113-
temp = getTemporaryWeaverFolder();// new File("_jw_temp");
110+
temp = getTemporaryWeaverFolder();
114111
outputDir = temp;
115-
// this.setOutputProcessor(temp, spoon, spoon.getEnvironment());
116-
//
117-
// this.setInputSources(Arrays.asList(temp), spoon);
118112
}
119113

120114
// Pass only Java files to spoon
121115
// Method can do some processing, such as filtering duplicate classes
122116
var javaSources = getJavaSources(sources);
123117
spoon = newSpoon(javaSources, outputDir);
124118

125-
// spoon = newSpoon(sources, outputDir);
126119
this.currentOutputDir = outputDir;
127120
buildAndProcess();
128-
/* turning off path verifier as it is giving errors for new classes and code */
129-
// spoon.getEnvironment().setNoClasspath(true);
130-
// spoon.getEnvironment().setNoClasspath(false);
131-
jApp = JApp.newInstance(spoon, sources);
132-
// spoon.getEnvironment().setAutoImports(false);
121+
122+
jApp = JApp.newInstance(spoon, sources, this);
133123

134124
return true;
135125
}
@@ -205,7 +195,8 @@ private Optional<String> getPackage(File javaFile) {
205195
}
206196

207197
/**
208-
* Closes the weaver to the specified output directory location, if the weaver generates new file(s)
198+
* Closes the weaver to the specified output directory location, if the weaver
199+
* generates new file(s)
209200
*
210201
* @return if close was successful
211202
*/
@@ -229,12 +220,9 @@ protected boolean close() {
229220
if (prettyPrint) {
230221
spoon.prettyprint();
231222
spoon = newSpoon(Arrays.asList(temp), outputDir);
232-
// spoon.getEnvironment().setNoClasspath(true);
233-
// spoon.getEnvironment().setNoClasspath(false);
234223
buildAndProcess();
235224
spoon.prettyprint();
236225
} else {
237-
// System.out.println("PRESERVE? " + spoon.getEnvironment().isPreserveLineNumbers());
238226
spoon.prettyprint();
239227
}
240228
}
@@ -270,8 +258,6 @@ public void writeCode(File inputFolder, File outputFolder) {
270258
spoon.prettyprint();
271259

272260
var newSpoon = newSpoon(Arrays.asList(inputFolder), outputFolder);
273-
// newSpoon.getEnvironment().setNoClasspath(true);
274-
// newSpoon.getEnvironment().setNoClasspath(false);
275261
buildAndProcess(newSpoon);
276262
newSpoon.prettyprint();
277263

@@ -282,7 +268,8 @@ public void writeCode(File inputFolder, File outputFolder) {
282268
/**
283269
* Returns a list of Gears associated to this weaver engine
284270
*
285-
* @return a list of implementations of {@link AGear} or null if no gears are available
271+
* @return a list of implementations of {@link AGear} or null if no gears are
272+
* available
286273
*/
287274
@Override
288275
public List<AGear> getGears() {
@@ -294,48 +281,24 @@ public List<AGear> getGears() {
294281
private void reset() {
295282
SpoonUtils.resetCounter();
296283
table.reset();
297-
/*RESET GEARS*/
284+
/* RESET GEARS */
298285
reportGear.reset();
299286
}
300287

301288
/**
302-
* Instantiates a new {@link JWSpoonLauncher} based on the given input sources and the properties of this
289+
* Instantiates a new {@link JWSpoonLauncher} based on the given input sources
290+
* and the properties of this
303291
* {@link JavaWeaver} instance.
304292
*
305293
* @param sources the input sources to parse
306294
* @return
307295
*/
308296
private JWSpoonLauncher newSpoon(List<File> sources, File outputDir) {
309-
// Launcher.LOGGER.setLevel(Level.INFO);
310-
// Logger.getRootLogger().setLevel(Level.TRACE);
311-
312-
// try {
313-
// Launcher.LOGGER.addAppender(new FileAppender(new SimpleLayout(), "logg.txt"));
314-
// } catch (IOException e1) {
315-
// SpecsLogs.warn("Error message:\n", e1);
316-
// }
317-
318-
// try {
319-
// BasicConfigurator.configure(new FileAppender(new SimpleLayout(), "logg.txt"));
320-
// } catch (IOException e1) {
321-
// SpecsLogs.warn("Error message:\n", e1);
322-
// }
323-
324297
JWSpoonLauncher spoon = new JWSpoonLauncher(sources);
325298

326-
// String[] spoonArgs = { "--level", "ERROR" };
327-
// spoon.setArgs(spoonArgs);
328-
329299
Environment environment = spoon.getFactory().getEnvironment();
330-
// System.out.println("LEVE: " + environment.getLevel());
331-
// environment.setLevel("WARN");
332-
// System.out.println("NEW LEVE: " + environment.getLevel());
333-
// environment.setLevel("WARN");
334-
// System.out.println("LEVEL: " + Launcher.LOGGER.getLevel());
335-
// Launcher.LOGGER.setLevel(Level.TRACE);
336-
// System.out.println("LEVEL NEW: " + Launcher.LOGGER.getLevel());
337300

338-
spoon.setArgs(new String[]{"--output-type", outType.toString()}); // required to define the type of output...
301+
spoon.setArgs(new String[] { "--output-type", outType.toString() }); // required to define the type of output...
339302

340303
if (!classPath.isEmpty()) {
341304

@@ -346,7 +309,6 @@ private JWSpoonLauncher newSpoon(List<File> sources, File outputDir) {
346309
processedClasspath.addAll(classPath);
347310
var additionalJars = classPath.stream()
348311
.filter(classPath -> classPath.isDirectory())
349-
// .flatMap(folder -> SpecsIo.getFiles(folder, "jar").stream())
350312
.flatMap(folder -> SpecsIo.getFilesRecursive(folder, "jar").stream())
351313
.collect(Collectors.toList());
352314
SpecsLogs.debug("Adding JARs to classpath: " + additionalJars);
@@ -355,39 +317,25 @@ private JWSpoonLauncher newSpoon(List<File> sources, File outputDir) {
355317
List<String> filesStr = processedClasspath.stream().map(SpecsIo::getCanonicalPath)
356318
.collect(Collectors.toList());
357319
String[] classPathArray = filesStr.toArray(new String[0]);
358-
// KadabraLog.info(filesStr);
359320
environment.setSourceClasspath(classPathArray);
360-
// KadabraLog.info(Arrays.toString(environment.getSourceClasspath());
361321
} catch (Exception e) {
362322
throw new JavaWeaverException("setting the classpath", e);
363323
}
364324
}
365-
// environment.disableConsistencyChecks(); // Spoon 8
366-
// environment.setSelfChecks(false); // Spoon 6
367-
// environment.setSelfChecks(true);
368-
// environment.setAutoImports(true);
369-
environment.setCommentEnabled(true);
370-
// environment.setGenerateJavadoc(true);
371325

326+
environment.setCommentEnabled(true);
372327
setOutputProcessor(outputDir, spoon, environment);
373-
// the output type: compilation units)
374328
environment.setNoClasspath(noClassPath);
375-
// environment.setNoClasspath(false);
376329
setInputSources(sources, spoon);
377330
spoon.addProcessor(new IfProcessor());
378-
// spoon.addProcessor(new CommentProcessor());
379331

380332
// Set fully qualified names
381-
// environment.setAutoImports(true);
382-
// environment.setAutoImports(!args.get(JavaWeaverKeys.FULLY_QUALIFIED_NAMES));
383333
if (args.get(JavaWeaverKeys.FULLY_QUALIFIED_NAMES)) {
384334
environment.setAutoImports(false);
385335
} else {
386336
environment.setAutoImports(true);
387337
}
388338

389-
// environment.setPreserveLineNumbers(false);
390-
391339
environment.setCopyResources(args.get(JavaWeaverKeys.COPY_RESOURCES));
392340

393341
environment.setComplianceLevel(args.get(JavaWeaverKeys.JAVA_COMPLIANCE_LEVEL));
@@ -409,11 +357,11 @@ private static void setInputSources(List<File> sources, Launcher spoon) {
409357
}
410358

411359
private static void setOutputProcessor(File outputDir, Launcher spoon, Environment environment) {
412-
// JavaOutputProcessor outProcessor = spoon.createOutputWriter(outputDir, environment); // Spoon 6
413-
JavaOutputProcessor outProcessor = spoon.createOutputWriter(); // Spoon 8
414-
environment.setDefaultFileGenerator(outProcessor); // Define output folder (needed for the output type: classes)
415360

416-
// spoon.setOutputDirectory(IoUtils.getCanonicalPath(outputDir)); // Define output folder AGAIN (needed for the
361+
JavaOutputProcessor outProcessor = spoon.createOutputWriter();
362+
// Define output folder (needed for the output type: classes)
363+
environment.setDefaultFileGenerator(outProcessor);
364+
417365
spoon.setSourceOutputDirectory(SpecsIo.getCanonicalPath(outputDir));// Define output folder AGAIN
418366
spoon.setBinaryOutputDirectory(SpecsIo.getCanonicalPath(outputDir)); // Define output folder AGAIN (needed for
419367
}
@@ -425,7 +373,6 @@ private static void setOutputProcessor(File outputDir, Launcher spoon, Environme
425373
*/
426374
private void parseOptions(DataStore args) {
427375

428-
// System.out.println("IN JavaWeaver.parseOptions\n" + args);
429376
if (args.hasValue(JavaWeaverKeys.CLEAR_OUTPUT_FOLDER)) {
430377
clearOutputFolder = args.get(JavaWeaverKeys.CLEAR_OUTPUT_FOLDER);
431378
}
@@ -515,15 +462,8 @@ public String getName() {
515462
return "KADABRA";
516463
}
517464

518-
/**
519-
* Get weaver with this type
520-
**/
521-
public static JavaWeaver getJavaWeaver() {
522-
return (JavaWeaver) getThreadLocalWeaver();
523-
}
524-
525-
public static SpoonFactory getFactory() {
526-
return new SpoonFactory(getJavaWeaver().spoon.getFactory());
465+
public SpoonFactory getFactory() {
466+
return new SpoonFactory(spoon.getFactory());
527467
}
528468

529469
@Override

0 commit comments

Comments
 (0)