private static boolean isEqual(String packageName, ImportReference pkgOrStarImport) { if (pkgOrStarImport == null || pkgOrStarImport.tokens == null || pkgOrStarImport.tokens.length == 0) return packageName.isEmpty(); int pos = 0; int len = packageName.length(); for (int i = 0; i < pkgOrStarImport.tokens.length; i++) { if (i != 0) { if (pos >= len) return false; if (packageName.charAt(pos++) != '.') return false; } for (int j = 0; j < pkgOrStarImport.tokens[i].length; j++) { if (pos >= len) return false; if (packageName.charAt(pos++) != pkgOrStarImport.tokens[i][j]) return false; } } return true; }
private void format(ImportReference importRef, boolean isLast) { this.scribe.printNextToken(TerminalTokens.TokenNameimport); if (!isLast) this.scribe.blank_lines_between_import_groups = this.preferences.blank_lines_between_import_groups; this.scribe.space(); if (importRef.isStatic()) { this.scribe.printNextToken(TerminalTokens.TokenNamestatic); this.scribe.space(); } if ((importRef.bits & ASTNode.OnDemand) != 0) { this.scribe.printQualifiedReference(importRef.sourceEnd, false/*do not expect parenthesis*/); this.scribe.printNextToken(TerminalTokens.TokenNameDOT); this.scribe.printNextToken(TerminalTokens.TokenNameMULTIPLY); this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); } else { this.scribe.printQualifiedReference(importRef.sourceEnd, false/*do not expect parenthesis*/); this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); } if (isLast) { this.scribe.blank_lines_between_import_groups = -1; this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.IMPORT_TRAILING_COMMENT); } else { this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.NO_TRAILING_COMMENT); this.scribe.blank_lines_between_import_groups = -1; } this.scribe.printNewLine(); }
protected ImportReference createImportReference( String[] importName, int start, int end, boolean onDemand, int modifiers) { int length = importName.length; long[] positions = new long[length]; long position = ((long) start << 32) + end; char[][] qImportName = new char[length][]; for (int i = 0; i < length; i++) { qImportName[i] = importName[i].toCharArray(); positions[i] = position; // dummy positions } return new ImportReference( qImportName, positions, onDemand, modifiers); }
protected void notifySourceElementRequestor( ImportReference importReference, boolean isPackage) { if (isPackage) { this.requestor.acceptPackage(importReference); } else { final boolean onDemand = (importReference.bits & ASTNode.OnDemand) != 0; this.requestor.acceptImport( importReference.declarationSourceStart, importReference.declarationSourceEnd, importReference.sourceStart, onDemand ? importReference.trailingStarPosition : importReference.sourceEnd, importReference.tokens, onDemand, importReference.modifiers); } }
public RecoveredElement add(ImportReference importReference, int bracketBalanceValue) { resetPendingModifiers(); if (this.imports == null) { this.imports = new RecoveredImport[5]; this.importCount = 0; } else { if (this.importCount == this.imports.length) { System.arraycopy( this.imports, 0, (this.imports = new RecoveredImport[2 * this.importCount]), 0, this.importCount); } } RecoveredImport element = new RecoveredImport(importReference, this, bracketBalanceValue); this.imports[this.importCount++] = element; /* if import not finished, then import becomes current */ if (importReference.declarationSourceEnd == 0) return element; return this; }
protected void consumeImportDeclaration() { // SingleTypeImportDeclaration ::= SingleTypeImportDeclarationName ';' ImportReference impt = (ImportReference) this.astStack[this.astPtr]; // flush annotations defined prior to import statements impt.declarationEnd = this.endStatementPosition; impt.declarationSourceEnd = flushCommentsDefinedPriorTo(impt.declarationSourceEnd); // recovery if (this.currentElement != null) { this.lastCheckPoint = impt.declarationSourceEnd + 1; this.currentElement = this.currentElement.add(impt, 0); this.lastIgnoredToken = -1; this.restartRecovery = true; // used to avoid branching back into the regular automaton } }
public void installStubs(final Resource resource) { boolean _isInfoFile = this.isInfoFile(resource); if (_isInfoFile) { return; } final CompilationUnit compilationUnit = this.getCompilationUnit(resource); IErrorHandlingPolicy _proceedWithAllProblems = DefaultErrorHandlingPolicies.proceedWithAllProblems(); CompilerOptions _compilerOptions = this.getCompilerOptions(resource); DefaultProblemFactory _defaultProblemFactory = new DefaultProblemFactory(); ProblemReporter _problemReporter = new ProblemReporter(_proceedWithAllProblems, _compilerOptions, _defaultProblemFactory); final Parser parser = new Parser(_problemReporter, true); final CompilationResult compilationResult = new CompilationResult(compilationUnit, 0, 1, (-1)); final CompilationUnitDeclaration result = parser.dietParse(compilationUnit, compilationResult); if ((result.types != null)) { for (final TypeDeclaration type : result.types) { { ImportReference _currentPackage = result.currentPackage; char[][] _importName = null; if (_currentPackage!=null) { _importName=_currentPackage.getImportName(); } List<String> _map = null; if (((List<char[]>)Conversions.doWrapArray(_importName))!=null) { final Function1<char[], String> _function = (char[] it) -> { return String.valueOf(it); }; _map=ListExtensions.<char[], String>map(((List<char[]>)Conversions.doWrapArray(_importName)), _function); } String _join = null; if (_map!=null) { _join=IterableExtensions.join(_map, "."); } final String packageName = _join; final JvmDeclaredType jvmType = this.createType(type, packageName); resource.getContents().add(jvmType); } } } }
@Override public String getFullyQualifiedNameForSimpleName(String unqualified) { if (imports != null) { outer: for (ImportReference imp : imports) { if ((imp.bits & ASTNode.OnDemand) != 0) continue; char[][] tokens = imp.tokens; char[] token = tokens.length == 0 ? new char[0] : tokens[tokens.length - 1]; int len = token.length; if (len != unqualified.length()) continue; for (int i = 0; i < len; i++) if (token[i] != unqualified.charAt(i)) continue outer; return LombokInternalAliasing.processAliases(toQualifiedName(tokens)); } } return null; }
@Override public Collection<String> applyNameToStarImports(String startsWith, String name) { List<String> out = Collections.emptyList(); if (pkg != null && pkg.tokens != null && pkg.tokens.length != 0) { char[] first = pkg.tokens[0]; int len = first.length; boolean match = true; if (startsWith.length() == len) { for (int i = 0; match && i < len; i++) { if (startsWith.charAt(i) != first[i]) match = false; } if (match) out.add(toQualifiedName(pkg.tokens) + "." + name); } } if (imports != null) { outer: for (ImportReference imp : imports) { if ((imp.bits & ASTNode.OnDemand) == 0) continue; if (imp.isStatic()) continue; if (imp.tokens == null || imp.tokens.length == 0) continue; char[] firstToken = imp.tokens[0]; if (firstToken.length != startsWith.length()) continue; for (int i = 0; i < firstToken.length; i++) if (startsWith.charAt(i) != firstToken[i]) continue outer; String fqn = toQualifiedName(imp.tokens) + "." + name; if (out.isEmpty()) out = Collections.singletonList(fqn); else if (out.size() == 1) { out = new ArrayList<String>(out); out.add(fqn); } else { out.add(fqn); } } } return out; }
private void fixPositions(ImportReference node) { node.sourceEnd = sourceEnd; node.sourceStart = sourceStart; node.declarationEnd = sourceEnd; node.declarationSourceEnd = sourceEnd; node.declarationSourceStart = sourceStart; if (node.sourcePositions == null || node.sourcePositions.length != node.tokens.length) node.sourcePositions = new long[node.tokens.length]; Arrays.fill(node.sourcePositions, sourcePos); }
protected void notifySourceElementRequestor(ImportReference importReference, boolean isPackage) { if (importReference instanceof CompletionOnKeyword2) return; if (importReference instanceof CompletionOnImportReference || importReference instanceof CompletionOnPackageReference) { if (importReference.tokens[importReference.tokens.length - 1].length == 0) return; } super.notifySourceElementRequestor(importReference, isPackage); }
protected ImportReference newImportReference(char[][] tokens, long[] sourcePositions, boolean onDemand, int mod) { ImportReference ref = this.importReference; ref.tokens = tokens; ref.sourcePositions = sourcePositions; if (onDemand) { ref.bits |= ASTNode.OnDemand; } ref.sourceEnd = (int) (sourcePositions[sourcePositions.length-1] & 0x00000000FFFFFFFF); ref.sourceStart = (int) (sourcePositions[0] >>> 32); ref.modifiers = this.modifiers; return ref; }
protected void matchLevelAndReportImportRef(ImportReference importRef, Binding binding, MatchLocator locator) throws CoreException { // for static import, binding can be a field binding or a member type binding // verify that in this case binding is static and use declaring class for fields Binding refBinding = binding; if (importRef.isStatic()) { if (binding instanceof FieldBinding) { FieldBinding fieldBinding = (FieldBinding) binding; if (!fieldBinding.isStatic()) return; refBinding = fieldBinding.declaringClass; } else if (binding instanceof MethodBinding) { MethodBinding methodBinding = (MethodBinding) binding; if (!methodBinding.isStatic()) return; refBinding = methodBinding.declaringClass; } else if (binding instanceof MemberTypeBinding) { MemberTypeBinding memberBinding = (MemberTypeBinding) binding; if (!memberBinding.isStatic()) return; } } // Look for closest pattern PatternLocator closestPattern = null; int level = IMPOSSIBLE_MATCH; for (int i = 0, length = this.patternLocators.length; i < length; i++) { PatternLocator patternLocator = this.patternLocators[i]; int newLevel = patternLocator.referenceType() == 0 ? IMPOSSIBLE_MATCH : patternLocator.resolveLevel(refBinding); if (newLevel > level) { closestPattern = patternLocator; if (newLevel == ACCURATE_MATCH) break; level = newLevel; } } if (closestPattern != null) { closestPattern.matchLevelAndReportImportRef(importRef, binding, locator); } }
protected void matchReportImportRef(ImportReference importRef, Binding binding, IJavaElement element, int accuracy, MatchLocator locator) throws CoreException { PatternLocator closestPattern = null; int level = IMPOSSIBLE_MATCH; for (int i = 0, length = this.patternLocators.length; i < length; i++) { int newLevel = this.patternLocators[i].matchLevel(importRef); if (newLevel > level) { closestPattern = this.patternLocators[i]; if (newLevel == ACCURATE_MATCH) break; level = newLevel; } } if (closestPattern != null) closestPattern.matchReportImportRef(importRef, binding, element, accuracy, locator); }
protected void matchReportImportRef(ImportReference importRef, Binding binding, IJavaElement element, int accuracy, MatchLocator locator) throws CoreException { PatternLocator weakestPattern = null; int level = IMPOSSIBLE_MATCH; for (int i = 0, length = this.patternLocators.length; i < length; i++) { int newLevel = this.patternLocators[i].matchLevel(importRef); if (newLevel == IMPOSSIBLE_MATCH) return; if (weakestPattern == null || newLevel < level) { weakestPattern = this.patternLocators[i]; level = newLevel; } } weakestPattern.matchReportImportRef(importRef, binding, element, accuracy, locator); }
/** * @see ISourceElementRequestor */ public void acceptPackage(ImportReference importReference) { Object parentInfo = this.infoStack.peek(); JavaElement parentHandle= (JavaElement) this.handleStack.peek(); PackageDeclaration handle = null; if (parentHandle.getElementType() == IJavaElement.COMPILATION_UNIT) { char[] name = CharOperation.concatWith(importReference.getImportName(), '.'); handle = createPackageDeclaration(parentHandle, new String(name)); } else { Assert.isTrue(false); // Should not happen } resolveDuplicates(handle); AnnotatableInfo info = new AnnotatableInfo(); info.setSourceRangeStart(importReference.declarationSourceStart); info.setSourceRangeEnd(importReference.declarationSourceEnd); info.setNameSourceStart(importReference.sourceStart); info.setNameSourceEnd(importReference.sourceEnd); addToChildren(parentInfo, handle); this.newElements.put(handle, info); if (importReference.annotations != null) { for (int i = 0, length = importReference.annotations.length; i < length; i++) { org.eclipse.jdt.internal.compiler.ast.Annotation annotation = importReference.annotations[i]; acceptAnnotation(annotation, info, handle); } } }
public ImportReference[] buildImports(ClassFileReader reader) { // add remaining references to the list of type names // (code extracted from BinaryIndexer#extractReferenceFromConstantPool(...)) int[] constantPoolOffsets = reader.getConstantPoolOffsets(); int constantPoolCount = constantPoolOffsets.length; for (int i = 0; i < constantPoolCount; i++) { int tag = reader.u1At(constantPoolOffsets[i]); char[] name = null; switch (tag) { case ClassFileConstants.MethodRefTag : case ClassFileConstants.InterfaceMethodRefTag : int constantPoolIndex = reader.u2At(constantPoolOffsets[i] + 3); int utf8Offset = constantPoolOffsets[reader.u2At(constantPoolOffsets[constantPoolIndex] + 3)]; name = reader.utf8At(utf8Offset + 3, reader.u2At(utf8Offset + 1)); break; case ClassFileConstants.ClassTag : utf8Offset = constantPoolOffsets[reader.u2At(constantPoolOffsets[i] + 1)]; name = reader.utf8At(utf8Offset + 3, reader.u2At(utf8Offset + 1)); break; } if (name == null || (name.length > 0 && name[0] == '[')) break; // skip over array references this.typeNames.add(CharOperation.splitOn('/', name)); } // convert type names into import references int typeNamesLength = this.typeNames.size(); ImportReference[] imports = new ImportReference[typeNamesLength]; char[][][] set = this.typeNames.set; int index = 0; for (int i = 0, length = set.length; i < length; i++) { char[][] typeName = set[i]; if (typeName != null) { imports[index++] = new ImportReference(typeName, new long[typeName.length]/*dummy positions*/, false/*not on demand*/, 0); } } return imports; }
/** * Convert a binary type into an AST type declaration and put it in the given compilation unit. */ public TypeDeclaration buildTypeDeclaration(IType type, CompilationUnitDeclaration compilationUnit) throws JavaModelException { PackageFragment pkg = (PackageFragment) type.getPackageFragment(); char[][] packageName = Util.toCharArrays(pkg.names); if (packageName.length > 0) { compilationUnit.currentPackage = new ImportReference(packageName, new long[]{0}, false, ClassFileConstants.AccDefault); } /* convert type */ TypeDeclaration typeDeclaration = convert(type, null, null); IType alreadyComputedMember = type; IType parent = type.getDeclaringType(); TypeDeclaration previousDeclaration = typeDeclaration; while(parent != null) { TypeDeclaration declaration = convert(parent, alreadyComputedMember, previousDeclaration); alreadyComputedMember = parent; previousDeclaration = declaration; parent = parent.getDeclaringType(); } compilationUnit.types = new TypeDeclaration[]{previousDeclaration}; return typeDeclaration; }
synchronized IPackageBinding resolvePackage(PackageDeclaration pkg) { if (this.scope == null) return null; try { org.eclipse.jdt.internal.compiler.ast.ASTNode node = (org.eclipse.jdt.internal.compiler.ast.ASTNode) this.newAstToOldAst.get(pkg); if (node instanceof ImportReference) { ImportReference importReference = (ImportReference) node; Binding binding = this.scope.getOnlyPackage(CharOperation.subarray(importReference.tokens, 0, importReference.tokens.length)); if ((binding != null) && (binding.isValidBinding())) { if (binding instanceof ReferenceBinding) { // this only happens if a type name has the same name as its package ReferenceBinding referenceBinding = (ReferenceBinding) binding; binding = referenceBinding.fPackage; } if (binding instanceof org.eclipse.jdt.internal.compiler.lookup.PackageBinding) { IPackageBinding packageBinding = getPackageBinding((org.eclipse.jdt.internal.compiler.lookup.PackageBinding) binding); if (packageBinding == null) { return null; } this.bindingsToAstNodes.put(packageBinding, pkg); String key = packageBinding.getKey(); if (key != null) { this.bindingTables.bindingKeysToBindings.put(key, packageBinding); } return packageBinding; } } } } catch (AbortCompilation e) { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=53357 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=63550 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=64299 } return null; }
public RecoveredElement add(ImportReference importReference, int bracketBalanceValue){ /* default behavior is to delegate recording to parent if any */ resetPendingModifiers(); if (this.parent == null) return this; // ignore this.updateSourceEndIfNecessary(previousAvailableLineEnd(importReference.declarationSourceStart - 1)); return this.parent.add(importReference, bracketBalanceValue); }
protected void consumePackageDeclaration() { // PackageDeclaration ::= 'package' Name ';' /* build an ImportRef build from the last name stored in the identifier stack. */ ImportReference impt = this.compilationUnit.currentPackage; this.compilationUnit.javadoc = this.javadoc; this.javadoc = null; // flush comments defined prior to import statements impt.declarationEnd = this.endStatementPosition; impt.declarationSourceEnd = flushCommentsDefinedPriorTo(impt.declarationSourceEnd); }
protected void consumeReduceImports() { // Consume imports int length; if ((length = this.astLengthStack[this.astLengthPtr--]) != 0) { this.astPtr -= length; System.arraycopy( this.astStack, this.astPtr + 1, this.compilationUnit.imports = new ImportReference[length], 0, length); } }
protected void consumeSingleTypeImportDeclarationName() { // SingleTypeImportDeclarationName ::= 'import' Name /* push an ImportRef build from the last name stored in the identifier stack. */ ImportReference impt; int length; char[][] tokens = new char[length = this.identifierLengthStack[this.identifierLengthPtr--]][]; this.identifierPtr -= length; long[] positions = new long[length]; System.arraycopy(this.identifierStack, this.identifierPtr + 1, tokens, 0, length); System.arraycopy(this.identifierPositionStack, this.identifierPtr + 1, positions, 0, length); pushOnAstStack(impt = new ImportReference(tokens, positions, false, ClassFileConstants.AccDefault)); if (this.currentToken == TokenNameSEMICOLON){ impt.declarationSourceEnd = this.scanner.currentPosition - 1; } else { impt.declarationSourceEnd = impt.sourceEnd; } impt.declarationEnd = impt.declarationSourceEnd; //this.endPosition is just before the ; impt.declarationSourceStart = this.intStack[this.intPtr--]; // recovery if (this.currentElement != null){ this.lastCheckPoint = impt.declarationSourceEnd+1; this.currentElement = this.currentElement.add(impt, 0); this.lastIgnoredToken = -1; this.restartRecovery = true; // used to avoid branching back into the regular automaton } }
protected void consumeTypeImportOnDemandDeclarationName() { // TypeImportOnDemandDeclarationName ::= 'import' Name '.' RejectTypeAnnotations '*' /* push an ImportRef build from the last name stored in the identifier stack. */ ImportReference impt; int length; char[][] tokens = new char[length = this.identifierLengthStack[this.identifierLengthPtr--]][]; this.identifierPtr -= length; long[] positions = new long[length]; System.arraycopy(this.identifierStack, this.identifierPtr + 1, tokens, 0, length); System.arraycopy(this.identifierPositionStack, this.identifierPtr + 1, positions, 0, length); pushOnAstStack(impt = new ImportReference(tokens, positions, true, ClassFileConstants.AccDefault)); // star end position impt.trailingStarPosition = this.intStack[this.intPtr--]; if (this.currentToken == TokenNameSEMICOLON){ impt.declarationSourceEnd = this.scanner.currentPosition - 1; } else { impt.declarationSourceEnd = impt.sourceEnd; } impt.declarationEnd = impt.declarationSourceEnd; //this.endPosition is just before the ; impt.declarationSourceStart = this.intStack[this.intPtr--]; // recovery if (this.currentElement != null){ this.lastCheckPoint = impt.declarationSourceEnd+1; this.currentElement = this.currentElement.add(impt, 0); this.lastIgnoredToken = -1; this.restartRecovery = true; // used to avoid branching back into the regular automaton } }
public void cannotImportPackage(ImportReference importRef) { String[] arguments = new String[] {CharOperation.toString(importRef.tokens)}; this.handle( IProblem.CannotImportPackage, arguments, arguments, importRef.sourceStart, importRef.sourceEnd); }
public void conflictingImport(ImportReference importRef) { String[] arguments = new String[] {CharOperation.toString(importRef.tokens)}; this.handle( IProblem.ConflictingImport, arguments, arguments, importRef.sourceStart, importRef.sourceEnd); }
public void duplicateImport(ImportReference importRef) { String[] arguments = new String[] {CharOperation.toString(importRef.tokens)}; this.handle( IProblem.DuplicateImport, arguments, arguments, importRef.sourceStart, importRef.sourceEnd); }
public void invalidUsageOfStaticImports(ImportReference staticImport) { this.handle( IProblem.InvalidUsageOfStaticImports, NoArgument, NoArgument, staticImport.declarationSourceStart, staticImport.declarationSourceEnd); }
public void unusedImport(ImportReference importRef) { int severity = computeSeverity(IProblem.UnusedImport); if (severity == ProblemSeverities.Ignore) return; String[] arguments = new String[] { CharOperation.toString(importRef.tokens) }; this.handle( IProblem.UnusedImport, arguments, arguments, severity, importRef.sourceStart, importRef.sourceEnd); }