Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

review: feature: Implementation for Jigsaw modules(Java 9) with support for shadow modules #4751

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ void getSpoonProperties() {
new Pair<>("isImplicit(): boolean", "false"),
new Pair<>("getReferencedTypes(): Set<CtTypeReference<?>>", "[]"),
new Pair<>("getParent(): CtElement", "null"),
new Pair<>("getComments(): List<CtComment>", "[]")))
new Pair<>("getComments(): List<CtComment>", "[]"),
new Pair<>("getDeclaringModule(): CtModule", "null")))
);
}

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/spoon/ContractVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
*/
package spoon;


import spoon.reflect.CtModelImpl;
import spoon.reflect.code.CtArrayWrite;
import spoon.reflect.code.CtAssignment;
import spoon.reflect.code.CtExpression;
Expand Down Expand Up @@ -597,7 +595,7 @@ public void checkJavaIdentifiers() {
// checking method JavaIdentifiers.isLegalJavaPackageIdentifier
_rootPackage.getElements(new TypeFilter<>(CtPackage.class)).parallelStream().forEach(element -> {
// the default package is excluded (called "unnamed package")
if (element instanceof CtModelImpl.CtRootPackage) {
if (element.isUnnamedPackage()) {
return;
}

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/spoon/reflect/CtModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ public interface CtModel extends Serializable, CtQueryable {
*/
CtModule getUnnamedModule();

/**
* searches for a module
*/
CtModule getModule(String name);

/**
* adds a module to the model.
*/
<T extends CtModel> T addModule(CtModule module);

/**
* removes a module to the model.
*/
<T extends CtModel> T removeModule(CtModule module);

/**
* returns all modules of the model
*/
Expand Down
106 changes: 51 additions & 55 deletions src/main/java/spoon/reflect/CtModelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,37 @@
import spoon.processing.Processor;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtModule;
import spoon.reflect.declaration.CtNamedElement;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.factory.ModuleFactory;
import spoon.reflect.path.CtRole;
import spoon.reflect.visitor.Filter;
import spoon.reflect.visitor.chain.CtConsumableFunction;
import spoon.reflect.visitor.chain.CtFunction;
import spoon.reflect.visitor.chain.CtQuery;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.QueueProcessingManager;
import spoon.support.reflect.declaration.CtPackageImpl;
import spoon.support.reflect.declaration.CtModuleImpl;
import spoon.support.util.internal.ElementNameMap;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class CtModelImpl implements CtModel {

private static final long serialVersionUID = 1L;

private boolean buildModelFinished = false;
private final CtModule unnamedModule;
private final Modules modules;
private boolean buildModelFinished;

public CtModelImpl(Factory factory) {
this.unnamedModule = new CtModuleImpl.UnnamedModule(factory);
this.modules = new Modules();
addModule(unnamedModule);
}

@Override
public <R extends CtElement> CtQuery filterChildren(Filter<R> filter) {
Expand All @@ -49,76 +57,37 @@ public <I> CtQuery map(CtConsumableFunction<I> queryStep) {
return getUnnamedModule().getFactory().Query().createQuery(this.getAllModules().toArray()).map(queryStep);
}

public static class CtRootPackage extends CtPackageImpl {
{
this.setSimpleName(CtPackage.TOP_LEVEL_PACKAGE_NAME);
}

@Override
public <T extends CtNamedElement> T setSimpleName(String name) {
if (name == null) {
return (T) this;
}

if (name.equals(CtPackage.TOP_LEVEL_PACKAGE_NAME)) {
return super.setSimpleName(name);
}

return (T) this;
}

@Override
public String getQualifiedName() {
return "";
}

@Override
public String toString() {
return TOP_LEVEL_PACKAGE_NAME;
}
@Override
public Collection<CtType<?>> getAllTypes() {
return getAllPackages().stream().map(CtPackage::getTypes).flatMap(Collection::stream).collect(Collectors.toList());
}

private final CtModule unnamedModule;

public CtModelImpl(Factory f) {
this.unnamedModule = new ModuleFactory.CtUnnamedModule();
this.unnamedModule.setFactory(f);
this.unnamedModule.setRootPackage(new CtModelImpl.CtRootPackage());
getRootPackage().setFactory(f);
@Override
public Collection<CtPackage> getAllPackages() {
return Collections.unmodifiableCollection(getElements(new TypeFilter<>(CtPackage.class)));
}

@Override
public CtPackage getRootPackage() {
return getUnnamedModule().getRootPackage();
}


@Override
public Collection<CtType<?>> getAllTypes() {
final List<CtType<?>> result = new ArrayList<>();
getAllPackages().forEach(ctPackage -> {
result.addAll(ctPackage.getTypes());
});
return result;
}


@Override
public Collection<CtPackage> getAllPackages() {
return Collections.unmodifiableCollection(getElements(new TypeFilter<>(CtPackage.class)));
public CtModule getUnnamedModule() {
return this.unnamedModule;
}

@Override
public CtModule getUnnamedModule() {
return this.unnamedModule;
public CtModule getModule(String name) {
return modules.get(name);
}

@Override
public Collection<CtModule> getAllModules() {
return ((ModuleFactory.CtUnnamedModule) this.unnamedModule).getAllModules();
return Collections.unmodifiableCollection(modules.values());
}


@Override
public void processWith(Processor<?> processor) {
QueueProcessingManager processingManager = new QueueProcessingManager(getUnnamedModule().getFactory());
Expand All @@ -137,10 +106,37 @@ public boolean isBuildModelFinished() {
return this.buildModelFinished;
}

@Override
public <T extends CtModel> T addModule(CtModule module) {
modules.put(module.getSimpleName(), module);
return (T) this;
}

@Override
public <T extends CtModel> T removeModule(CtModule module) {
modules.remove(module.getSimpleName());
return (T) this;
}

@Override
public <T extends CtModel> T setBuildModelIsFinished(boolean buildModelFinished) {
this.buildModelFinished = buildModelFinished;
return (T) this;
}

public void updateModuleName(CtModule newModule, String oldName) {
modules.updateKey(oldName, newModule.getSimpleName());
}

private static class Modules extends ElementNameMap<CtModule> {
@Override
protected CtElement getOwner() {
return null;

Check notice

Code scanning

Return of 'null'

Return of 'null'
}

@Override
protected CtRole getRole() {
return CtRole.DECLARED_MODULE;
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/spoon/reflect/declaration/CtElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,12 @@ <E extends CtElement> List<E> getAnnotatedChildren(
*/
String toStringDebug();

/**
* Gets the declaring module.
*/
@DerivedProperty
CtModule getDeclaringModule();

/**
* @return the source code of this element with the pretty-printing rules of Spoon
* Warning: this is not side-effect free, this triggers some {@link spoon.reflect.visitor.ImportAnalyzer} which would change the model: add/remove imports, change the value `implicit` of some model elements, etc.
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/spoon/reflect/declaration/CtModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* grants access at compile time to types in only those packages which are explicitly exported,
* but grants access at run time to types in all its packages, as if all packages had been exported.
*/
public interface CtModule extends CtNamedElement {
public interface CtModule extends CtNamedElement, CtShadowable {

/**
* The name for the top level module.
Expand Down Expand Up @@ -173,4 +173,10 @@ public interface CtModule extends CtNamedElement {

@Override
CtModule clone();

@DerivedProperty
CtPackage getPackage(String qualifiedName);

@DerivedProperty
List<CtPackage> getAllPackages();
}
6 changes: 0 additions & 6 deletions src/main/java/spoon/reflect/declaration/CtPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ public interface CtPackage extends CtNamedElement, CtShadowable {
*/
String TOP_LEVEL_PACKAGE_NAME = "unnamed package";

/**
* Gets the declaring module.
*/
@DerivedProperty
CtModule getDeclaringModule();

/**
* Gets the declaring package of the current one. Returns null if the package is not yet in another one.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/spoon/reflect/factory/CoreFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ public interface CoreFactory {
*/
CtPackage createPackage();

/**
* Creates a package.
*/
CtPackage createPackage(CtModule parent);

/**
* Creates a package reference.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/factory/EnumFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public CtEnum<?> create(String qualifiedName) {
@SuppressWarnings("unchecked")
public CtEnum<?> get(String qualifiedName) {
try {
return (CtEnum<?>) super.get(qualifiedName);
return (CtEnum) super.get(qualifiedName);
} catch (Exception e) {
return null;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/spoon/reflect/factory/Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,11 @@ public interface Factory {
*/
CtPackage createPackage();

/**
* @see CoreFactory#createPackage(CtModule)
*/
CtPackage createPackage(CtModule parent);

/**
* @see CoreFactory#createTypeParameter()
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/spoon/reflect/factory/FactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,11 @@ public CtPackage createPackage() {
return Core().createPackage();
}

@Override
public CtPackage createPackage(CtModule parent) {
return Core().createPackage(parent);
}

@Override
public CtTypeParameter createTypeParameter() {
return Core().createTypeParameter();
Expand Down
Loading