下面列出了org.eclipse.jdt.core.dom.ITypeBinding#isTopLevel ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static ICompilationUnit findCompilationUnit(ITypeBinding typeBinding, IJavaProject project) throws JavaModelException {
if (!typeBinding.isFromSource()) {
return null;
}
while (typeBinding != null && !typeBinding.isTopLevel()) {
typeBinding= typeBinding.getDeclaringClass();
}
if (typeBinding != null) {
IPackageBinding pack= typeBinding.getPackage();
String packageName= pack.isUnnamed() ? "" : pack.getName(); //$NON-NLS-1$
IType type= project.findType(packageName, typeBinding.getName());
if (type != null) {
return type.getCompilationUnit();
}
}
return null;
}
/**
* Fill-in the "Super Class" and "Super Interfaces" fields.
* @param page the wizard page.
*/
private void fillInWizardPageSuperTypes(NewTypeWizardPage page) {
ITypeBinding type= getPossibleSuperTypeBinding(fNode);
type= Bindings.normalizeTypeBinding(type);
if (type != null) {
if (type.isArray()) {
type= type.getElementType();
}
if (type.isTopLevel() || type.isMember()) {
if (type.isClass() && (fTypeKind == K_CLASS)) {
page.setSuperClass(type.getQualifiedName(), true);
} else if (type.isInterface()) {
List<String> superInterfaces= new ArrayList<String>();
superInterfaces.add(type.getQualifiedName());
page.setSuperInterfaces(superInterfaces, true);
}
}
}
}
public String getQualifiedName(ITypeBinding binding) {
if (binding.isParameterizedType()) {
return getQualifiedName(binding.getErasure());
}
if (binding.isArray()) {
return getQualifiedName(binding.getComponentType(), new StringBuilder()).append("[]").toString();
}
if (binding.isTopLevel() || binding.isTypeVariable() || binding.isPrimitive())
return binding.getQualifiedName();
return getQualifiedName(binding.getDeclaringClass(), new StringBuilder()).append('$').append(binding.getName()).toString();
}
/**
* @since 2.4
*/
public StringBuilder getQualifiedName(ITypeBinding binding, StringBuilder stringBuilder) {
if (binding.isParameterizedType()) {
getQualifiedName(binding.getErasure(), stringBuilder);
}
else if (binding.isArray()) {
getQualifiedName(binding.getComponentType(), stringBuilder).append("[]");
}
else if (binding.isTopLevel() || binding.isTypeVariable() || binding.isPrimitive()) {
stringBuilder.append(binding.getQualifiedName());
} else {
getQualifiedName(binding.getDeclaringClass(), stringBuilder).append('$').append(binding.getName());
}
return stringBuilder;
}
/**
* @since 2.4
*/
protected SegmentSequence.Builder getQualifiedName(ITypeBinding binding, SegmentSequence.Builder builder) {
if (binding.isParameterizedType()) {
getQualifiedName(binding.getErasure(), builder);
}
else if (binding.isArray()) {
getQualifiedName(binding.getComponentType(), builder).append("[]");
}
else if (binding.isTopLevel() || binding.isTypeVariable() || binding.isPrimitive()) {
builder.append(binding.getQualifiedName());
} else {
getQualifiedName(binding.getDeclaringClass(), builder).append('$').append(binding.getName());
}
return builder;
}
/**
* Initialized the type from the given binding
*
* @param binding the binding to initialize from
*/
protected void initialize(ITypeBinding binding) {
fBindingKey= binding.getKey();
Assert.isNotNull(fBindingKey);
fModifiers= binding.getModifiers();
if (binding.isClass()) {
fFlags= F_IS_CLASS;
// the annotation test has to be done before test for interface
// since annotations are interfaces as well.
} else if (binding.isAnnotation()) {
fFlags= F_IS_ANNOTATION | F_IS_INTERFACE;
} else if (binding.isInterface()) {
fFlags= F_IS_INTERFACE;
} else if (binding.isEnum()) {
fFlags= F_IS_ENUM;
}
if (binding.isTopLevel()) {
fFlags|= F_IS_TOP_LEVEL;
} else if (binding.isNested()) {
fFlags|= F_IS_NESTED;
if (binding.isMember()) {
fFlags|= F_IS_MEMBER;
} else if (binding.isLocal()) {
fFlags|= F_IS_LOCAL;
} else if (binding.isAnonymous()) {
fFlags|= F_IS_ANONYMOUS;
}
}
}
public static boolean isArrayCompatible(ITypeBinding definedType) {
if (definedType.isTopLevel()) {
if (definedType.isClass()) {
return "Object".equals(definedType.getName()) && "java.lang".equals(definedType.getPackage().getName()); //$NON-NLS-1$//$NON-NLS-2$
} else {
String qualifiedName= definedType.getQualifiedName();
return "java.io.Serializable".equals(qualifiedName) || "java.lang.Cloneable".equals(qualifiedName); //$NON-NLS-1$ //$NON-NLS-2$
}
}
return false;
}
private boolean needsImport(ITypeBinding typeBinding, SimpleName ref) {
if (!typeBinding.isTopLevel() && !typeBinding.isMember() || typeBinding.isRecovered()) {
return false; // no imports for anonymous, local, primitive types or parameters types
}
int modifiers= typeBinding.getModifiers();
if (Modifier.isPrivate(modifiers)) {
return false; // imports for privates are not required
}
ITypeBinding currTypeBinding= Bindings.getBindingOfParentType(ref);
if (currTypeBinding == null) {
if (ASTNodes.getParent(ref, ASTNode.PACKAGE_DECLARATION) != null) {
return true; // reference in package-info.java
}
return false; // not in a type
}
if (!Modifier.isPublic(modifiers)) {
if (!currTypeBinding.getPackage().getName().equals(typeBinding.getPackage().getName())) {
return false; // not visible
}
}
ASTNode parent= ref.getParent();
while (parent instanceof Type) {
parent= parent.getParent();
}
if (parent instanceof AbstractTypeDeclaration && parent.getParent() instanceof CompilationUnit) {
return true;
}
if (typeBinding.isMember()) {
if (fAnalyzer.isDeclaredInScope(typeBinding, ref, ScopeAnalyzer.TYPES | ScopeAnalyzer.CHECK_VISIBILITY))
return false;
}
return true;
}
public static boolean isJavaLangObject(ITypeBinding definedType) {
return definedType.isTopLevel() && definedType.isClass() && "Object".equals(definedType.getName()) && "java.lang".equals(definedType.getPackage().getName()); //$NON-NLS-1$//$NON-NLS-2$
}