Skip to content

Commit

Permalink
Showing 38 changed files with 1,046 additions and 456 deletions.
1 change: 1 addition & 0 deletions src/main/java/spoon/metamodel/Metamodel.java
Original file line number Diff line number Diff line change
@@ -138,6 +138,7 @@ public static Set<CtType<?>> getAllMetamodelInterfaces() {
result.add(factory.Type().get(spoon.reflect.declaration.CtAnonymousExecutable.class));
result.add(factory.Type().get(spoon.reflect.declaration.CtClass.class));
result.add(factory.Type().get(spoon.reflect.declaration.CtCodeSnippet.class));
result.add(factory.Type().get(spoon.reflect.declaration.CtCompilationUnit.class));
result.add(factory.Type().get(spoon.reflect.declaration.CtConstructor.class));
result.add(factory.Type().get(spoon.reflect.declaration.CtElement.class));
result.add(factory.Type().get(spoon.reflect.declaration.CtEnum.class));
Original file line number Diff line number Diff line change
@@ -73,6 +73,9 @@ public final class ModelElementContainerDefaultCapacities {
// > 1 very rarely
public static final int COMPILATION_UNIT_DECLARED_TYPES_CONTAINER_DEFAULT_CAPACITY = 1;

// TODO detect best default
public static final int COMPILATION_UNIT_IMPORTS_CONTAINER_DEFAULT_CAPACITY = 10;

// > 1 very rarely
public static final int ANONYMOUS_EXECUTABLES_CONTAINER_DEFAULT_CAPACITY = 1;

119 changes: 5 additions & 114 deletions src/main/java/spoon/reflect/cu/CompilationUnit.java
Original file line number Diff line number Diff line change
@@ -16,109 +16,14 @@
*/
package spoon.reflect.cu;

import spoon.processing.FactoryAccessor;
import spoon.reflect.declaration.CtImport;
import spoon.reflect.declaration.CtModule;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;
import spoon.support.Experimental;

import java.io.File;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import spoon.reflect.declaration.CtCompilationUnit;

/**
* Defines a compilation unit. In Java, a compilation unit can contain only one
* public type declaration and other secondary types declarations (not public).
*/
public interface CompilationUnit extends FactoryAccessor, SourcePositionHolder, Serializable {

enum UNIT_TYPE {
TYPE_DECLARATION,
PACKAGE_DECLARATION,
MODULE_DECLARATION,
UNKNOWN
}

/**
* Returns the declaration type of the compilation unit.
*/
UNIT_TYPE getUnitType();

/**
* Gets the file that corresponds to this compilation unit if any (contains
* the source code).
*/
File getFile();
public interface CompilationUnit extends CtCompilationUnit {

/**
* Sets the file that corresponds to this compilation unit.
*/
void setFile(File file);

/**
* @return array of offsets in the origin source file, where occurs line separator
*/
int[] getLineSeparatorPositions();

/**
* @param lineSeparatorPositions array of offsets in the origin source file, where occurs line separator
*/
void setLineSeparatorPositions(int[] lineSeparatorPositions);

/**
* Gets all binary (.class) files that corresponds to this compilation unit
* and have been created by calling
* {@link spoon.SpoonModelBuilder#compile(spoon.SpoonModelBuilder.InputType...)}.
*/
List<File> getBinaryFiles();

/**
* Gets all the types declared in this compilation unit.
*/
List<CtType<?>> getDeclaredTypes();

/**
* Sets the types declared in this compilation unit.
*/
void setDeclaredTypes(List<CtType<?>> types);

/**
* Add a type to the list of declared types
*/
void addDeclaredType(CtType type);

/**
* Gets the declared module if the compilationUnit is "module-info.java"
*/
CtModule getDeclaredModule();

/**
* Sets the declared module if the compilationUnit is "module-info.java"
*/
void setDeclaredModule(CtModule module);

/**
* Gets the package declared in the top level type of the compilation unit.
*/
CtPackage getDeclaredPackage();

/**
* Sets the package declared in the top level type of the compilation unit.
*/
void setDeclaredPackage(CtPackage ctPackage);

/**
* Searches and returns the main type (the type which has the same name as
* the file).
*/
CtType<?> getMainType();

/**
* Gets the original source code as a string.
*/
String getOriginalSourceCode();

/**
* Helper method to get the begin index of the line that corresponds to the
@@ -128,6 +33,7 @@ enum UNIT_TYPE {
* an arbitrary index in the source code
* @return the index where the line starts
*/
@Deprecated
int beginOfLineIndex(int index);

/**
@@ -138,6 +44,7 @@ enum UNIT_TYPE {
* an arbitrary index in the source code
* @return the index where the next line starts
*/
@Deprecated
int nextLineIndex(int index);

/**
@@ -147,22 +54,6 @@ enum UNIT_TYPE {
* the index where the line starts in the source code
* @return the number of tabs for this line
*/
@Deprecated
int getTabCount(int index);

/**
* Get the imports computed for this CU.
* WARNING: This method is tagged as experimental, as its signature and/or usage might change in future release.
* @return All the imports from the original source code
*/
@Experimental
Set<CtImport> getImports();

/**
* Set the imports of this CU
* WARNING: This method is tagged as experimental, as its signature and/or usage might change in future release.
* @param imports All the imports of the original source code
*/
@Experimental
void setImports(Set<CtImport> imports);

}
222 changes: 222 additions & 0 deletions src/main/java/spoon/reflect/declaration/CtCompilationUnit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
/**
* Copyright (C) 2006-2018 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and
* abiding by the rules of distribution of free software. You can use, modify
* and/or redistribute the software under the terms of the CeCILL-C license as
* circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package spoon.reflect.declaration;

import java.io.File;
import java.util.Collection;
import java.util.List;

import spoon.reflect.annotations.PropertyGetter;
import spoon.reflect.annotations.PropertySetter;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.path.CtRole;
import spoon.reflect.reference.CtModuleReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.support.DerivedProperty;
import spoon.support.Experimental;
import spoon.support.UnsettableProperty;

/**
* Defines a compilation unit. In Java, a compilation unit can contain only one
* public type declaration and other secondary types declarations (not public).
*/
@Experimental
public interface CtCompilationUnit extends CtElement {
enum UNIT_TYPE {
TYPE_DECLARATION,
PACKAGE_DECLARATION,
MODULE_DECLARATION,
UNKNOWN
}

/**
* Returns the declaration type of the compilation unit.
*/
UNIT_TYPE getUnitType();
/**
* Gets the file that corresponds to this compilation unit if any (contains
* the source code).
*/
File getFile();

/**
* Sets the file that corresponds to this compilation unit.
*/
CtCompilationUnit setFile(File file);

/**
* @return array of offsets in the origin source file, where occurs line separator
*/
int[] getLineSeparatorPositions();

/**
* @param lineSeparatorPositions array of offsets in the origin source file, where occurs line separator
*/
CtCompilationUnit setLineSeparatorPositions(int[] lineSeparatorPositions);
/**
* Gets all binary (.class) files that corresponds to this compilation unit
* and have been created by calling
* {@link spoon.SpoonModelBuilder#compile(spoon.SpoonModelBuilder.InputType...)}.
*/
List<File> getBinaryFiles();

/**
* Gets all the types declared in this compilation unit.
*/
@DerivedProperty
List<CtType<?>> getDeclaredTypes();

/**
* Gets references to all the types declared in this compilation unit.
*/
@PropertyGetter(role = CtRole.DECLARED_TYPE_REF)
List<CtTypeReference<?>> getDeclaredTypeReferences();

/**
* Sets the references to types declared in this compilation unit.
*/
@PropertySetter(role = CtRole.DECLARED_TYPE_REF)
CtCompilationUnit setDeclaredTypeReferences(List<CtTypeReference<?>> types);

/**
* Sets the types declared in this compilation unit.
* It is here for backward compatibility.
* It calls internally {@link #setDeclaredTypeReferences(List)}
* so the {@link CtCompilationUnit} contains type reference only.
* It doesn't contain whole type, which belongs to it's CtPackage in primary `java concept` model.
* Note that {@link CtCompilationUnit} represents a secondary model related to mapping of java modules, packages and types to file system.
*/
@DerivedProperty
CtCompilationUnit setDeclaredTypes(List<CtType<?>> types);

/**
* Add a type to the list of declared types.
* It is here for backward compatibility.
* It calls internally {@link #addDeclaredTypeReference(CtTypeReference)}
* so the {@link CtCompilationUnit} contains type reference only.
* It doesn't contain whole type, which belongs to it's CtPackage in primary `java concept` model.
* Note that {@link CtCompilationUnit} represents a secondary model related to mapping of java modules, packages and types to file system.
*/
@DerivedProperty
CtCompilationUnit addDeclaredType(CtType<?> type);

/**
* Add a type reference to the list of declared types
*/
@PropertySetter(role = CtRole.DECLARED_TYPE_REF)
CtCompilationUnit addDeclaredTypeReference(CtTypeReference<?> type);

/**
* Gets the declared module if the compilationUnit is "module-info.java"
*/
@DerivedProperty
CtModule getDeclaredModule();

/**
* Gets the declared module reference if the compilationUnit is "module-info.java"
*/
@PropertyGetter(role = CtRole.DECLARED_MODULE_REF)
CtModuleReference getDeclaredModuleReference();

/**
* Sets the declared module if the compilationUnit is "module-info.java"
* It is here for backward compatibility.
* It internally calls {@link #setDeclaredModuleReference(CtModuleReference)}
* It doesn't contain whole CtModule, which belongs to CtModel in primary `java concept` model.
* Note that {@link CtCompilationUnit} represents a secondary model related to mapping of java modules, packages and types to file system.
*/
@DerivedProperty
CtCompilationUnit setDeclaredModule(CtModule module);
/**
* Sets the declared module reference if the compilationUnit is "module-info.java"
*/
@PropertySetter(role = CtRole.DECLARED_MODULE_REF)
CtCompilationUnit setDeclaredModuleReference(CtModuleReference module);

/**
* Gets the declared package
*/
@DerivedProperty
CtPackage getDeclaredPackage();

/**
* @return the package declaration
*/
@PropertyGetter(role = CtRole.PACKAGE_DECLARATION)
CtPackageDeclaration getPackageDeclaration();

/**
* Sets the package declaration using the instance of CtPackage.
* It is here for backward compatibility.
* It calls internally {@link #setPackageDeclaration(CtPackageDeclaration)}
* It doesn't contain whole CtPackage, which belongs to it's parent package or to CtModule in primary `java concept` model.
* Note that {@link CtCompilationUnit} represents a secondary model related to mapping of java modules, packages and types to file system.
*/
@DerivedProperty
CtCompilationUnit setDeclaredPackage(CtPackage ctPackage);

/**
* Sets the package declaration
*/
@PropertySetter(role = CtRole.PACKAGE_DECLARATION)
CtCompilationUnit setPackageDeclaration(CtPackageDeclaration packageDeclaration);

/**
* Searches and returns the main type (the type which has the same name as
* the file).
*/
@DerivedProperty
CtType<?> getMainType();

/**
* Gets the original source code as a string.
*/
String getOriginalSourceCode();

/**
* Get the imports computed for this CU.
* WARNING: This method is tagged as experimental, as its signature and/or usage might change in future release.
* @return All the imports from the original source code
*/
@Experimental
@PropertyGetter(role = CtRole.DECLARED_IMPORT)
List<CtImport> getImports();

@Override
CtCompilationUnit clone();

/**
* Set the imports of this CU
* WARNING: This method is tagged as experimental, as its signature and/or usage might change in future release.
* @param imports All the imports of the original source code
*/
@Experimental
@PropertySetter(role = CtRole.DECLARED_IMPORT)
CtCompilationUnit setImports(Collection<CtImport> imports);

@Override
@DerivedProperty
CtElement getParent();

@Override
@UnsettableProperty
<E extends CtElement> E setParent(E parent);

@Override
@UnsettableProperty
<E extends CtElement> E setPosition(SourcePosition position);
}
Loading

0 comments on commit 96d3150

Please sign in to comment.