List of usage examples for org.eclipse.jdt.internal.compiler.ast ImportReference getImportName
public char[][] getImportName()
From source file:com.android.tools.lint.psi.EcjPsiBuilder.java
License:Apache License
private EcjPsiImport toImport(EcjPsiImportList parent, ImportReference importReference) { String typeName = getTypeName(importReference.getImportName()); boolean onDemand = (importReference.bits & ASTNode.OnDemand) != 0; EcjPsiImport ref;// w w w . j a va2 s. c o m if ((importReference.modifiers & ClassFileConstants.AccStatic) != 0) { ref = new EcjPsiStaticImport(mManager, importReference, typeName, onDemand); } else { ref = new EcjPsiImport(mManager, importReference, typeName, onDemand); } ref.setReference(toImportReference(ref, importReference)); parent.adoptChild(ref); return ref; }
From source file:com.codenvy.ide.ext.java.server.internal.core.CompilationUnitStructureRequestor.java
License:Open Source License
/** * @see org.eclipse.jdt.internal.compiler.ISourceElementRequestor *//*from w ww. j a va 2 s .c om*/ 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); } } }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.indexing.SourceIndexerRequestor.java
License:Open Source License
/** * @see org.eclipse.jdt.internal.compiler.ISourceElementRequestor#acceptPackage(org.eclipse.jdt.internal.compiler.ast.ImportReference) *//*from www . ja v a2s . c o m*/ public void acceptPackage(ImportReference importReference) { this.packageName = CharOperation.concatWith(importReference.getImportName(), '.'); }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java
License:Open Source License
/** * Creates an IImportDeclaration from the given import statement *///w w w .j a v a2 s. c o m protected IJavaElement createImportHandle(ImportReference importRef) { char[] importName = CharOperation.concatWith(importRef.getImportName(), '.'); if ((importRef.bits & ASTNode.OnDemand) != 0) importName = CharOperation.concat(importName, ".*".toCharArray()); //$NON-NLS-1$ Openable openable = this.currentPossibleMatch.openable; if (openable instanceof CompilationUnit) return ((CompilationUnit) openable).getImport(new String(importName)); // binary types do not contain import statements so just answer the top-level type as the element IType binaryType = ((ClassFile) openable).getType(); String typeName = binaryType.getElementName(); int lastDollar = typeName.lastIndexOf('$'); if (lastDollar == -1) return binaryType; return createTypeHandle(typeName.substring(0, lastDollar)); }
From source file:com.google.gwt.dev.javac.UnusedImportsRemover.java
License:Apache License
void execImpl(CompilationUnitDeclaration cud) { if (cud.imports == null) { return;/*from w ww .j a v a 2 s . c o m*/ } AccumulateNamesVisitor astVisitor = new AccumulateNamesVisitor(); if (cud.types != null) { for (TypeDeclaration typeDecl : cud.types) { typeDecl.traverse(astVisitor, cud.scope); } } // for some reason JDT does not traverse package annotations even if the traversal started at // the Compilation unit declaration. Hence we do it manually. if (cud.currentPackage != null && cud.currentPackage.annotations != null) { for (Annotation annotation : cud.currentPackage.annotations) { annotation.traverse(astVisitor, (BlockScope) null); } } List<ImportReference> newImports = new ArrayList<ImportReference>(); for (ImportReference importRef : cud.imports) { String importName = new String(importRef.getImportName()[importRef.getImportName().length - 1]); if (importName.equals("*") || // very hacky it seems that this is the only way // to notice a import static blah.Blah.*; importRef.trailingStarPosition > 0 || usedNames.contains(importName)) { // Either a * or a possible reference, so keep it. newImports.add(importRef); } } if (newImports.size() != cud.imports.length) { cud.imports = newImports.toArray(new ImportReference[newImports.size()]); } }
From source file:lombok.eclipse.EclipseAST.java
License:Open Source License
private static String packageDeclaration(CompilationUnitDeclaration cud) { ImportReference pkg = cud.currentPackage; return pkg == null ? null : Eclipse.toQualifiedName(pkg.getImportName()); }
From source file:org.nabucco.framework.mda.model.java.ast.comparator.ImportDeclarationComparator.java
License:Open Source License
@Override public int compare(ImportReference i1, ImportReference i2) { if (i1 == null && i2 == null) { return 0; }//from www . ja v a 2 s. c o m if (i1 == null) { return -1; } if (i2 == null) { return 1; } char[][] import1 = i1.getImportName(); char[][] import2 = i2.getImportName(); return this.compare(import1, import2); }
From source file:spoon.support.compiler.jdt.JDTTreeBuilderQuery.java
License:Open Source License
/** * Searches a type declared in imports.//from w w w . ja va2s. c o m * * @param typeName * Expected type name. * @param imports * Search the type in imports. * @return qualified name of the expected type. */ static String searchType(String typeName, ImportReference[] imports) { if (typeName == null || imports == null) { return null; } for (ImportReference anImport : imports) { final String importType = CharOperation .charToString(anImport.getImportName()[anImport.getImportName().length - 1]); if (typeName.equals(importType)) { return CharOperation.toString(anImport.getImportName()); } } return null; }
From source file:spoon.support.compiler.jdt.ReferenceBuilder.java
License:Open Source License
/** * Try to get the declaring reference (package or type) from imports of the current * compilation unit declaration (current class). This method returns a CtReference * which can be a CtTypeReference if it retrieves the information in an static import, * a CtPackageReference if it retrieves the information in an standard import, otherwise * it returns null./*from www . j a v a 2 s .co m*/ * * @param expectedName Name expected in imports. * @return CtReference which can be a CtTypeReference, a CtPackageReference or null. */ CtReference getDeclaringReferenceFromImports(char[] expectedName) { CompilationUnitDeclaration cuDeclaration = this.jdtTreeBuilder .getContextBuilder().compilationunitdeclaration; if (cuDeclaration == null) { return null; } LookupEnvironment environment = cuDeclaration.scope.environment; if (cuDeclaration.imports != null) { for (ImportReference anImport : cuDeclaration.imports) { if (CharOperation.equals(anImport.getImportName()[anImport.getImportName().length - 1], expectedName)) { if (anImport.isStatic()) { int indexDeclaring = 2; if ((anImport.bits & ASTNode.OnDemand) != 0) { // With .* indexDeclaring = 1; } char[][] packageName = CharOperation.subarray(anImport.getImportName(), 0, anImport.getImportName().length - indexDeclaring); char[][] className = CharOperation.subarray(anImport.getImportName(), anImport.getImportName().length - indexDeclaring, anImport.getImportName().length - (indexDeclaring - 1)); PackageBinding aPackage; try { if (packageName.length != 0) { aPackage = environment.createPackage(packageName); } else { aPackage = null; } final MissingTypeBinding declaringType = environment.createMissingType(aPackage, className); this.jdtTreeBuilder.getContextBuilder().ignoreComputeImports = true; final CtTypeReference<Object> typeReference = getTypeReference(declaringType); this.jdtTreeBuilder.getContextBuilder().ignoreComputeImports = false; return typeReference; } catch (NullPointerException e) { return null; } } else { PackageBinding packageBinding = null; char[][] chars = CharOperation.subarray(anImport.getImportName(), 0, anImport.getImportName().length - 1); // `findImport(chars, false, false);` and `createPackage(chars)` require // an array with a minimum length of 1 and throw an // ArrayIndexOutOfBoundsException if `chars.length == 0`. Fixes #759. if (chars.length > 0) { Binding someBinding = cuDeclaration.scope.findImport(chars, false, false); if (someBinding != null && someBinding.isValidBinding() && someBinding instanceof PackageBinding) { packageBinding = (PackageBinding) someBinding; } else { try { packageBinding = environment.createPackage(chars); } catch (NullPointerException e) { packageBinding = null; } } } if (packageBinding == null || packageBinding instanceof ProblemPackageBinding) { // Big crisis here. We are already in noclasspath mode but JDT doesn't support always // creation of a package in this mode. So, if we are in this brace, we make the job of JDT... packageBinding = new PackageBinding(chars, null, environment, environment.module); } return getPackageReference(packageBinding); } } } } return null; }