diff --git a/JavaWeaver/.gitignore b/JavaWeaver/.gitignore
index 0eea28e5..0c20678d 100644
--- a/JavaWeaver/.gitignore
+++ b/JavaWeaver/.gitignore
@@ -3,6 +3,7 @@ src-weaved/*
package.json
src/weaver/kadabra/abstracts/
+!src/weaver/kadabra/abstracts/AJavaWeaverJoinPoint.java
src/weaver/kadabra/exceptions/
src/weaver/kadabra/enums/
*.dotty
diff --git a/JavaWeaver/src/weaver/kadabra/JavaWeaver.java b/JavaWeaver/src/weaver/kadabra/JavaWeaver.java
index a68dc0d4..419eb572 100644
--- a/JavaWeaver/src/weaver/kadabra/JavaWeaver.java
+++ b/JavaWeaver/src/weaver/kadabra/JavaWeaver.java
@@ -46,10 +46,12 @@
/**
* LARA Weaving Engine that uses Spoon as the Java compiler/processor
* Weaver Implementation for JavaWeaver
- * Since the generated abstract classes are always overwritten, their implementation should be done by extending those
- * abstract classes with user-defined classes.
- * The abstract class {@link weaver.abstracts.AJoinPoint} can be used to add user-defined methods and fields which the
- * user intends to add for all join points and are not intended to be used in LARA aspects.
+ * Since the generated abstract classes are always overwritten, their
+ * implementation should be done by extending those abstract classes with
+ * user-defined classes.
+ * The abstract class {@link weaver.abstracts.AJoinPoint} can be used to add
+ * user-defined methods and fields which the user intends to add for all join
+ * points and are not intended to be used in LARA aspects.
*
* @author Lara C.
*/
@@ -58,7 +60,6 @@ public class JavaWeaver extends AJavaWeaver {
KadabraLog.setDebug(true);
}
- // private static final Set WEAVER_NAMES = SpecsCollections.asSet("kadabra");
private static final Set LANGUAGES = SpecsCollections.asSet("java");
// Fields
@@ -76,7 +77,7 @@ public class JavaWeaver extends AJavaWeaver {
private boolean noClassPath = false; // Continues even if an error of missing lib occurs
private OutputType outType = OutputType.COMPILATION_UNITS;
private final Report reportGear;
- // private final JavaWeaverGear dependeciesGear = new JavaWeaverGear();
+
private File temp;
public JavaWeaver() {
@@ -86,7 +87,8 @@ public JavaWeaver() {
}
/**
- * Set a file/folder in the weaver if it is valid file/folder type for the weaver.
+ * Set a file/folder in the weaver if it is valid file/folder type for the
+ * weaver.
*
* @param sources the file with the source code
* @param outputDir output directory for the generated file(s)
@@ -107,19 +109,11 @@ protected boolean begin(List sources, File outputDir, DataStore args) {
reset();
parseOptions(args);
- // if (prettyPrint) {
- // tempOutFolder = new File(IoUtils.getCanonicalPath(outputDir) + "_temp");
- // outputDir = tempOutFolder;
- // }
-
if (prettyPrint) {
this.outputDir.mkdirs();
- temp = getTemporaryWeaverFolder();// new File("_jw_temp");
+ temp = getTemporaryWeaverFolder();
outputDir = temp;
- // this.setOutputProcessor(temp, spoon, spoon.getEnvironment());
- //
- // this.setInputSources(Arrays.asList(temp), spoon);
}
// Pass only Java files to spoon
@@ -127,14 +121,10 @@ protected boolean begin(List sources, File outputDir, DataStore args) {
var javaSources = getJavaSources(sources);
spoon = newSpoon(javaSources, outputDir);
- // spoon = newSpoon(sources, outputDir);
this.currentOutputDir = outputDir;
buildAndProcess();
- /* turning off path verifier as it is giving errors for new classes and code */
- // spoon.getEnvironment().setNoClasspath(true);
- // spoon.getEnvironment().setNoClasspath(false);
- jApp = JApp.newInstance(spoon, sources);
- // spoon.getEnvironment().setAutoImports(false);
+
+ jApp = JApp.newInstance(spoon, sources, this);
return true;
}
@@ -210,7 +200,8 @@ private Optional getPackage(File javaFile) {
}
/**
- * Closes the weaver to the specified output directory location, if the weaver generates new file(s)
+ * Closes the weaver to the specified output directory location, if the weaver
+ * generates new file(s)
*
* @return if close was successful
*/
@@ -234,12 +225,9 @@ protected boolean close() {
if (prettyPrint) {
spoon.prettyprint();
spoon = newSpoon(Arrays.asList(temp), outputDir);
- // spoon.getEnvironment().setNoClasspath(true);
- // spoon.getEnvironment().setNoClasspath(false);
buildAndProcess();
spoon.prettyprint();
} else {
- // System.out.println("PRESERVE? " + spoon.getEnvironment().isPreserveLineNumbers());
spoon.prettyprint();
}
}
@@ -281,8 +269,6 @@ public void rebuild(File inputFolder, File outputFolder) {
spoon.prettyprint();
var newSpoon = newSpoon(Arrays.asList(inputFolder), outputFolder);
- // newSpoon.getEnvironment().setNoClasspath(true);
- // newSpoon.getEnvironment().setNoClasspath(false);
buildAndProcess(newSpoon);
newSpoon.prettyprint();
@@ -293,7 +279,8 @@ public void rebuild(File inputFolder, File outputFolder) {
/**
* Returns a list of Gears associated to this weaver engine
*
- * @return a list of implementations of {@link AGear} or null if no gears are available
+ * @return a list of implementations of {@link AGear} or null if no gears are
+ * available
*/
@Override
public List getGears() {
@@ -305,48 +292,23 @@ public List getGears() {
private void reset() {
SpoonUtils.resetCounter();
table.reset();
- /*RESET GEARS*/
+ /* RESET GEARS */
reportGear.reset();
}
/**
- * Instantiates a new {@link JWSpoonLauncher} based on the given input sources and the properties of this
- * {@link JavaWeaver} instance.
+ * Instantiates a new {@link JWSpoonLauncher} based on the given input sources
+ * and the properties of this {@link JavaWeaver} instance.
*
* @param sources the input sources to parse
* @return
*/
private JWSpoonLauncher newSpoon(List sources, File outputDir) {
- // Launcher.LOGGER.setLevel(Level.INFO);
- // Logger.getRootLogger().setLevel(Level.TRACE);
-
- // try {
- // Launcher.LOGGER.addAppender(new FileAppender(new SimpleLayout(), "logg.txt"));
- // } catch (IOException e1) {
- // SpecsLogs.warn("Error message:\n", e1);
- // }
-
- // try {
- // BasicConfigurator.configure(new FileAppender(new SimpleLayout(), "logg.txt"));
- // } catch (IOException e1) {
- // SpecsLogs.warn("Error message:\n", e1);
- // }
-
JWSpoonLauncher spoon = new JWSpoonLauncher(sources);
- // String[] spoonArgs = { "--level", "ERROR" };
- // spoon.setArgs(spoonArgs);
-
Environment environment = spoon.getFactory().getEnvironment();
- // System.out.println("LEVE: " + environment.getLevel());
- // environment.setLevel("WARN");
- // System.out.println("NEW LEVE: " + environment.getLevel());
- // environment.setLevel("WARN");
- // System.out.println("LEVEL: " + Launcher.LOGGER.getLevel());
- // Launcher.LOGGER.setLevel(Level.TRACE);
- // System.out.println("LEVEL NEW: " + Launcher.LOGGER.getLevel());
- spoon.setArgs(new String[]{"--output-type", outType.toString()}); // required to define the type of output...
+ spoon.setArgs(new String[] { "--output-type", outType.toString() }); // required to define the type of output...
if (!classPath.isEmpty()) {
@@ -357,7 +319,6 @@ private JWSpoonLauncher newSpoon(List sources, File outputDir) {
processedClasspath.addAll(classPath);
var additionalJars = classPath.stream()
.filter(classPath -> classPath.isDirectory())
- // .flatMap(folder -> SpecsIo.getFiles(folder, "jar").stream())
.flatMap(folder -> SpecsIo.getFilesRecursive(folder, "jar").stream())
.collect(Collectors.toList());
SpecsLogs.debug("Adding JARs to classpath: " + additionalJars);
@@ -366,39 +327,25 @@ private JWSpoonLauncher newSpoon(List sources, File outputDir) {
List filesStr = processedClasspath.stream().map(SpecsIo::getCanonicalPath)
.collect(Collectors.toList());
String[] classPathArray = filesStr.toArray(new String[0]);
- // KadabraLog.info(filesStr);
environment.setSourceClasspath(classPathArray);
- // KadabraLog.info(Arrays.toString(environment.getSourceClasspath());
} catch (Exception e) {
throw new JavaWeaverException("setting the classpath", e);
}
}
- // environment.disableConsistencyChecks(); // Spoon 8
- // environment.setSelfChecks(false); // Spoon 6
- // environment.setSelfChecks(true);
- // environment.setAutoImports(true);
- environment.setCommentEnabled(true);
- // environment.setGenerateJavadoc(true);
+ environment.setCommentEnabled(true);
setOutputProcessor(outputDir, spoon, environment);
- // the output type: compilation units)
environment.setNoClasspath(noClassPath);
- // environment.setNoClasspath(false);
setInputSources(sources, spoon);
spoon.addProcessor(new IfProcessor());
- // spoon.addProcessor(new CommentProcessor());
// Set fully qualified names
- // environment.setAutoImports(true);
- // environment.setAutoImports(!args.get(JavaWeaverKeys.FULLY_QUALIFIED_NAMES));
if (args.get(JavaWeaverKeys.FULLY_QUALIFIED_NAMES)) {
environment.setAutoImports(false);
} else {
environment.setAutoImports(true);
}
- // environment.setPreserveLineNumbers(false);
-
environment.setCopyResources(args.get(JavaWeaverKeys.COPY_RESOURCES));
environment.setComplianceLevel(args.get(JavaWeaverKeys.JAVA_COMPLIANCE_LEVEL));
@@ -420,11 +367,11 @@ private static void setInputSources(List sources, Launcher spoon) {
}
private static void setOutputProcessor(File outputDir, Launcher spoon, Environment environment) {
- // JavaOutputProcessor outProcessor = spoon.createOutputWriter(outputDir, environment); // Spoon 6
- JavaOutputProcessor outProcessor = spoon.createOutputWriter(); // Spoon 8
- environment.setDefaultFileGenerator(outProcessor); // Define output folder (needed for the output type: classes)
- // spoon.setOutputDirectory(IoUtils.getCanonicalPath(outputDir)); // Define output folder AGAIN (needed for the
+ JavaOutputProcessor outProcessor = spoon.createOutputWriter();
+ // Define output folder (needed for the output type: classes)
+ environment.setDefaultFileGenerator(outProcessor);
+
spoon.setSourceOutputDirectory(SpecsIo.getCanonicalPath(outputDir));// Define output folder AGAIN
spoon.setBinaryOutputDirectory(SpecsIo.getCanonicalPath(outputDir)); // Define output folder AGAIN (needed for
}
@@ -436,7 +383,6 @@ private static void setOutputProcessor(File outputDir, Launcher spoon, Environme
*/
private void parseOptions(DataStore args) {
- // System.out.println("IN JavaWeaver.parseOptions\n" + args);
if (args.hasValue(JavaWeaverKeys.CLEAR_OUTPUT_FOLDER)) {
clearOutputFolder = args.get(JavaWeaverKeys.CLEAR_OUTPUT_FOLDER);
}
@@ -526,15 +472,8 @@ public String getName() {
return "KADABRA";
}
- /**
- * Get weaver with this type
- **/
- public static JavaWeaver getJavaWeaver() {
- return (JavaWeaver) getThreadLocalWeaver();
- }
-
- public static SpoonFactory getFactory() {
- return new SpoonFactory(getJavaWeaver().spoon.getFactory());
+ public SpoonFactory getFactory() {
+ return new SpoonFactory(spoon.getFactory());
}
@Override
diff --git a/JavaWeaver/src/weaver/kadabra/abstracts/AJavaWeaverJoinPoint.java b/JavaWeaver/src/weaver/kadabra/abstracts/AJavaWeaverJoinPoint.java
index 3bf892b9..42075cc9 100644
--- a/JavaWeaver/src/weaver/kadabra/abstracts/AJavaWeaverJoinPoint.java
+++ b/JavaWeaver/src/weaver/kadabra/abstracts/AJavaWeaverJoinPoint.java
@@ -26,14 +26,21 @@
import java.util.stream.Stream;
/**
- * Abstract class which can be edited by the developer. This class will not be overwritten.
+ * Abstract class which can be edited by the developer. This class will not be
+ * overwritten.
*
* @author Lara Weaver Generator
*/
public abstract class AJavaWeaverJoinPoint extends AJoinPoint {
- // public static final Map CLONED_NODES = new HashMap<>();
+ public AJavaWeaverJoinPoint(JavaWeaver weaver) {
+ super(weaver);
+ }
+ @Override
+ public JavaWeaver getWeaverEngine() {
+ return (JavaWeaver) super.getWeaverEngine();
+ }
@Override
public boolean equals(Object obj) {
@@ -44,12 +51,13 @@ public boolean equals(Object obj) {
return false;
}
-
/**
- * Compares the two join points based on their node reference of the used compiler/parsing tool.
+ * Compares the two join points based on their node reference of the used
+ * compiler/parsing tool.
* This is the default implementation for comparing two join points.
- * Note for developers: A weaver may override this implementation in the editable abstract join point, so the
- * changes are made for all join points, or override this method in specific join points.
+ * Note for developers: A weaver may override this implementation in the
+ * editable abstract join point, so the changes are made for all join points, or
+ * override this method in specific join points.
*/
@Override
public boolean compareNodes(AJoinPoint aJoinPoint) {
@@ -63,45 +71,12 @@ public String getSrcCodeImpl() {
@Override
public String getCodeImpl() {
- // return getNode().getFactory().getEnvironment().createPrettyPrinter().printElement(getNode());
-
- return JavaWeaver.getJavaWeaver().getSourceCodePrinter().getSourceCode(getNode());
-
- // return new DefaultJavaPrettyPrinter(getNode().getFactory().getEnvironment()).printElement(getNode());
-
- // return new SourceCodePrinter(getNode().getFactory().getEnvironment()).printElement(getNode());
-
- // DefaultJavaPrettyPrinter printer = JavaWeaver.getJavaWeaver().getPrinter();
- // var node = getNode();
- // String errorMessage = "";
- // try {
- // // we do not want to compute imports of for CtImport and CtReference
- // // as it may change the print of a reference
- // if (!(node instanceof CtImport) && !(node instanceof CtReference)) {
- // printer.getImportsContext().computeImports(this);
- // }
- // printer.scan(node);
- // } catch (ParentNotInitializedException ignore) {
- // SpecsLogs.debug("Could not get code for " + this + ": " + ignore);
- // // throw new RuntimeException("Could not get code for node " + this, ignore);
- // // LOGGER.error(ERROR_MESSAGE_TO_STRING, ignore);
- // // errorMessage = ERROR_MESSAGE_TO_STRING;
- // }
- // // in line-preservation mode, newlines are added at the beginning to matches the lines
- // // removing them from the toString() representation
- // return printer.toString().replaceFirst("^\\s+", "") + errorMessage;
-
- // return getNode().toString(); // temporary
- // return getNode().toString() + "\n"; // temporary
+ return getWeaverEngine().getSourceCodePrinter().getSourceCode(getNode());
}
@Override
public abstract CtElement getNode();
- // public CtElement getParent() {
- // return getNode().getParent();
- // }
-
@Override
public void setLineImpl(int value) {
CtElement node = getNode();
@@ -137,12 +112,8 @@ public AJoinPoint getParentImpl() {
return null;
}
- // System.out.println("JP: " + getClass());
- // System.out.println("NODE: " + getNode().getClass());
- // System.out.println("PARENT J: " + parent.getClass());
+ var parentJp = CtElement2JoinPoint.convert(parent, getWeaverEngine());
- var parentJp = CtElement2JoinPoint.convert(parent);
- // System.out.println("PARENT JP: " + parentJp.getClass());
// Special case: if parent is a CallStatement, return the Call instead
if (parentJp instanceof ACallStatement) {
return ((ACallStatement) parentJp).getCallImpl();
@@ -172,54 +143,31 @@ public Optional getAncestorTry(Class ancestorType) {
public AJoinPoint getAncestorImpl(String type) {
Preconditions.checkNotNull(type, "Missing type of ancestor in attribute 'ancestor'");
- // System.out.println("Getting ancestor '" + type + "' of " + getJoinPointType());
-
AJoinPoint currentNode = getParentImpl();
while (currentNode != null) {
- // System.out.println("Parent " + currentNode.getJoinPointType() + " is '" + type + "'?");
if (currentNode.instanceOf(type)) {
- // System.out.println("Yes!");
return currentNode;
}
- // System.out.println("No..");
currentNode = currentNode.getParentImpl();
}
return currentNode;
-
- // CtElement currentNode = getNode().getParent();
- // while (currentNode != null) {
- // System.out.println("CURRENT NODE: " + currentNode.getClass());
- //
- // AJavaWeaverJoinPoint parentJp = CtElement2JoinPoint.convert(currentNode);
- //
- // if (parentJp.instanceOf(type)) {
- // return parentJp;
- // }
- // // String joinPointType = parentJp.getJoinPointType();
- // // if (joinPointType.equals(type)) {
- // // return parentJp;
- // // }
- // currentNode = currentNode.getParent();
- // }
- //
- // return null;
}
protected AExpression toAExpression(CtExpression> expression) {
- return CtExpression2AExpression.convertToExpression(expression);
+ return CtExpression2AExpression.convertToExpression(expression, getWeaverEngine());
}
protected AJavaWeaverJoinPoint toAStatement(CtStatement statement) {
- return CtStatement2AStatement.convert(statement);
+ return CtStatement2AStatement.convert(statement, getWeaverEngine());
}
protected AType toAType(CtType> type) {
- return CtType2AType.convert(type);
+ return CtType2AType.convert(type, getWeaverEngine());
}
protected AExecutable toAExecutable(CtExecutable> exec) {
- return CtExecutable2AExecutable.convert(exec);
+ return CtExecutable2AExecutable.convert(exec, getWeaverEngine());
}
@Override
@@ -233,25 +181,11 @@ public Integer getLineImpl() {
}
return position.getLine();
- // // Check if this joinpoint is a statement
- // if (this instanceof AStatement) {
- // return ((AStatement) this).getLineImpl();
- // }
- //
- // // Try to obtain a statement
- // AJavaWeaverJoinPoint stmtAncestor = ancestorImpl("statement");
- // if (stmtAncestor instanceof AStatement) {
- // return ((AStatement) stmtAncestor).getLineImpl();
- // }
- //
- // SpecsLogs.msgInfo("attribute 'line' not implemented for join point " + getJoinPointType()
- // + " that is not inside a statement");
- //
- // return -1;
}
/**
- * Required, because original insertImpl returns JoinPoint, but abstract join points return AJoinPoint.
+ * Required, because original insertImpl returns JoinPoint, but abstract join
+ * points return AJoinPoint.
*/
@Override
public AJoinPoint[] insertImpl(String position, String code) {
@@ -280,20 +214,13 @@ public AJoinPoint insertAfterImpl(String code) {
return insertImpl("after", code)[0];
}
- // @Override
- // public AJoinPoint[] insertImpl(String position, String code) {
- // return insertImpl("after", code);
- // }
-
@Override
public AJoinPoint insertReplaceImpl(AJoinPoint jp) {
- // return insertImpl("replace", jp)[0];
return replaceWithImpl(jp);
}
@Override
public AJoinPoint insertReplaceImpl(String code) {
- // return insertImpl("replace", code)[0];
return replaceWithImpl(code);
}
@@ -307,11 +234,6 @@ public AJoinPoint replaceWithImpl(String code) {
return insertImpl("replace", code)[0];
}
- // @Override
- // public AJoinPoint[] insertImpl(String position, T JoinPoint) {
- // throw new NotImplementedException(this);
- // }
-
@Override
public AJoinPoint[] insertImpl(String position, JoinPoint JoinPoint) {
throw new NotImplementedException(this);
@@ -319,48 +241,7 @@ public AJoinPoint[] insertImpl(String position, JoinPoint JoinPoint) {
@Override
public AJoinPoint[] getDescendantsArrayImpl() {
-
return getJpDescendantsStream().toArray(size -> new AJoinPoint[size]);
- /*
- if (this instanceof JApp) {
- JApp app = (JApp) this;
-
- List descendants = new ArrayList<>();
- for (AFile file : app.selectFile()) {
- descendants.add(file);
- descendants.addAll(Arrays.asList(file.getDescendantsArrayImpl()));
- }
- // for (ALibClass libClass : app.selectLibClass()) {
- // descendants.addAll(Arrays.asList(libClass.getDescendantsArrayImpl()));
- // }
-
- return descendants.toArray(new AJoinPoint[0]);
- }
-
- if (this instanceof JFile) {
- JFile jfile = (JFile) this;
-
- List descendants = new ArrayList<>();
- for (var file : jfile.getNode().getCu().getDeclaredTypes()) {
- AJavaWeaverJoinPoint type = CtElement2JoinPoint.convertTry(file).orElse(null);
- if (type == null) {
- continue;
- }
-
- descendants.add(type);
- descendants.addAll(Arrays.asList(type.getDescendantsArrayImpl()));
- }
-
- return descendants.toArray(new AJoinPoint[0]);
- }
-
- return getNode().getElements(element -> true)
- .stream()
- .map(CtElement2JoinPoint::convertTry)
- .filter(Optional::isPresent)
- .map(Optional::get)
- .toArray(size -> new AJoinPoint[size]);
- */
}
@Override
@@ -374,10 +255,6 @@ public Boolean getIsBlockImpl() {
return getNode() instanceof CtBlock;
}
- // @Override
- // public Boolean getIsBlockStatementImpl() {
- // return SpoonUtils.isStatementInBlock(getNode());
- // }
@Override
public AJoinPoint copyImpl() {
// Clone the node
@@ -385,10 +262,7 @@ public AJoinPoint copyImpl() {
// Set the parent
copy.setParent(getNode().getParent());
- // CLONED_NODES.put(copy, getNode());
-
- return CtElement2JoinPoint.convert(copy);
- // throw new RuntimeException(".copy not implemented yet for join point " + getJoinPointType());
+ return CtElement2JoinPoint.convert(copy, getWeaverEngine());
}
private List extends CtElement> getChildrenNodes() {
@@ -397,8 +271,6 @@ private List extends CtElement> getChildrenNodes() {
@Override
public Stream getJpChildrenStream() {
- // return getChildrenNodes().stream()
- // .map(CtElement2JoinPoint::convert);
return Arrays.stream(getChildrenArrayImpl());
}
@@ -410,20 +282,18 @@ public JoinPoint getJpParent() {
@Override
public AJoinPoint[] getChildrenArrayImpl() {
return getChildrenNodes().stream()
- .map(CtElement2JoinPoint::convert)
+ .map(node -> CtElement2JoinPoint.convert(node, getWeaverEngine()))
.toArray(size -> new AJoinPoint[size]);
}
@Override
public AJoinPoint childImpl(Integer index) {
return getChildrenArrayImpl()[index];
- // return CtElement2JoinPoint.convert(getChildrenNodes().get(index));
}
@Override
public Integer getNumChildrenImpl() {
return getChildrenArrayImpl().length;
- // return getChildrenNodes().size();
}
@Override
@@ -436,70 +306,27 @@ public String getAstImpl() {
}
// This method is more robust regarding the initial node
- return SpoonUtils.toAst(getNode(), "");
+ return SpoonUtils.toAst(getNode(), "", getWeaverEngine());
}
@Override
public String toString() {
- // return getJoinPointType();
return getNode().toString();
}
@Override
public void removeImpl() {
// Delete from annotations
- JWEnvironment env = ActionUtils.getKadabraEnvironment(JavaWeaver.getFactory().getSpoonFactory());
+ JWEnvironment env = ActionUtils.getKadabraEnvironment(getWeaverEngine().getFactory().getSpoonFactory());
// TODO: This remove can be optimized
- // System.out.println("NODE HAS PARENT? " + getNode().isParentInitialized());
- // System.out.println("NODE PARENT? " + getNode().getParent());
env.getTable().remove(getNode());
- // getNode().replace(Collections.emptyList()); // Spoon 6
- getNode().delete(); // Spoon 8
-
- }
-
- // /**
- // * If node implements CtModifiable, returns the modifiers. Otherwise, returns empty set.
- // *
- // * @return
- // */
- // // @SuppressWarnings("unchecked")
- // public Set getModifiersInternal() {
- // var node = getNode();
- //
- // if (node instanceof CtModifiable) {
- // return ((CtModifiable) node).getModifiers();
- // }
- //
- // return Collections.emptySet();
- // //
- // // // Choose best method
- // // Method invokingMethod = SpecsSystem.getMethod(getNode().getClass(), "getModifiers");
- // //
- // // // Could not find method, return empty set
- // // if (invokingMethod == null) {
- // // return Collections.emptySet();
- // //
- // // }
- // //
- // // // Invoke method
- // // try {
- // // return (Set) invokingMethod.invoke(getNode());
- // // } catch (Exception e) {
- // // throw new RuntimeException("Exception while calling getModifiers(): ", e);
- // // }
- // }
+ getNode().delete();
+ }
- /**
- *
- */
@Override
public String[] getModifiersArrayImpl() {
return modifiersToString(SpoonUtils.getModifiers(getNode()));
- // return JoinPoints.getModifiersInternal(this).stream()
- // .map(ModifierKind::name)
- // .toArray(length -> new String[length]);
}
public String[] modifiersToString(Collection modifiers) {
@@ -519,21 +346,10 @@ public Boolean hasModifierImpl(String modifier) {
}
return SpoonUtils.getModifiers(getNode()).contains(modifierKind);
- // var modifierLowerCase = modifier.toLowerCase();
- //
- // return Arrays.stream(getModifiersArrayImpl())
- // .map(String::toLowerCase)
- // .filter(currentModifier -> currentModifier.equals(modifierLowerCase))
- // .findFirst()
- // .isPresent();
}
@Override
public Boolean getIsFinalImpl() {
- // System.out.println("JP: " + getNode().getShortRepresentation() + ":" +
- // JoinPoints.getModifiersInternal(this));
- // System.out.println("JP: " + getClass() + ":" + JoinPoints.getModifiersInternal(this));
- // System.out.println("NODE: " + getNode().getClass() + ":" + JoinPoints.getModifiersInternal(this));
return SpoonUtils.getModifiers(getNode()).contains(ModifierKind.FINAL);
}
@@ -542,13 +358,11 @@ public Boolean getIsStaticImpl() {
return SpoonUtils.getModifiers(getNode()).contains(ModifierKind.STATIC);
}
- ;
-
@Override
public AAnnotation[] getAnnotationsArrayImpl() {
return getNode().getAnnotations().stream()
- .map(annotation -> (AAnnotation) SelectUtils.expression2JoinPoint(annotation))
+ .map(annotation -> (AAnnotation) SelectUtils.expression2JoinPoint(annotation, getWeaverEngine()))
.collect(Collectors.toList())
.toArray(size -> new AAnnotation[size]);
}
diff --git a/JavaWeaver/src/weaver/kadabra/importable/KadabraJoinPoints.java b/JavaWeaver/src/weaver/kadabra/importable/KadabraJoinPoints.java
index 69349e39..42299e79 100644
--- a/JavaWeaver/src/weaver/kadabra/importable/KadabraJoinPoints.java
+++ b/JavaWeaver/src/weaver/kadabra/importable/KadabraJoinPoints.java
@@ -39,12 +39,12 @@ public class KadabraJoinPoints {
* Creates a new comment join point.
*
* @param comment
- * the contents of the comment
+ * the contents of the comment
* @param type
- * the type of comment, according to CtComment.CommentType
+ * the type of comment, according to CtComment.CommentType
* @return
*/
- public static JComment comment(String comment, String type) {
+ public static JComment comment(JavaWeaver weaver, String comment, String type) {
// Convert the type
CommentType typeEnum = null;
@@ -55,20 +55,20 @@ public static JComment comment(String comment, String type) {
"Comment type not supported: '" + type + "'. Use one of " + Arrays.toString(CommentType.values()));
}
- return (JComment) CtElement2JoinPoint.convert(JavaWeaver.getFactory().comment(comment, typeEnum));
+ return CtElement2JoinPoint.convert(weaver.getFactory().comment(comment, typeEnum), weaver, JComment.class);
}
/**
* Creates a new expression join point that represents the given literal.
*
* @param type
- * the type of the literal
+ * the type of the literal
* @param literal
- * a string representing a Java literal. In the case it is signed, returns a unaryExpression instead of a
- * literal
+ * a string representing a Java literal. In the case it is
+ * signed, returns a unaryExpression instead of a literal
* @return
*/
- public static Object literal(String literal, String type) {
+ public static Object literal(JavaWeaver weaver, String literal, String type) {
boolean isNegative = false;
// Check if negative
@@ -78,62 +78,41 @@ public static Object literal(String literal, String type) {
}
// Check type of literal
-
var decodedValue = SpoonLiterals.decodeLiteralValue(type, literal);
- // If a number, check if it is a negative value
- // boolean isNegative = decodedValue instanceof Number ? ((Number) decodedValue).doubleValue() < 0 : false;
-
- CtExpression> expressionNode = JavaWeaver.getFactory().literal(decodedValue);
+ CtExpression> expressionNode = weaver.getFactory().literal(decodedValue);
if (isNegative) {
- expressionNode = JavaWeaver.getFactory().unaryOperator(UnaryOperatorKind.NEG, expressionNode);
+ expressionNode = weaver.getFactory().unaryOperator(UnaryOperatorKind.NEG, expressionNode);
}
- return CtElement2JoinPoint.convert(expressionNode);
+ return CtElement2JoinPoint.convert(expressionNode, weaver);
}
- public static Object nullLiteral(Object referenceJp) {
+ public static Object nullLiteral(JavaWeaver weaver, Object referenceJp) {
if (referenceJp != null) {
SpecsCheck.checkArgument(referenceJp instanceof JoinPoint,
() -> "Reference join point must be a join point, it is a "
+ referenceJp.getClass().getSimpleName());
}
- var factory = JavaWeaver.getFactory().getSpoonFactory();
+ var factory = weaver.getFactory().getSpoonFactory();
CtElement nullLiteral = factory.createLiteral(null);
- //
- // if (referenceJp != null) {
- // var node = ((AJavaWeaverJoinPoint) referenceJp).getNode();
- // var children = node.getDirectChildren();
- // if (!children.isEmpty()) {
- // var firstChild = children.get(0);
- //
- // if (firstChild instanceof CtTypeReference) {
- // var copy = (CtTypeReference) firstChild.clone();
- //
- // var castNode = factory.Code().createTypeCast(copy, nullLiteral);
- // }
- // // System.out.println("FIRST CHILD: " + firstChild);
- // // System.out.println("FIRST CHILD CLASS: " + firstChild.getClass());
- // }
- // }
-
- return CtElement2JoinPoint.convert(nullLiteral);
+ return CtElement2JoinPoint.convert(nullLiteral, weaver);
}
/**
* Creates a new unary operator for the given operation and expression.
*
* @param operator
- * the operator of the unary expression
+ * the operator of the unary expression
* @param operand
- * an expression join point
+ * an expression join point
* @return
*/
- public static Object unaryOperator(String operator, Object operand) {
+ public static Object unaryOperator(JavaWeaver weaver, String operator, Object operand) {
SpecsCheck.checkArgument(operand instanceof JoinPoint,
() -> "Operand must be a join point, it " + operator.getClass().getSimpleName());
@@ -148,21 +127,23 @@ public static Object unaryOperator(String operator, Object operand) {
// Convert string to kind
UnaryOperatorKind opKind = OperatorUtils.parseUnary(operator);
- return CtElement2JoinPoint.convert(JavaWeaver.getFactory().unaryOperator(opKind, nodeExpr));
+ return CtElement2JoinPoint.convert(weaver.getFactory().unaryOperator(opKind, nodeExpr), weaver);
}
/**
* Creates a new unary operator for the given operation and expression.
*
* @param operator
- * the operator of the binary expression
+ * the operator of the binary expression
* @param lhs
- * a join point representing the left hand of the binary expression
+ * a join point representing the left hand of the binary
+ * expression
* @param rhs
- * a join point representing the right hand of the binary expression
+ * a join point representing the right hand of the binary
+ * expression
* @return
*/
- public static Object binaryOperator(String operator, Object lhs, Object rhs) {
+ public static Object binaryOperator(JavaWeaver weaver, String operator, Object lhs, Object rhs) {
SpecsCheck.checkArgument(lhs instanceof JoinPoint,
() -> "Lhs must be a join point, it " + operator.getClass().getSimpleName());
@@ -183,10 +164,10 @@ public static Object binaryOperator(String operator, Object lhs, Object rhs) {
// Convert string to kind
BinaryOperatorKind opKind = OperatorUtils.parseBinary(operator);
- return CtElement2JoinPoint.convert(JavaWeaver.getFactory().binaryOperator(opKind, nodeLhs, nodeRhs));
+ return CtElement2JoinPoint.convert(weaver.getFactory().binaryOperator(opKind, nodeLhs, nodeRhs), weaver);
}
- public static Object assignment(Object lhs, Object rhs) {
+ public static Object assignment(JavaWeaver weaver, Object lhs, Object rhs) {
Objects.requireNonNull(lhs, () -> "lhs cannot be null");
Objects.requireNonNull(rhs, () -> "rhs cannot be null");
SpecsCheck.checkArgument(lhs instanceof JoinPoint,
@@ -205,13 +186,13 @@ public static Object assignment(Object lhs, Object rhs) {
CtExpression> nodeLhs = (CtExpression>) jpLhs.getNode();
CtExpression> nodeRhs = (CtExpression>) jpRhs.getNode();
- return CtElement2JoinPoint.convert(JavaWeaver.getFactory().assignment(nodeLhs, nodeRhs));
+ return CtElement2JoinPoint.convert(weaver.getFactory().assignment(nodeLhs, nodeRhs), weaver);
}
- public static Object var(JLocalVariable localVariable, boolean isWrite) {
+ public static Object var(JavaWeaver weaver, JLocalVariable localVariable, boolean isWrite) {
var localVarSpoon = localVariable.getNode();
- return CtElement2JoinPoint.convert(JavaWeaver.getFactory().var(localVarSpoon, isWrite));
+ return CtElement2JoinPoint.convert(weaver.getFactory().var(localVarSpoon, isWrite), weaver);
}
/**
@@ -220,13 +201,8 @@ public static Object var(JLocalVariable localVariable, boolean isWrite) {
* @param code
* @return
*/
- public static Object snippetExpression(String code) {
+ public static Object snippetExpression(JavaWeaver weaver, String code) {
return CtElement2JoinPoint
- .convert(SnippetFactory.createSnippetExpression(JavaWeaver.getFactory().getSpoonFactory(), code));
+ .convert(SnippetFactory.createSnippetExpression(weaver.getFactory().getSpoonFactory(), code), weaver);
}
-
- // public static Object literal2(String literal, String type) {
- // var expressionNode = JavaWeaver.getFactory().literal(Integer.valueOf(1));
- // return CtElement2JoinPoint.convert(expressionNode);
- // }
}
diff --git a/JavaWeaver/src/weaver/kadabra/joinpoints/JAndroidManifest.java b/JavaWeaver/src/weaver/kadabra/joinpoints/JAndroidManifest.java
index 5e1df273..9dc72821 100644
--- a/JavaWeaver/src/weaver/kadabra/joinpoints/JAndroidManifest.java
+++ b/JavaWeaver/src/weaver/kadabra/joinpoints/JAndroidManifest.java
@@ -16,6 +16,7 @@
import org.json.XML;
import pt.up.fe.specs.util.xml.XmlDocument;
import spoon.reflect.declaration.CtElement;
+import weaver.kadabra.JavaWeaver;
import weaver.kadabra.abstracts.joinpoints.AAndroidManifest;
import weaver.kadabra.abstracts.joinpoints.AJoinPoint;
@@ -23,8 +24,8 @@ public class JAndroidManifest extends AAndroidManifest {
private final XmlDocument manifest;
- public JAndroidManifest(XmlDocument manifest) {
- super(new JXmlNode(manifest));
+ public JAndroidManifest(XmlDocument manifest, JavaWeaver weaver) {
+ super(new JXmlNode(manifest, weaver), weaver);
this.manifest = manifest;
}
diff --git a/JavaWeaver/src/weaver/kadabra/joinpoints/JAnnotation.java b/JavaWeaver/src/weaver/kadabra/joinpoints/JAnnotation.java
index 3aa806f6..4a9e70a9 100644
--- a/JavaWeaver/src/weaver/kadabra/joinpoints/JAnnotation.java
+++ b/JavaWeaver/src/weaver/kadabra/joinpoints/JAnnotation.java
@@ -15,14 +15,15 @@
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtElement;
+import weaver.kadabra.JavaWeaver;
import weaver.kadabra.abstracts.joinpoints.AAnnotation;
public class JAnnotation extends AAnnotation {
private final CtAnnotation> annotation;
- public JAnnotation(CtAnnotation> annotation) {
- super(new JExpression<>(annotation));
+ public JAnnotation(CtAnnotation> annotation, JavaWeaver weaver) {
+ super(new JExpression<>(annotation, weaver), weaver);
this.annotation = annotation;
}
diff --git a/JavaWeaver/src/weaver/kadabra/joinpoints/JAnonymousExec.java b/JavaWeaver/src/weaver/kadabra/joinpoints/JAnonymousExec.java
index be610a36..24c8fc02 100644
--- a/JavaWeaver/src/weaver/kadabra/joinpoints/JAnonymousExec.java
+++ b/JavaWeaver/src/weaver/kadabra/joinpoints/JAnonymousExec.java
@@ -15,24 +15,24 @@
import spoon.reflect.declaration.CtAnonymousExecutable;
import spoon.reflect.declaration.CtElement;
+import weaver.kadabra.JavaWeaver;
import weaver.kadabra.abstracts.joinpoints.AAnonymousExec;
public class JAnonymousExec extends AAnonymousExec {
private CtAnonymousExecutable node;
- private JAnonymousExec(CtAnonymousExecutable executable) {
- super(JExecutable.newInstance(executable));
+ public JAnonymousExec(CtAnonymousExecutable executable, JavaWeaver weaver) {
+ super(JExecutable.newInstance(executable, weaver), weaver);
this.node = executable;
}
- public static JAnonymousExec newInstance(CtAnonymousExecutable executable) {
- return new JAnonymousExec(executable);
+ public static JAnonymousExec newInstance(CtAnonymousExecutable executable, JavaWeaver weaver) {
+ return new JAnonymousExec(executable, weaver);
}
@Override
public CtElement getNode() {
-
return node;
}
diff --git a/JavaWeaver/src/weaver/kadabra/joinpoints/JApp.java b/JavaWeaver/src/weaver/kadabra/joinpoints/JApp.java
index 48073bc2..6d8158ec 100644
--- a/JavaWeaver/src/weaver/kadabra/joinpoints/JApp.java
+++ b/JavaWeaver/src/weaver/kadabra/joinpoints/JApp.java
@@ -20,6 +20,7 @@
import spoon.reflect.declaration.CtInterface;
import spoon.reflect.factory.Factory;
import spoon.support.gui.SpoonModelTree;
+import weaver.kadabra.JavaWeaver;
import weaver.kadabra.abstracts.joinpoints.*;
import weaver.kadabra.spoon.extensions.nodes.CtApp;
import weaver.utils.android.AndroidResources;
@@ -39,20 +40,21 @@ public class JApp extends AApp {
private final Set sources;
private final AndroidResources androidResources;
- private JApp(Launcher spoon, List sources) {
+ private JApp(Launcher spoon, List sources, JavaWeaver weaver) {
+ super(weaver);
this.spoon = spoon;
this.sources = new HashSet<>(sources);
this.androidResources = AndroidResources.newInstance(sources);
}
- public static JApp newInstance(Launcher spoon, List sources) {
- var app = new JApp(spoon, sources);
+ public static JApp newInstance(Launcher spoon, List sources, JavaWeaver weaver) {
+ var app = new JApp(spoon, sources, weaver);
return app;
}
- public static JApp newInstance(CtApp app) {
- return new JApp(app.spoon, Collections.emptyList());
+ public static JApp newInstance(CtApp app, JavaWeaver weaver) {
+ return new JApp(app.spoon, Collections.emptyList(), weaver);
}
public AndroidResources getAndroidResources() {
@@ -62,7 +64,7 @@ public AndroidResources getAndroidResources() {
private List extends AFile> retrieveFiles() {
final List files = spoon.getFactory().CompilationUnit().getMap().values().stream()
- .map(JFile::new)
+ .map(cu -> new JFile(cu, getWeaverEngine()))
.collect(Collectors.toList());
return files;
@@ -83,14 +85,12 @@ public AClass newClassImpl(String name) {
@Override
public AClass newClassImpl(String name, String extend, String[] implement) {
if (name == null || name.isEmpty()) {
-
throw new NullPointerException("the name of the new class cannot be null or empty");
}
- // KadabraLog.debug("GENERATING NEW CLASS: " + name);
var cu = ActionUtils.compilationUnitWithClass(name, extend, implement,
spoon.getModelBuilder().getBinaryOutputDirectory(), spoon.getFactory());
CtClass> mainClass = (CtClass>) cu.getMainType();
- AClass newInstance = JClass.newInstance(mainClass, cu);
+ AClass newInstance = JClass.newInstance(mainClass, cu, getWeaverEngine());
return newInstance;
}
@@ -105,7 +105,7 @@ public AInterfaceType newInterfaceImpl(String name, String[] extend) {
final CtInterface