private static CharSequence readSourceDeclaration(SourceTypeBinding binding) { TypeDeclaration referenceContext = binding.scope.referenceContext; char[] content = referenceContext.compilationResult.compilationUnit.getContents(); int start = referenceContext.declarationSourceStart; int end = referenceContext.declarationSourceEnd; StringBuilder declaration = new StringBuilder(); for (int p = start; p <= end; p++) { char c = content[p]; if (c == '{') { break; } declaration.append(c); } return declaration; }
/** * Creates a JDTCommentBuilder that will insert all comment of the declarationUnit into the Spoon AST * @param declarationUnit the declaration unit * @param factory the Spoon AST */ JDTCommentBuilder(CompilationUnitDeclaration declarationUnit, Factory factory) { this.declarationUnit = declarationUnit; if (declarationUnit.comments == null) { return; } this.factory = factory; this.sourceUnit = declarationUnit.compilationResult.compilationUnit; this.contents = sourceUnit.getContents(); this.filePath = CharOperation.charToString(sourceUnit.getFileName()); this.spoonUnit = JDTTreeBuilder.getOrCreateCompilationUnit(declarationUnit, factory); }
private void sortModuleDeclarationsFirst(ICompilationUnit[] sourceUnits) { Arrays.sort(sourceUnits, (u1, u2) -> { char[] fn1 = u1.getFileName(); char[] fn2 = u2.getFileName(); boolean isMod1 = CharOperation.endsWith(fn1, TypeConstants.MODULE_INFO_FILE_NAME) || CharOperation.endsWith(fn1, TypeConstants.MODULE_INFO_CLASS_NAME); boolean isMod2 = CharOperation.endsWith(fn2, TypeConstants.MODULE_INFO_FILE_NAME) || CharOperation.endsWith(fn2, TypeConstants.MODULE_INFO_CLASS_NAME); if (isMod1 == isMod2) { return 0; } return isMod1 ? -1 : 1; }); }
boolean isPackage(char[][] compoundName, char[] name) { if (compoundName == null || compoundName.length == 0) return this.nameEnvironment.isPackage(null, name); return this.nameEnvironment.isPackage(compoundName, name); } // The method verifier is lazily initialized to guarantee the receiver, the compiler & the oracle are ready.
private boolean affectsSignature(IBinaryTypeAnnotation typeAnnotation) { if (typeAnnotation == null) return false; int targetType = typeAnnotation.getTargetType(); if (targetType >= AnnotationTargetTypeConstants.LOCAL_VARIABLE && targetType <= AnnotationTargetTypeConstants.METHOD_REFERENCE_TYPE_ARGUMENT) return false; // affects detail within a block return true; }
public static IModule createAutomatic(String fileName, boolean isFile, Manifest manifest) { boolean fromManifest = true; char[] inferredName = AutomaticModuleNaming.determineAutomaticModuleNameFromManifest(manifest); if (inferredName == null) { fromManifest = false; inferredName = AutomaticModuleNaming.determineAutomaticModuleNameFromFileName(fileName, true, isFile); } return createAutomatic(inferredName, fromManifest); } }
private void completeIfNeeded(IUpdatableModule.UpdateKind kind) { if (!this.isComplete[kind.ordinal()]) { this.isComplete[kind.ordinal()] = true; if (this.environment.nameEnvironment instanceof IModuleAwareNameEnvironment) { ((IModuleAwareNameEnvironment) this.environment.nameEnvironment).applyModuleUpdates(this, kind); } } }
public static IModule createAutomatic(String fileName, boolean isFile, Manifest manifest) { return createAutomatic(AutomaticModuleNaming.determineAutomaticModuleName(fileName, isFile, manifest)); } }
/** * Test whether the given element matches this lookup strategy. * @param elem location being tests * @param isNamed predicate to determine if 'elem' represents a named module * @return true iff the given element matches this lookup strategy. */ public <T> boolean matches(T elem, Predicate<T> isNamed) { return matchesWithName(elem, isNamed, t -> true); }
public char[][] singletonModuleNameIf(boolean condition) { if (!condition) return null; if (this.module != null) return new char[][] { this.module.name() }; return new char[][] { ModuleBinding.UNNAMED }; } }
public IModule getModule() { if (this.isAutoModule && this.module == null) { return this.module = IModule.createAutomatic(this.path, false, null/*no manifest*/); } return this.module; } }
@Override public CharSequence extract(ProcessingEnvironment environment, TypeElement typeElement) throws IOException { if (typeElement instanceof ElementImpl) { Binding binding = ((ElementImpl) typeElement)._binding; if (binding instanceof SourceTypeBinding) { CompilationUnitDeclaration unit = ((SourceTypeBinding) binding).scope.referenceCompilationUnit(); char[] contents = unit.compilationResult.compilationUnit.getContents(); return CharBuffer.wrap(contents); } } return UNABLE_TO_EXTRACT; }
JDTImportBuilder(CompilationUnitDeclaration declarationUnit, Factory factory) { this.declarationUnit = declarationUnit; this.factory = factory; this.sourceUnit = declarationUnit.compilationResult.compilationUnit; this.filePath = CharOperation.charToString(sourceUnit.getFileName()); // get the CU: it has already been built during model building in JDTBasedSpoonCompiler this.spoonUnit = JDTTreeBuilder.getOrCreateCompilationUnit(declarationUnit, factory); this.imports = new HashSet<>(); }
private boolean affectsSignature(IBinaryTypeAnnotation typeAnnotation) { if (typeAnnotation == null) return false; int targetType = typeAnnotation.getTargetType(); if (targetType >= AnnotationTargetTypeConstants.LOCAL_VARIABLE && targetType <= AnnotationTargetTypeConstants.METHOD_REFERENCE_TYPE_ARGUMENT) return false; // affects detail within a block return true; }
private void completeIfNeeded(IUpdatableModule.UpdateKind kind) { if (!this.isComplete[kind.ordinal()]) { this.isComplete[kind.ordinal()] = true; if (this.environment.nameEnvironment instanceof IModuleAwareNameEnvironment) { ((IModuleAwareNameEnvironment) this.environment.nameEnvironment).applyModuleUpdates(this, kind); } } }
@Override public CharSequence extractReturnType(ExecutableElement executableElement) { if (executableElement instanceof ExecutableElementImpl) { Binding binding = ((ExecutableElementImpl) executableElement)._binding; if (binding instanceof MethodBinding) { MethodBinding methodBinding = (MethodBinding) binding; @Nullable AbstractMethodDeclaration sourceMethod = methodBinding.sourceMethod(); if (sourceMethod != null) { CharSequence rawType = getRawType(methodBinding); char[] content = sourceMethod.compilationResult.compilationUnit.getContents(); int sourceEnd = methodBinding.sourceStart();// intentionaly int sourceStart = scanForTheSourceStart(content, sourceEnd); char[] methodTest = Arrays.copyOfRange(content, sourceStart, sourceEnd); Entry<String, List<String>> extracted = SourceTypes.extract(String.valueOf(methodTest)); return SourceTypes.stringify( Maps.immutableEntry(rawType.toString(), extracted.getValue())); } } } return UNABLE_TO_EXTRACT; }
public CompilationResult(ICompilationUnit compilationUnit, int unitIndex, int totalUnitsKnown, int maxProblemPerUnit){ this.fileName = compilationUnit.getFileName(); this.compilationUnit = compilationUnit; this.unitIndex = unitIndex; this.totalUnitsKnown = totalUnitsKnown; this.maxProblemPerUnit = maxProblemPerUnit; }
/** * @return char content of actually processed compilation unit */ public char[] getCompilationUnitContents() { return compilationunitdeclaration.compilationResult.compilationUnit.getContents(); } }
private char[] getSource() { if (this.source == null) this.source = this.cu.getContents(); return this.source; }
private CtImport createImportWithPosition(CtReference ref, ImportReference importRef) { char[] content = sourceUnit.getContents(); CtImport imprt = factory.Type().createImport(ref); //include comment before import int declStart = importRef.declarationSourceStart; int commentStart = PositionBuilder.findNextNonWhitespace(false, content, declStart, PositionBuilder.findPrevNonWhitespace(content, 0, declStart - 1) + 1); imprt.setPosition(factory.Core().createCompoundSourcePosition(spoonUnit, importRef.sourceStart(), importRef.sourceEnd(), commentStart, importRef.declarationEnd, spoonUnit.getLineSeparatorPositions())); imprt.getReference().setPosition(factory.Core().createSourcePosition(spoonUnit, importRef.sourceStart(), importRef.sourceEnd(), spoonUnit.getLineSeparatorPositions())); return imprt; }