Java Code Examples for org.eclipse.jdt.core.dom.ITypeBinding#isClass()

The following examples show how to use org.eclipse.jdt.core.dom.ITypeBinding#isClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 6 votes vote down vote up
private boolean infeasibleRefactoring(ITypeBinding commonSuperTypeOfSourceTypeDeclarations, ITypeBinding typeBinding1, ITypeBinding typeBinding2) {
	if(commonSuperTypeOfSourceTypeDeclarations.isInterface()) {
		//common super type is an interface and at least one of the subclasses does not have java.lang.Object as a superclass
		return !typeBinding1.getSuperclass().getQualifiedName().equals("java.lang.Object") ||
				!typeBinding2.getSuperclass().getQualifiedName().equals("java.lang.Object") ||
		//common super type is a tagging interface and both subclasses have java.lang.Object as a superclass
				(ASTNodeMatcher.isTaggingInterface(commonSuperTypeOfSourceTypeDeclarations) &&
				typeBinding1.getSuperclass().getQualifiedName().equals("java.lang.Object") &&
				typeBinding2.getSuperclass().getQualifiedName().equals("java.lang.Object") &&
				!allAccessedLocalMethodsHaveMatchingSignatures());
	}
	else if(commonSuperTypeOfSourceTypeDeclarations.isClass()) {
		//common super type is a class and at least one of the subclasses does not have the common super type as a direct superclass
		return !typeBinding1.getSuperclass().isEqualTo(commonSuperTypeOfSourceTypeDeclarations) ||
				!typeBinding2.getSuperclass().isEqualTo(commonSuperTypeOfSourceTypeDeclarations);
	}
	return false;
}
 
Example 2
Source File: TypeURIHelper.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void createFragment(ITypeBinding typeBinding, StringBuilder uriBuilder) {
	if (typeBinding.isPrimitive()) {
		createFragmentForPrimitive(typeBinding, uriBuilder);
		return;
	}
	if (typeBinding.isArray()) {
		createFragmentForArray(typeBinding, uriBuilder);
		return;
	}
	if (typeBinding.isTypeVariable()) {
		createFragmentForTypeVariable(typeBinding, uriBuilder);
		return;
	}
	if (typeBinding.isAnnotation() || typeBinding.isClass() || typeBinding.isInterface() || typeBinding.isEnum()) {
		createFragmentForClass(typeBinding, uriBuilder);
		return;
	}
	throw new IllegalStateException("Unexpected type binding: " + typeBinding);
}
 
Example 3
Source File: TypeURIHelper.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void createResourceURI(ITypeBinding typeBinding, StringBuilder uriBuilder) {
	if (typeBinding.isPrimitive()) {
		createResourceURIForPrimitive(uriBuilder);
		return;
	}
	if (typeBinding.isClass() || typeBinding.isInterface() || typeBinding.isAnnotation() || typeBinding.isEnum()) {
		createResourceURIForClass(typeBinding, uriBuilder);
		return;
	}
	if (typeBinding.isArray()) {
		createResourceURIForArray(typeBinding, uriBuilder);
		return;
	}
	if (typeBinding.isTypeVariable()) {
		createResourceURIForTypeVariable(typeBinding, uriBuilder);
		return;
	}
	throw new IllegalStateException("Unexpected type: " + typeBinding);
}
 
Example 4
Source File: MoveInstanceMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public final boolean visit(final QualifiedName node) {
	Assert.isNotNull(node);
	IBinding binding= node.resolveBinding();
	if (binding instanceof ITypeBinding) {
		final ITypeBinding type= (ITypeBinding) binding;
		if (type.isClass() && type.getDeclaringClass() != null) {
			final Type newType= fTargetRewrite.getImportRewrite().addImport(type, node.getAST());
			fRewrite.replace(node, newType, null);
			return false;
		}
	}
	binding= node.getQualifier().resolveBinding();
	if (Bindings.equals(fTarget, binding)) {
		fRewrite.replace(node, getFieldReference(node.getName(), fRewrite), null);
		return false;
	}
	node.getQualifier().accept(this);
	return false;
}
 
Example 5
Source File: BindingLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static ImageDescriptor getTypeImageDescriptor(boolean inner, ITypeBinding binding, int flags) {
	if (binding.isEnum())
		return JavaPluginImages.DESC_OBJS_ENUM;
	else if (binding.isAnnotation())
		return JavaPluginImages.DESC_OBJS_ANNOTATION;
	else if (binding.isInterface()) {
		if ((flags & JavaElementImageProvider.LIGHT_TYPE_ICONS) != 0)
			return JavaPluginImages.DESC_OBJS_INTERFACEALT;
		if (inner)
			return getInnerInterfaceImageDescriptor(binding.getModifiers());
		return getInterfaceImageDescriptor(binding.getModifiers());
	} else if (binding.isClass()) {
		if ((flags & JavaElementImageProvider.LIGHT_TYPE_ICONS) != 0)
			return JavaPluginImages.DESC_OBJS_CLASSALT;
		if (inner)
			return getInnerClassImageDescriptor(binding.getModifiers());
		return getClassImageDescriptor(binding.getModifiers());
	} else if (binding.isTypeVariable()) {
		return JavaPluginImages.DESC_OBJS_TYPEVARIABLE;
	}
	// primitive type, wildcard
	return null;
}
 
Example 6
Source File: NewCUUsingWizardProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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);
			}
		}
	}
}
 
Example 7
Source File: SemanticHighlightings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean consumes(SemanticToken token) {

	// 1: match types
	SimpleName name= token.getNode();
	ASTNode node= name.getParent();
	int nodeType= node.getNodeType();
	if (nodeType != ASTNode.SIMPLE_TYPE && nodeType != ASTNode.THIS_EXPRESSION && nodeType != ASTNode.QUALIFIED_TYPE  && nodeType != ASTNode.QUALIFIED_NAME && nodeType != ASTNode.TYPE_DECLARATION && nodeType != ASTNode.METHOD_INVOCATION)
		return false;
	while (nodeType == ASTNode.QUALIFIED_NAME) {
		node= node.getParent();
		nodeType= node.getNodeType();
		if (nodeType == ASTNode.IMPORT_DECLARATION)
			return false;
	}

	// 2: match classes
	IBinding binding= token.getBinding();
	if (binding instanceof ITypeBinding) {
		ITypeBinding typeBinding= (ITypeBinding) binding;
		// see also ClassHighlighting
		return typeBinding.isClass() && (typeBinding.getModifiers() & Modifier.ABSTRACT) != 0;
	}

	return false;
}
 
Example 8
Source File: JdtUtils.java    From j2cl with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if instances of this type capture its outer instances; i.e. if it is an non static
 * member class, or an anonymous or local class defined in an instance context.
 */
public static boolean capturesEnclosingInstance(ITypeBinding typeBinding) {
  if (!typeBinding.isClass() || !typeBinding.isNested()) {
    // Only non-top level classes (excludes Enums, Interfaces etc.) can capture outer instances.
    return false;
  }

  if (typeBinding.isLocal()) {
    // Local types (which include anonymous classes in JDT) are static only if they are declared
    // in a static context; i.e. if the member where they are declared is static.
    return !isStatic(getDeclaringMethodOrFieldBinding(typeBinding));
  } else {
    checkArgument(typeBinding.isMember());
    // Member classes must be marked explicitly static.
    return !isStatic(typeBinding);
  }
}
 
Example 9
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the unboxed type binding according to JLS3 5.1.7, or the original binding if
 * the given type is not a boxed type.
 *
 * @param type a type binding
 * @param ast an AST to resolve the unboxed type
 * @return the unboxed type, or the original type if no unboxed type found
 */
public static ITypeBinding getUnboxedTypeBinding(ITypeBinding type, AST ast) {
	if (!type.isClass())
		return type;
	String unboxedTypeName= getUnboxedTypeName(type.getQualifiedName());
	if (unboxedTypeName == null)
		return type;
	ITypeBinding unboxed= ast.resolveWellKnownType(unboxedTypeName);
	if (unboxed == null)
		return type;
	return unboxed;
}
 
Example 10
Source File: SrcTreeGenerator.java    From sahagin-java with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visit(MethodDeclaration node) {
    IMethodBinding methodBinding = node.resolveBinding();
    Pair<String, CaptureStyle> testDocPair = testDocIfSubMethod(methodBinding);
    if (testDocPair.getLeft() == null) {
        return super.visit(node);
    }

    ITypeBinding classBinding = methodBinding.getDeclaringClass();
    if (!classBinding.isClass() && !classBinding.isInterface()) {
        // enum method, etc
        return super.visit(node);
    }

    TestClass testClass = classBindingTestClass(classBinding);

    TestMethod testMethod = new TestMethod();
    testMethod.setKey(generateMethodKey(methodBinding, false));
    testMethod.setSimpleName(methodBinding.getName());
    testMethod.setTestDoc(testDocPair.getLeft());
    testMethod.setCaptureStyle(testDocPair.getRight());
    for (Object element : node.parameters()) {
        if (!(element instanceof SingleVariableDeclaration)) {
            throw new RuntimeException("not supported yet: " + element);
        }
        SingleVariableDeclaration varDecl = (SingleVariableDeclaration)element;
        testMethod.addArgVariable(varDecl.getName().getIdentifier());
        if (varDecl.isVarargs()) {
            testMethod.setVariableLengthArgIndex(testMethod.getArgVariables().size() - 1);
        }
    }
    testMethod.setTestClassKey(testClass.getKey());
    testMethod.setTestClass(testClass);
    subMethodTable.addTestMethod(testMethod);

    testClass.addTestMethodKey(testMethod.getKey());
    testClass.addTestMethod(testMethod);

    return super.visit(node);
}
 
Example 11
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean pullUpToCommonSuperclass(ITypeBinding commonSuperTypeOfSourceTypeDeclarations, ITypeBinding typeBinding1, ITypeBinding typeBinding2) {
	return ASTReader.getSystemObject().getClassObject(commonSuperTypeOfSourceTypeDeclarations.getQualifiedName()) != null &&
			commonSuperTypeOfSourceTypeDeclarations.isClass() &&
			(cloneFragmentsDoNotAccessFieldsOrMethods() ||
			superclassInheritedOnlyByRefactoredSubclasses(commonSuperTypeOfSourceTypeDeclarations, typeBinding1, typeBinding2) ||
			superclassIsOneOfRefactoredSubclasses(commonSuperTypeOfSourceTypeDeclarations, typeBinding1, typeBinding2) ||
			!superclassDirectlyInheritedFromRefactoredSubclasses(commonSuperTypeOfSourceTypeDeclarations, typeBinding1, typeBinding2));
}
 
Example 12
Source File: TypeRules.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: TType.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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;
		}
	}
}
 
Example 14
Source File: MoveInstanceMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Computes the target categories for the method to move.
 *
 * @param declaration
 *            the method declaration
 * @return the possible targets as variable bindings of read-only fields and
 *         parameters
 */
protected IVariableBinding[] computeTargetCategories(final MethodDeclaration declaration) {
	Assert.isNotNull(declaration);
	if (fPossibleTargets.length == 0 || fCandidateTargets.length == 0) {
		final List<IVariableBinding> possibleTargets= new ArrayList<IVariableBinding>(16);
		final List<IVariableBinding> candidateTargets= new ArrayList<IVariableBinding>(16);
		final IMethodBinding method= declaration.resolveBinding();
		if (method != null) {
			final ITypeBinding declaring= method.getDeclaringClass();
			IVariableBinding[] bindings= getArgumentBindings(declaration);
			ITypeBinding binding= null;
			for (int index= 0; index < bindings.length; index++) {
				binding= bindings[index].getType();
				if ((binding.isClass() || binding.isEnum() || is18OrHigherInterface(binding)) && binding.isFromSource()) {
					possibleTargets.add(bindings[index]);
					candidateTargets.add(bindings[index]);
				}
			}
			final ReadyOnlyFieldFinder visitor= new ReadyOnlyFieldFinder(declaring);
			declaration.accept(visitor);
			bindings= visitor.getReadOnlyFields();
			for (int index= 0; index < bindings.length; index++) {
				binding= bindings[index].getType();
				if ((binding.isClass() || is18OrHigherInterface(binding)) && binding.isFromSource())
					possibleTargets.add(bindings[index]);
			}
			bindings= visitor.getDeclaredFields();
			for (int index= 0; index < bindings.length; index++) {
				binding= bindings[index].getType();
				if ((binding.isClass() || is18OrHigherInterface(binding)) && binding.isFromSource())
					candidateTargets.add(bindings[index]);
			}
		}
		fPossibleTargets= new IVariableBinding[possibleTargets.size()];
		possibleTargets.toArray(fPossibleTargets);
		fCandidateTargets= new IVariableBinding[candidateTargets.size()];
		candidateTargets.toArray(fCandidateTargets);
	}
	return fPossibleTargets;
}
 
Example 15
Source File: ExtractSupertypeProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates the necessary constructors for the extracted supertype.
 *
 * @param targetRewrite
 *            the target compilation unit rewrite
 * @param superType
 *            the super type, or <code>null</code> if no super type (ie.
 *            <code>java.lang.Object</code>) is available
 * @param targetDeclaration
 *            the type declaration of the target type
 * @param status
 *            the refactoring status
 */
protected final void createNecessaryConstructors(final CompilationUnitRewrite targetRewrite, final IType superType, final AbstractTypeDeclaration targetDeclaration, final RefactoringStatus status) {
	Assert.isNotNull(targetRewrite);
	Assert.isNotNull(targetDeclaration);
	if (superType != null) {
		final ITypeBinding binding= targetDeclaration.resolveBinding();
		if (binding != null && binding.isClass()) {
			final IMethodBinding[] bindings= StubUtility2.getVisibleConstructors(binding, true, true);
			int deprecationCount= 0;
			for (int i= 0; i < bindings.length; i++) {
				if (bindings[i].isDeprecated())
					deprecationCount++;
			}
			final ListRewrite rewrite= targetRewrite.getASTRewrite().getListRewrite(targetDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
			if (rewrite != null) {
				boolean createDeprecated= deprecationCount == bindings.length;
				for (int i= 0; i < bindings.length; i++) {
					IMethodBinding curr= bindings[i];
					if (!curr.isDeprecated() || createDeprecated) {
						MethodDeclaration stub;
						try {
							ImportRewriteContext context= new ContextSensitiveImportRewriteContext(targetDeclaration, targetRewrite.getImportRewrite());
							stub= StubUtility2.createConstructorStub(targetRewrite.getCu(), targetRewrite.getASTRewrite(), targetRewrite.getImportRewrite(), context, curr, binding.getName(),
									Modifier.PUBLIC, false, false, fSettings);
							if (stub != null)
								rewrite.insertLast(stub, null);
						} catch (CoreException exception) {
							JavaPlugin.log(exception);
							status.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractSupertypeProcessor_unexpected_exception_on_layer));
						}
					}
				}
			}
		}
	}
}
 
Example 16
Source File: SemanticHighlightings.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean consumes(SemanticToken token) {

	// 1: match types
	SimpleName name = token.getNode();
	ASTNode node = name.getParent();
	int nodeType = node.getNodeType();
	if (nodeType != ASTNode.SIMPLE_TYPE && nodeType != ASTNode.THIS_EXPRESSION && nodeType != ASTNode.QUALIFIED_TYPE && nodeType != ASTNode.QUALIFIED_NAME && nodeType != ASTNode.TYPE_DECLARATION
			&& nodeType != ASTNode.METHOD_INVOCATION) {
		return false;
	}
	while (nodeType == ASTNode.QUALIFIED_NAME) {
		node = node.getParent();
		nodeType = node.getNodeType();
		if (nodeType == ASTNode.IMPORT_DECLARATION) {
			return false;
		}
	}

	// 2: match classes
	IBinding binding = token.getBinding();
	if (binding instanceof ITypeBinding) {
		ITypeBinding typeBinding = (ITypeBinding) binding;
		// see also ClassHighlighting
		return typeBinding.isClass() && (typeBinding.getModifiers() & Modifier.ABSTRACT) != 0;
	}

	return false;
}
 
Example 17
Source File: JdtUtils.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private static Kind getKindFromTypeBinding(ITypeBinding typeBinding) {
  if (typeBinding.isEnum() && !typeBinding.isAnonymous()) {
    // Do not consider the anonymous classes that constitute enum values as Enums, only the
    // enum "class" itself is considered Kind.ENUM.
    return Kind.ENUM;
  } else if (typeBinding.isClass() || (typeBinding.isEnum() && typeBinding.isAnonymous())) {
    return Kind.CLASS;
  } else if (typeBinding.isInterface()) {
    return Kind.INTERFACE;
  }
  throw new InternalCompilerError("Type binding %s not handled", typeBinding);
}
 
Example 18
Source File: TypeRules.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
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$
}
 
Example 19
Source File: FullConstraintCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean isClassBinding(ITypeBinding typeBinding){
	return typeBinding != null && typeBinding.isClass();
}
 
Example 20
Source File: NewCUUsingWizardProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
	StringBuffer buf= new StringBuffer();
	switch (fTypeKind) {
		case K_CLASS:
			buf.append(CorrectionMessages.NewCUCompletionUsingWizardProposal_createclass_info);
			break;
		case K_INTERFACE:
			buf.append(CorrectionMessages.NewCUCompletionUsingWizardProposal_createinterface_info);
			break;
		case K_ENUM:
			buf.append(CorrectionMessages.NewCUCompletionUsingWizardProposal_createenum_info);
			break;
		case K_ANNOTATION:
			buf.append(CorrectionMessages.NewCUCompletionUsingWizardProposal_createannotation_info);
			break;
	}
	buf.append("<br>"); //$NON-NLS-1$
	buf.append("<br>"); //$NON-NLS-1$
	if (fTypeContainer instanceof IType) {
		buf.append(CorrectionMessages.NewCUCompletionUsingWizardProposal_tooltip_enclosingtype);
	} else {
		buf.append(CorrectionMessages.NewCUCompletionUsingWizardProposal_tooltip_package);
	}
	buf.append(" <b>"); //$NON-NLS-1$
	buf.append(JavaElementLabels.getElementLabel(fTypeContainer, JavaElementLabels.T_FULLY_QUALIFIED));
	buf.append("</b><br>"); //$NON-NLS-1$
	buf.append("public "); //$NON-NLS-1$


	switch (fTypeKind) {
		case K_CLASS:
			buf.append("class <b>"); //$NON-NLS-1$
			break;
		case K_INTERFACE:
			buf.append("interface <b>"); //$NON-NLS-1$
			break;
		case K_ENUM:
			buf.append("enum <b>"); //$NON-NLS-1$
			break;
		case K_ANNOTATION:
			buf.append("@interface <b>"); //$NON-NLS-1$
			break;
	}
	nameToHTML(fTypeNameWithParameters, buf);

	ITypeBinding superclass= getPossibleSuperTypeBinding(fNode);
	if (superclass != null) {
		if (superclass.isClass()) {
			if (fTypeKind == K_CLASS) {
				buf.append("</b> extends <b>"); //$NON-NLS-1$
				nameToHTML(BindingLabelProvider.getBindingLabel(superclass, BindingLabelProvider.DEFAULT_TEXTFLAGS), buf);
			}
		} else {
			if (fTypeKind == K_INTERFACE) {
				buf.append("</b> extends <b>"); //$NON-NLS-1$
			} else {
				buf.append("</b> implements <b>"); //$NON-NLS-1$
			}
			nameToHTML(BindingLabelProvider.getBindingLabel(superclass, BindingLabelProvider.DEFAULT_TEXTFLAGS), buf);
		}
	}
	buf.append("</b> {<br>}<br>"); //$NON-NLS-1$
	return buf.toString();
}