本文整理汇总了Java中com.github.javaparser.ast.ImportDeclaration.getName方法的典型用法代码示例。如果您正苦于以下问题:Java ImportDeclaration.getName方法的具体用法?Java ImportDeclaration.getName怎么用?Java ImportDeclaration.getName使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.github.javaparser.ast.ImportDeclaration
的用法示例。
在下文中一共展示了ImportDeclaration.getName方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toImportedType
import com.github.javaparser.ast.ImportDeclaration; //导入方法依赖的package包/类
private static Function<? super ImportDeclaration, ? extends String> toImportedType() {
return new Function<ImportDeclaration, String>() {
@Nullable
@Override
public String apply(@Nullable ImportDeclaration input) {
if (input == null) {
return null;
}
NameExpr name = input.getName();
if (input.isStatic() && !input.isAsterisk()) {
name = QualifiedNameExpr.class.cast(name).getQualifier();
}
return name.toString();
}
};
}
示例2: apply
import com.github.javaparser.ast.ImportDeclaration; //导入方法依赖的package包/类
public String apply(Node node) {
if (node instanceof NamedNode) {
String name = ((NamedNode)node).getName();
Node current = node;
while (!(current instanceof CompilationUnit)) {
current = current.getParentNode();
}
CompilationUnit compilationUnit = (CompilationUnit) current;
for (ImportDeclaration importDecl : compilationUnit.getImports()) {
NameExpr importExpr = importDecl.getName();
if (importExpr instanceof QualifiedNameExpr) {
QualifiedNameExpr qualifiedNameExpr = (QualifiedNameExpr) importExpr;
String className = qualifiedNameExpr.getName();
if (name.equals(className)) {
return qualifiedNameExpr.getQualifier().toString();
}
} else if (importDecl.getName().getName().endsWith(SEPARATOR + name)) {
String importName = importDecl.getName().getName();
return importName.substring(0, importName.length() - name.length() -1);
}
}
try {
Class.forName(JAVA_LANG + "." + name);
return JAVA_LANG;
} catch (ClassNotFoundException ex) {
return compilationUnit.getPackage().getPackageName();
}
}
return null;
}
示例3: getImportDeclarationFor
import com.github.javaparser.ast.ImportDeclaration; //导入方法依赖的package包/类
/**
* Looks up the import declaration applicable to the presented name
* expression.
* <p>
* If a fully-qualified name is passed to this method, the corresponding
* import will be evaluated for a complete match. If a simple name is passed
* to this method, the corresponding import will be evaluated if its simple
* name matches. This therefore reflects the normal Java semantics for using
* simple type names that have been imported.
*
* @param compilationUnitService the types in the compilation unit
* (required)
* @param nameExpr the expression to locate an import for (which would
* generally be a {@link NameExpr} and thus not have a package
* identifier; required)
* @return the relevant import, or null if there is no import for the
* expression
*/
private static ImportDeclaration getImportDeclarationFor(
final CompilationUnitService compilationUnitService,
final NameExpr nameExpr) {
Validate.notNull(compilationUnitService,
"Compilation unit services required");
Validate.notNull(nameExpr, "Name expression required");
final List<ImportDeclaration> imports = compilationUnitService
.getImports();
for (final ImportDeclaration candidate : imports) {
final NameExpr candidateNameExpr = candidate.getName();
if (!candidate.toString().contains("*")) {
if(!(candidateNameExpr instanceof QualifiedNameExpr)) {
System.out.println("Expected import '" + candidate
+ "' to use a fully-qualified type name");
}
}
if (nameExpr instanceof QualifiedNameExpr) {
// User is asking for a fully-qualified name; let's see if there
// is a full match
if (isEqual(nameExpr, candidateNameExpr)) {
return candidate;
}
}
else {
// User is not asking for a fully-qualified name, so let's do a
// simple name comparison that discards the import's
// qualified-name package
if (candidateNameExpr.getName().equals(nameExpr.getName())) {
return candidate;
}
}
}
return null;
}
示例4: getImportDeclarationFor
import com.github.javaparser.ast.ImportDeclaration; //导入方法依赖的package包/类
/**
* Looks up the import declaration applicable to the presented name
* expression.
* <p>
* If a fully-qualified name is passed to this method, the corresponding
* import will be evaluated for a complete match. If a simple name is passed
* to this method, the corresponding import will be evaluated if its simple
* name matches. This therefore reflects the normal Java semantics for using
* simple type names that have been imported.
*
* @param compilationUnit the types in the compilation unit
* (required)
* @param nameExpr the expression to locate an import for (which would
* generally be a {@link NameExpr} and thus not have a package
* identifier; required)
* @return the relevant import, or null if there is no import for the
* expression
*/
public static ImportDeclaration getImportDeclarationFor(
final CompilationUnit compilationUnit,
final NameExpr nameExpr) {
Validate.notNull(compilationUnit,
"Compilation unit services required");
Validate.notNull(nameExpr, "Name expression required");
final List<ImportDeclaration> imports = compilationUnit
.getImports();
for (final ImportDeclaration candidate : imports) {
final NameExpr candidateNameExpr = candidate.getName();
if (!candidate.toString().contains("*")) {
if(!(candidateNameExpr instanceof QualifiedNameExpr)) {
System.out.println("Expected import '" + candidate
+ "' to use a fully-qualified type name");
}
}
if (nameExpr instanceof QualifiedNameExpr) {
// User is asking for a fully-qualified name; let's see if there
// is a full match
if (NameUtil.isEqual(nameExpr, candidateNameExpr)) {
return candidate;
}
}
else {
// User is not asking for a fully-qualified name, so let's do a
// simple name comparison that discards the import's
// qualified-name package
if (candidateNameExpr.getName().equals(nameExpr.getName())) {
return candidate;
}
}
}
return null;
}