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

The following examples show how to use org.eclipse.jdt.core.dom.ITypeBinding#getSuperclass() . 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: JavaTypeHierarchyExtractor.java    From tassal with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void getTypeBindingParents(ITypeBinding binding) {
	if (binding.isParameterizedType()) {
		binding = binding.getErasure();
	}
	final String bindingName = binding.isRecovered() ? binding
			.getName() : binding.getQualifiedName();
	final ITypeBinding superclassBinding = binding.getSuperclass();
	if (superclassBinding != null) {
		addTypes(
				superclassBinding.isRecovered() ? superclassBinding.getName()
						: superclassBinding.getQualifiedName(),
				bindingName);
		getTypeBindingParents(superclassBinding);
	}

	for (ITypeBinding iface : binding.getInterfaces()) {
		if (iface.isParameterizedType()) {
			iface = iface.getErasure();
		}
		addTypes(
				iface.isRecovered() ? iface.getName()
						: iface.getQualifiedName(), bindingName);
		getTypeBindingParents(iface);
	}
}
 
Example 2
Source File: FlowContext.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
boolean isExceptionCaught(ITypeBinding excpetionType) {
	for (Iterator<List<CatchClause>> exceptions= fExceptionStack.iterator(); exceptions.hasNext(); ) {
		for (Iterator<CatchClause> catchClauses= exceptions.next().iterator(); catchClauses.hasNext(); ) {
			SingleVariableDeclaration caughtException= catchClauses.next().getException();
			IVariableBinding binding= caughtException.resolveBinding();
			if (binding == null) {
				continue;
			}
			ITypeBinding caughtype= binding.getType();
			while (caughtype != null) {
				if (caughtype == excpetionType) {
					return true;
				}
				caughtype= caughtype.getSuperclass();
			}
		}
	}
	return false;
}
 
Example 3
Source File: PDGNode.java    From JDeodorant with MIT License 6 votes vote down vote up
public boolean instantiatesLocalVariable(AbstractVariable variable) {
	if(variable instanceof PlainVariable && this.definesLocalVariable(variable)) {
		PlainVariable plainVariable = (PlainVariable)variable;
		String variableType = plainVariable.getVariableType();
		for(CreationObject creation : createdTypes) {
			if(creation instanceof ClassInstanceCreationObject) {
				ITypeBinding createdTypeBinding = ((ClassInstanceCreationObject)creation).getClassInstanceCreation().resolveTypeBinding();
				String superclassName = createdTypeBinding.getSuperclass() != null ? createdTypeBinding.getSuperclass().getQualifiedName() : null;
				Set<String> implementedInterfaces = new LinkedHashSet<String>();
				for(ITypeBinding implementedInterface : createdTypeBinding.getInterfaces()) {
					implementedInterfaces.add(implementedInterface.getQualifiedName());
				}
				if(variableType.equals(createdTypeBinding.getQualifiedName()) || variableType.equals(superclassName) || implementedInterfaces.contains(variableType))
					return true;
			}
		}
	}
	return false;
}
 
Example 4
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 6 votes vote down vote up
private Set<IVariableBinding> getDeclaredFields(ITypeBinding typeBinding) {
	Set<IVariableBinding> declaredFields = new LinkedHashSet<IVariableBinding>();
	//first add the directly declared methods
	for(IVariableBinding variableBinding : typeBinding.getDeclaredFields()) {
		declaredFields.add(variableBinding);
	}
	ITypeBinding superclassTypeBinding = typeBinding.getSuperclass();
	if(superclassTypeBinding != null) {
		declaredFields.addAll(getDeclaredFields(superclassTypeBinding));
	}
	ITypeBinding[] interfaces = typeBinding.getInterfaces();
	for(ITypeBinding interfaceTypeBinding : interfaces) {
		declaredFields.addAll(getDeclaredFields(interfaceTypeBinding));
	}
	return declaredFields;
}
 
Example 5
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Finds the method specified by <code>methodName</code> and </code>parameters</code> in
 * the type hierarchy denoted by the given type. Returns <code>null</code> if no such method
 * exists. If the method is defined in more than one super type only the first match is
 * returned. First the super class is examined and then the implemented interfaces.
 * 
 * @param type The type to search the method in
 * @param methodName The name of the method to find
 * @param parameters The parameter types of the method to find. If <code>null</code> is passed, only the name is matched and parameters are ignored.
 * @return the method binding representing the method
 */
public static IMethodBinding findMethodInHierarchy(ITypeBinding type, String methodName, ITypeBinding[] parameters) {
	IMethodBinding method= findMethodInType(type, methodName, parameters);
	if (method != null)
		return method;
	ITypeBinding superClass= type.getSuperclass();
	if (superClass != null) {
		method= findMethodInHierarchy(superClass, methodName, parameters);
		if (method != null)
			return method;
	}
	ITypeBinding[] interfaces= type.getInterfaces();
	for (int i= 0; i < interfaces.length; i++) {
		method= findMethodInHierarchy(interfaces[i], methodName, parameters);
		if (method != null)
			return method;
	}
	return null;
}
 
Example 6
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 6 votes vote down vote up
private static Set<ITypeBinding> getAllSuperTypesUpToCommonSuperclass(ITypeBinding typeBinding, ITypeBinding commonSuperclass) {
	Set<ITypeBinding> superTypes = new LinkedHashSet<ITypeBinding>();
	ITypeBinding superTypeBinding = typeBinding.getSuperclass();
	if(superTypeBinding != null && !superTypeBinding.isEqualTo(commonSuperclass)) {
		superTypes.add(superTypeBinding);
		superTypes.addAll(getAllSuperTypesUpToCommonSuperclass(superTypeBinding, commonSuperclass));
	}
	ITypeBinding[] superInterfaces = typeBinding.getInterfaces();
	for(ITypeBinding superInterface : superInterfaces) {
		if(!superInterface.isEqualTo(commonSuperclass)) {
			superTypes.add(superInterface);
			superTypes.addAll(getAllSuperTypesUpToCommonSuperclass(superInterface, commonSuperclass));
		}
	}
	return superTypes;
}
 
Example 7
Source File: JdtUtils.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private static Set<IMethodBinding> getOverriddenMethodsInType(
    IMethodBinding methodBinding, ITypeBinding typeBinding) {
  Set<IMethodBinding> overriddenMethods = new HashSet<>();
  for (IMethodBinding declaredMethod : typeBinding.getDeclaredMethods()) {
    if (methodBinding.overrides(declaredMethod) && !methodBinding.isConstructor()) {
      checkArgument(!Modifier.isStatic(methodBinding.getModifiers()));
      overriddenMethods.add(declaredMethod);
    }
  }
  // Recurse into immediate super class and interfaces for overridden method.
  if (typeBinding.getSuperclass() != null) {
    overriddenMethods.addAll(
        getOverriddenMethodsInType(methodBinding, typeBinding.getSuperclass()));
  }
  for (ITypeBinding interfaceBinding : typeBinding.getInterfaces()) {
    overriddenMethods.addAll(getOverriddenMethodsInType(methodBinding, interfaceBinding));
  }

  ITypeBinding javaLangObjectTypeBinding = JdtUtils.javaLangObjectTypeBinding.get();
  if (typeBinding != javaLangObjectTypeBinding) {
    for (IMethodBinding objectMethodBinding : javaLangObjectTypeBinding.getDeclaredMethods()) {
      if (!isPolymorphic(objectMethodBinding)) {
        continue;
      }
      checkState(!getVisibility(objectMethodBinding).isPackagePrivate());
      if (methodBinding.isSubsignature(objectMethodBinding)) {
        overriddenMethods.add(objectMethodBinding);
      }
    }
  }

  return overriddenMethods;
}
 
Example 8
Source File: StubUtility2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static void getDelegatableMethods(List<IMethodBinding> methods, IVariableBinding fieldBinding, ITypeBinding typeBinding, ITypeBinding binding, List<DelegateEntry> result) {
	boolean match= false;
	if (typeBinding.isTypeVariable()) {
		ITypeBinding[] typeBounds= typeBinding.getTypeBounds();
		if (typeBounds.length > 0) {
			for (int i= 0; i < typeBounds.length; i++) {
				getDelegatableMethods(methods, fieldBinding, typeBounds[i], binding, result);
			}
		} else {
			ITypeBinding objectBinding= Bindings.findTypeInHierarchy(binding, "java.lang.Object"); //$NON-NLS-1$
			if (objectBinding != null) {
				getDelegatableMethods(methods, fieldBinding, objectBinding, binding, result);
			}
		}
	} else {
		IMethodBinding[] candidates= getDelegateCandidates(typeBinding, binding);
		for (int index= 0; index < candidates.length; index++) {
			match= false;
			final IMethodBinding methodBinding= candidates[index];
			for (int offset= 0; offset < methods.size() && !match; offset++) {
				if (Bindings.areOverriddenMethods(methods.get(offset), methodBinding))
					match= true;
			}
			if (!match) {
				result.add(new DelegateEntry(methodBinding, fieldBinding));
				methods.add(methodBinding);
			}
		}
		final ITypeBinding superclass= typeBinding.getSuperclass();
		if (superclass != null)
			getDelegatableMethods(methods, fieldBinding, superclass, binding, result);
		ITypeBinding[] superInterfaces= typeBinding.getInterfaces();
		for (int offset= 0; offset < superInterfaces.length; offset++)
			getDelegatableMethods(methods, fieldBinding, superInterfaces[offset], binding, result);
	}
}
 
Example 9
Source File: DIT.java    From ck with Apache License 2.0 5 votes vote down vote up
private void calculate(ITypeBinding binding) {
	ITypeBinding father = binding.getSuperclass();
	if (father != null) {
		String fatherName = father.getQualifiedName();
		if (fatherName.endsWith("Object")) return;
		dit++;

		calculate(father);
	}

}
 
Example 10
Source File: ConvertAnonymousToNestedRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private ITypeBinding getSuperTypeBinding() {
	ITypeBinding types= fAnonymousInnerClassNode.resolveBinding();
	ITypeBinding[] interfaces= types.getInterfaces();
	if (interfaces.length > 0)
		return interfaces[0];
	else
		return types.getSuperclass();
}
 
Example 11
Source File: UnresolvedElementsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static boolean hasMethodWithName(ITypeBinding typeBinding, String name) {
	IVariableBinding[] fields= typeBinding.getDeclaredFields();
	for (int i= 0; i < fields.length; i++) {
		if (fields[i].getName().equals(name)) {
			return true;
		}
	}
	ITypeBinding superclass= typeBinding.getSuperclass();
	if (superclass != null) {
		return hasMethodWithName(superclass, name);
	}
	return false;
}
 
Example 12
Source File: ChangeTypeContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param type a type
 * @return the direct superclass and direct superinterfaces. Class Object is
 * included in the result if the root of the hierarchy is a top-level
 * interface.
 */
public Set<ITypeBinding> getDirectSuperTypes(ITypeBinding type){
	Set<ITypeBinding> result= new HashSet<ITypeBinding>();
	if (type.getSuperclass() != null){
		result.add(type.getSuperclass());
	}
	ITypeBinding[] interfaces= type.getInterfaces();
	for (int i=0; i < interfaces.length; i++){
		result.add(interfaces[i]);
	}
	if (fGeneralizeType.getOriginalType().isInterface() && type != fGeneralizeType.getObject()){
		result.add(fGeneralizeType.getObject());
	}
	return result;
}
 
Example 13
Source File: LocalCorrectionsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static boolean isSubtype(ITypeBinding curr, ITypeBinding[] addedExceptions) {
	while (curr != null) {
		for (int i = 0; i < addedExceptions.length; i++) {
			if (curr == addedExceptions[i]) {
				return true;
			}
		}
		curr = curr.getSuperclass();
	}
	return false;
}
 
Example 14
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean isSubtype(ITypeBinding curr, ITypeBinding[] addedExceptions) {
	while (curr != null) {
		for (int i= 0; i < addedExceptions.length; i++) {
			if (curr == addedExceptions[i]) {
				return true;
			}
		}
		curr= curr.getSuperclass();
	}
	return false;
}
 
Example 15
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.4
 */
protected void setSuperTypes(ITypeBinding binding, String qualifiedName, JvmDeclaredType result) {
	ITypeBinding superclass = binding.getSuperclass();
	InternalEList<JvmTypeReference> superTypes = (InternalEList<JvmTypeReference>)result.getSuperTypes();
	if (superclass != null) {
		superTypes.addUnique(createTypeReference(superclass));
	} 
	for (ITypeBinding intf : binding.getInterfaces()) {
		superTypes.addUnique(createTypeReference(intf));
	}
	if (superTypes.isEmpty() && !OBJECT_CLASS_NAME.equals(qualifiedName)) {
		superTypes.addUnique(createObjectClassReference());
	}
}
 
Example 16
Source File: JdtUtils.java    From j2cl with Apache License 2.0 5 votes vote down vote up
public static IMethodBinding getMethodBinding(
    ITypeBinding typeBinding, String methodName, ITypeBinding... parameterTypes) {

  Queue<ITypeBinding> deque = new ArrayDeque<>();
  deque.add(typeBinding);

  while (!deque.isEmpty()) {
    typeBinding = deque.poll();
    for (IMethodBinding methodBinding : typeBinding.getDeclaredMethods()) {
      if (methodBinding.getName().equals(methodName)
          && Arrays.equals(methodBinding.getParameterTypes(), parameterTypes)) {
        return methodBinding;
      }
    }
    ITypeBinding superclass = typeBinding.getSuperclass();
    if (superclass != null) {
      deque.add(superclass);
    }

    ITypeBinding[] superInterfaces = typeBinding.getInterfaces();
    if (superInterfaces != null) {
      for (ITypeBinding superInterface : superInterfaces) {
        deque.add(superInterface);
      }
    }
  }
  return null;
}
 
Example 17
Source File: AstVisitor.java    From jdt2famix with Eclipse Public License 1.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public boolean visit(TypeDeclaration node) {
	ITypeBinding binding = node.resolveBinding();
	if (binding == null) {
		logNullBinding("type declaration", node.getName(),
				((CompilationUnit) node.getRoot()).getLineNumber(node.getStartPosition()));
		return false;
	}
	Type type = importer.ensureTypeFromTypeBinding(binding);

	org.eclipse.jdt.core.dom.Type superclassType = node.getSuperclassType();
	/*
	 * This is an ugly patch. When the binding to the superclass or super interfaces
	 * cannot be resolved, we try to recover as much info as possible We do it here
	 * because it is hard to pass around the dom type
	 */
	if (binding.getSuperclass() == null && superclassType != null)
		importer.createInheritanceFromSubtypeToSuperDomType(type, superclassType);

	if (superclassType != null)
		type.getSuperInheritances().stream().filter(inheritance -> (inheritance.getSuperclass() instanceof Class
				&& !((Class) inheritance.getSuperclass()).getIsInterface())
				|| (inheritance.getSuperclass() instanceof ParameterizedType
						&& ((ParameterizedType) inheritance.getSuperclass()).getParameterizableClass() != null
						&& !((ParameterizedType) inheritance.getSuperclass()).getParameterizableClass()
								.getIsInterface()))
				.findFirst().ifPresent(in -> importer.createLightweightSourceAnchor(in, superclassType));

	if (binding.getInterfaces().length == 0 && !node.superInterfaceTypes().isEmpty())
		node.superInterfaceTypes().stream().forEach(t -> {
			importer.createInheritanceFromSubtypeToSuperDomType(type, (org.eclipse.jdt.core.dom.Type) t);
		});

	// create source anchors for implemented interfaces references
	createSourceAnchorsForInterfaceInheritance(node, type);

	type.setIsStub(false);
	importer.createSourceAnchor(type, node);
	importer.createLightweightSourceAnchor(type, node.getName());
	importer.ensureCommentFromBodyDeclaration(type, node);
	importer.pushOnContainerStack(type);
	return true;
}
 
Example 18
Source File: MethodsSourcePositionComparator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public int compare(IMethodBinding firstMethodBinding, IMethodBinding secondMethodBinding) {
	if (firstMethodBinding == null || secondMethodBinding == null) {
		return 0;
	}
	ITypeBinding firstMethodType= firstMethodBinding.getDeclaringClass();
	ITypeBinding secondMethodType= secondMethodBinding.getDeclaringClass();

	if (firstMethodType.equals(secondMethodType)) {
		return compareInTheSameType(firstMethodBinding, secondMethodBinding);
	}

	if (firstMethodType.equals(fTypeBinding)) {
		return 1;
	}
	if (secondMethodType.equals(fTypeBinding)) {
		return -1;
	}

	ITypeBinding type= fTypeBinding;
	int count= 0, firstCount= -1, secondCount= -1;
	while ((type= type.getSuperclass()) != null) {
		if (firstMethodType.equals(type)) {
			firstCount= count;
		}
		if (secondMethodType.equals(type)) {
			secondCount= count;
		}
		count++;
	}
	if (firstCount != -1 && secondCount != -1) {
		return (firstCount - secondCount);
	}
	if (firstCount != -1 && secondCount == -1) {
		return 1;
	}
	if (firstCount == -1 && secondCount != -1) {
		return -1;
	}

	ITypeBinding[] interfaces= fTypeBinding.getInterfaces();
	for (int i= 0; i < interfaces.length; i++) {
		if (firstMethodType.equals(interfaces[i])) {
			return 1;
		}
		if (secondMethodType.equals(interfaces[i])) {
			return -1;
		}
	}
	return 0;
}
 
Example 19
Source File: ElementTypeTeller.java    From txtUML with Eclipse Public License 1.0 4 votes vote down vote up
public static boolean hasSuperClass(ITypeBinding type, String superClassName) {
	while (type != null && !type.getErasure().getQualifiedName().equals(superClassName)) {
		type = type.getSuperclass();
	}
	return type != null;
}
 
Example 20
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Method to visit a super class hierarchy defined by a given type.
 * The given type itself is not visited.
 *
 * @param type the type whose super class hierarchy is to be visited
 * @param visitor the visitor
 * @return <code>true</code> if all types were visited,
 *         or <code>false</code> if the visiting got aborted because the <code>visit</code>
 *         method returned <code>false</code> for a type
 */
public static boolean visitSuperclasses(ITypeBinding type, TypeBindingVisitor visitor) {
	while ((type= type.getSuperclass()) != null) {
		if (!visitor.visit(type)) {
			return false;
		}
	}
	return true;
}