public static String bestNameableType(ITypeBinding type) { { String result_ = fqTypeName(type); if (result_ != null) return removeTypeParam(result_); } if (type.isCapture() || type.isWildcardType() || type.isTypeVariable()) { return bestNameableType(type.getErasure()); } // Find an interface or superclass being implemented. // For anonymous class, there can only be one. BUT for // local class, things are super f-ed up... if (type.isAnonymous()) { // any interfaces? if (type.getInterfaces().length > 0) { String result = fqTypeName(type.getInterfaces()[0]); assert (result != null); return removeTypeParam(result); } else { String result = fqTypeName(type.getSuperclass()); assert (result != null); return removeTypeParam(result); } } if (type.isLocal()) { // We can only make sense of this is there is just one // superclass+inferface. if (type.getInterfaces().length == 0) { // EASY! String result = fqTypeName(type.getSuperclass()); assert (result != null); return removeTypeParam(result); } else { // Well, maybe superclass is object? if (type.getSuperclass().getQualifiedName().equals(Object.class.getName())) { String result = fqTypeName(type.getInterfaces()[0]); assert (result != null); return removeTypeParam(result); } else { // Otherwise, return the superclass, I guess, but print a warning: String result = fqTypeName(type.getSuperclass()); assert (result != null); System.err.println("Local class has multiple supertypes..." + type.getBinaryName()); return removeTypeParam(result); } } } return Utilities.impossible(); }
/** * Ensures that creating a DOM AST and computing the bindings takes the owner's working copies * into account. (regression test for bug 39533 Working copy with no corresponding file not * considered by NameLookup) * * @deprecated using deprecated code */ public void testParseCompilationUnit1() throws CoreException { ICompilationUnit workingCopy1 = null; ICompilationUnit workingCopy2 = null; try { TestWorkingCopyOwner owner = new TestWorkingCopyOwner(); workingCopy1 = getCompilationUnit("P/X.java").getWorkingCopy(owner, null); workingCopy1.getBuffer().setContents("public class X implements I {\n" + "}"); workingCopy1.makeConsistent(null); workingCopy2 = getCompilationUnit("P/I.java").getWorkingCopy(owner, null); workingCopy2.getBuffer().setContents("public interface I {\n" + "}"); workingCopy2.makeConsistent(null); ASTParser parser = ASTParser.newParser(AST.JLS2); parser.setSource(workingCopy1); parser.setResolveBindings(true); parser.setWorkingCopyOwner(owner); CompilationUnit cu = (CompilationUnit) parser.createAST(null); List types = cu.types(); assertEquals("Unexpected number of types in AST", 1, types.size()); TypeDeclaration type = (TypeDeclaration) types.get(0); ITypeBinding typeBinding = type.resolveBinding(); assertTypeBindingsEqual("Unexpected interfaces", "I", typeBinding.getInterfaces()); } finally { if (workingCopy1 != null) { workingCopy1.discardWorkingCopy(); } if (workingCopy2 != null) { workingCopy2.discardWorkingCopy(); } } }
private boolean initializeAndCollectInhData( IMethodBinding binding, List<MethodPathItem> inhMethods) { if (binding == null) return false; // check if method is abstract if (Modifier.isAbstract(binding.getModifiers())) return false; ITypeBinding declTypeBinding = binding.getDeclaringClass(); // check only for classes if (declTypeBinding == null || !declTypeBinding.isClass()) return false; Set<String> checkedInterfaces = new HashSet<String>(); // (recursively) collects all keys of methods in abstract classes which // belongs to this declaration OverridingRelationUtils.collectSimilarMethodKeysInSuperClasses( binding, declTypeBinding.getSuperclass(), inhMethods, checkedInterfaces); // (recursively) collects all keys of methods in interfaces which // belongs to this declaration OverridingRelationUtils.collectSimilarMethodKeysInInterfaces( binding, declTypeBinding.getInterfaces(), inhMethods, checkedInterfaces); // the set should contain at least one inherited method if (inhMethods.size() == 0) return false; return true; }
private static boolean typeImplementsInterfaceDirectly(ITypeBinding type, String interfaceName) { for (ITypeBinding implementedInterface : type.getInterfaces()) { if (interfaceName.equals(implementedInterface.getErasure().getQualifiedName())) { return true; } } return false; }
private boolean entryMatchesRenamedMethod( File javaFile, Multimap<String, String> invokers, IMethodBinding methodBinding, String entry) { try { String[] columns = entry.split(";"); String classNamespace = columns[1]; String renamedMethod = columns[2]; String methodBindingClassNamespace = (methodBinding.getKey().split("\\."))[0]; if (simplifyMethodSignature(methodBinding).contains(renamedMethod)) { if (classesNamesMatches(methodBindingClassNamespace, classNamespace)) { return true; } else { /*the method belongs to a superclass or interface*/ ITypeBinding declaringClass = methodBinding.getDeclaringClass(); ITypeBinding superClass = declaringClass.getSuperclass(); ITypeBinding[] interfaceBinds = declaringClass.getInterfaces(); // [MISSING:FrameDecoder] if (superClass.toString().contains("MISSING")) { String superClassRelaxed = (superClass.toString().split(":")[1]); superClassRelaxed = superClassRelaxed.substring(0, superClassRelaxed.length() - 1); if (classesNamesMatches(superClassRelaxed, classNamespace)) { return true; } } else { if (classesNamesMatches(superClass.getKey(), classNamespace)) { return true; } else { for (ITypeBinding interfce : interfaceBinds) { if (interfce.toString().contains("MISSING")) { String interfaceRelaxed = (interfce.toString().split(":")[1]); interfaceRelaxed = interfaceRelaxed.substring(0, interfaceRelaxed.length() - 1); if (classesNamesMatches(interfaceRelaxed, classNamespace)) { return true; } } else { if (classesNamesMatches(interfce.getKey(), classNamespace)) { return true; } } } } } return false; } } else { return false; } } catch (NullPointerException e) { return false; } }
public void checkInput(RefactoringStatus status, String methodName, ASTNode destination) { ITypeBinding[] arguments = getArgumentTypes(); ITypeBinding type = ASTNodes.getEnclosingType(destination); status.merge(Checks.checkMethodInType(type, methodName, arguments)); ITypeBinding superClass = type.getSuperclass(); if (superClass != null) { status.merge(Checks.checkMethodInHierarchy(superClass, methodName, null, arguments)); } for (ITypeBinding superInterface : type.getInterfaces()) { status.merge(Checks.checkMethodInHierarchy(superInterface, methodName, null, arguments)); } }
/** * Adds the specified method binding to the search results. * * @param calledMethodBinding * @param node */ protected void addMethodCall(IMethodBinding calledMethodBinding, ASTNode node) { try { if (calledMethodBinding != null) { fProgressMonitor.worked(1); ITypeBinding calledTypeBinding = calledMethodBinding.getDeclaringClass(); IType calledType = null; if (!calledTypeBinding.isAnonymous()) { calledType = (IType) calledTypeBinding.getJavaElement(); } else { if (!"java.lang.Object" .equals(calledTypeBinding.getSuperclass().getQualifiedName())) { // $NON-NLS-1$ calledType = (IType) calledTypeBinding.getSuperclass().getJavaElement(); } else { calledType = (IType) calledTypeBinding.getInterfaces()[0].getJavaElement(); } } IMethod calledMethod = findIncludingSupertypes(calledMethodBinding, calledType, fProgressMonitor); IMember referencedMember = null; if (calledMethod == null) { if (calledMethodBinding.isConstructor() && calledMethodBinding.getParameterTypes().length == 0) { referencedMember = calledType; } } else { if (calledType.isInterface()) { calledMethod = findImplementingMethods(calledMethod); } if (!isIgnoredBySearchScope(calledMethod)) { referencedMember = calledMethod; } } final int position = node.getStartPosition(); final int number = fCompilationUnit.getLineNumber(position); fSearchResults.addMember( fMember, referencedMember, position, position + node.getLength(), number < 1 ? 1 : number); } } catch (JavaModelException jme) { JavaPlugin.log(jme); } }
public static void collectAllInterfaces(ITypeBinding type, Set<ITypeBinding> interfaces) { Deque<ITypeBinding> typeQueue = Lists.newLinkedList(); while (type != null) { typeQueue.add(type); type = type.getSuperclass(); } while (!typeQueue.isEmpty()) { ITypeBinding nextType = typeQueue.poll(); List<ITypeBinding> newInterfaces = Arrays.asList(nextType.getInterfaces()); interfaces.addAll(newInterfaces); typeQueue.addAll(newInterfaces); } }
private void printMethods(TypeDeclaration node) { printMethodsAndOcni(node, Arrays.asList(node.getMethods()), blockComments.get(node)); // If node implements CharSequence, add forwarding method from the // sequenceDescription method to description (toString()). See // JavaToIOSMethodTranslator.loadCharSequenceMethods() for details. ITypeBinding binding = Types.getTypeBinding(node); for (ITypeBinding interfaze : binding.getInterfaces()) { if (interfaze.getQualifiedName().equals("java.lang.CharSequence")) { println("- (NSString *)description {\n return [self sequenceDescription];\n}\n"); } } List<VariableDeclarationFragment> properties = getProperties(node.getFields()); if (properties.size() > 0) { printStrongReferencesMethod(properties); } }
/** * Finds the declaration for a given method and receiver in the same way that the ObjC compiler * will search for a declaration. */ private IMethodBinding getFirstDeclaration(String methodSig, ITypeBinding type) { if (type == null) { return null; } type = type.getTypeDeclaration(); for (IMethodBinding declaredMethod : type.getDeclaredMethods()) { if (methodSig.equals(getObjCMethodSignature(declaredMethod))) { return declaredMethod; } } List<ITypeBinding> supertypes = Lists.newArrayList(); supertypes.addAll(Arrays.asList(type.getInterfaces())); supertypes.add(type.isTypeVariable() ? 0 : supertypes.size(), type.getSuperclass()); for (ITypeBinding supertype : supertypes) { IMethodBinding result = getFirstDeclaration(methodSig, supertype); if (result != null) { return result; } } return null; }
private static boolean typeHierarchyHelper( HierarchyCallback callback, ITypeBinding root, boolean start_at_root) { // First, visit the type, then collect super-types if (start_at_root) { boolean result = callback.nextType(root); if (!result) return result; } { // super-class ITypeBinding sup = root.getSuperclass(); if (sup != null) { boolean result = typeHierarchyHelper(callback, sup, true); if (!result) return result; } } // interfaces for (ITypeBinding face : root.getInterfaces()) { boolean result = typeHierarchyHelper(callback, face, true); if (!result) return result; } return true; // if you get this far, return value doesn't matter. }