@SuppressWarnings("deprecation") private boolean isParameterNamesAvailable() throws Exception { ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setIgnoreMethodBodies(true); IJavaProject javaProject = projectProvider.getJavaProject(resourceSet); parser.setProject(javaProject); IType type = javaProject.findType("org.eclipse.xtext.common.types.testSetups.TestEnum"); IBinding[] bindings = parser.createBindings(new IJavaElement[] {type}, null); ITypeBinding typeBinding = (ITypeBinding) bindings[0]; IMethodBinding[] methods = typeBinding.getDeclaredMethods(); for (IMethodBinding method : methods) { if (method.isConstructor()) { IMethod element = (IMethod) method.getJavaElement(); if (element.exists()) { String[] parameterNames = element.getParameterNames(); if (parameterNames.length == 1 && parameterNames[0].equals("string")) { return true; } } else { return false; } } } return false; }
/** * Adds a cast check to compareTo methods. This helps Comparable types behave well in sorted * collections which rely on Java's runtime type checking. */ @Override public void endVisit(MethodDeclaration node) { IMethodBinding binding = node.getMethodBinding(); if (!binding.getName().equals("compareTo") || node.getBody() == null) { return; } ITypeBinding comparableType = BindingUtil.findInterface(binding.getDeclaringClass(), "java.lang.Comparable"); if (comparableType == null) { return; } ITypeBinding[] typeArguments = comparableType.getTypeArguments(); ITypeBinding[] parameterTypes = binding.getParameterTypes(); if (typeArguments.length != 1 || parameterTypes.length != 1 || !typeArguments[0].isEqualTo(parameterTypes[0])) { return; } IVariableBinding param = node.getParameters().get(0).getVariableBinding(); FunctionInvocation castCheck = createCastCheck(typeArguments[0], new SimpleName(param)); if (castCheck != null) { node.getBody().getStatements().add(0, new ExpressionStatement(castCheck)); } }
public static void getInvalidQualificationProposals( IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) { ASTNode node = problem.getCoveringNode(context.getASTRoot()); if (!(node instanceof Name)) { return; } Name name = (Name) node; IBinding binding = name.resolveBinding(); if (!(binding instanceof ITypeBinding)) { return; } ITypeBinding typeBinding = (ITypeBinding) binding; AST ast = node.getAST(); ASTRewrite rewrite = ASTRewrite.create(ast); rewrite.replace(name, ast.newName(typeBinding.getQualifiedName()), null); String label = CorrectionMessages.JavadocTagsSubProcessor_qualifylinktoinner_description; Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal( label, context.getCompilationUnit(), rewrite, IProposalRelevance.QUALIFY_INNER_TYPE_NAME, image); proposals.add(proposal); }
public static boolean isFloatingPoint(ITypeBinding type) { if (!type.isPrimitive()) { return false; } char binaryName = type.getBinaryName().charAt(0); return binaryName == 'F' || binaryName == 'D'; }
// Metodo para tratamento de declarações de métodos private void methodDeclarationHandler(MethodDeclaration methodDeclaration, ApiClass apiClass) { IMethodBinding mb = methodDeclaration.resolveBinding(); if (mb == null) { return; } LinkedList<String> paramsType = new LinkedList<String>(); for (ITypeBinding pt : mb.getParameterTypes()) { paramsType.add(pt.getQualifiedName().toString()); } ApiMethod apiMethod = new ApiMethod(apiClass, mb.getName(), paramsType); this.setModifiers(apiMethod, methodDeclaration); if (mb.isConstructor()) { apiMethod.setConstructor(true); apiMethod.setVoid(false); apiMethod.setFunction(false); apiMethod.setReturnType(null); } else if (mb.getReturnType().getQualifiedName().equalsIgnoreCase("void")) { apiMethod.setVoid(true); apiMethod.setFunction(false); apiMethod.setReturnType(null); } else { apiMethod.setVoid(false); apiMethod.setFunction(true); apiMethod.setReturnType(mb.getReturnType().getQualifiedName()); } apiClass.getApiMethods().add(apiMethod); this.methodMap.put(mb, apiMethod); }
/** * Ensures that creating a DOM AST and computing the bindings takes the owner's working copies * into account. * * @deprecated using deprecated code */ public void testParseCompilationUnit3() throws CoreException { try { createJavaProject("P1", new String[] {"src"}, new String[] {"JCL_LIB", "lib"}, "bin"); // create X.class in lib folder /* Evaluate the following in a scrapbook: org.eclipse.jdt.core.tests.model.ModifyingResourceTests.generateClassFile( "X", "public class X {\n" + "}") */ byte[] bytes = new byte[] { -54, -2, -70, -66, 0, 3, 0, 45, 0, 13, 1, 0, 1, 88, 7, 0, 1, 1, 0, 16, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 79, 98, 106, 101, 99, 116, 7, 0, 3, 1, 0, 6, 60, 105, 110, 105, 116, 62, 1, 0, 3, 40, 41, 86, 1, 0, 4, 67, 111, 100, 101, 12, 0, 5, 0, 6, 10, 0, 4, 0, 8, 1, 0, 15, 76, 105, 110, 101, 78, 117, 109, 98, 101, 114, 84, 97, 98, 108, 101, 1, 0, 10, 83, 111, 117, 114, 99, 101, 70, 105, 108, 101, 1, 0, 6, 88, 46, 106, 97, 118, 97, 0, 33, 0, 2, 0, 4, 0, 0, 0, 0, 0, 1, 0, 1, 0, 5, 0, 6, 0, 1, 0, 7, 0, 0, 0, 29, 0, 1, 0, 1, 0, 0, 0, 5, 42, -73, 0, 9, -79, 0, 0, 0, 1, 0, 10, 0, 0, 0, 6, 0, 1, 0, 0, 0, 1, 0, 1, 0, 11, 0, 0, 0, 2, 0, 12, }; this.createFile("P1/lib/X.class", bytes); // create libsrc and attach source createFolder("P1/libsrc"); createFile("P1/libsrc/X.java", "public class X extends Y {\n" + "}"); IPackageFragmentRoot lib = getPackageFragmentRoot("P1/lib"); lib.attachSource(new Path("/P1/libsrc"), null, null); // create Y.java in src folder createFile("P1/src/Y.java", ""); // create working copy on Y.java TestWorkingCopyOwner owner = new TestWorkingCopyOwner(); this.workingCopy = getCompilationUnit("P1/src/Y.java").getWorkingCopy(owner, null); this.workingCopy.getBuffer().setContents("public class Y {\n" + "}"); this.workingCopy.makeConsistent(null); // parse and resolve class file IClassFile classFile = getClassFile("P1/lib/X.class"); ASTParser parser = ASTParser.newParser(AST.JLS2); parser.setSource(classFile); 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(); ITypeBinding superType = typeBinding.getSuperclass(); assertEquals( "Unexpected super type", "Y", superType == null ? "<null>" : superType.getQualifiedName()); } finally { deleteProject("P1"); } }
/* * Evaluates possible return expressions. The favourite expression is returned. */ private Expression evaluateReturnExpressions( AST ast, ITypeBinding returnBinding, int returnOffset) { CompilationUnit root = (CompilationUnit) fMethodDecl.getRoot(); Expression result = null; if (returnBinding != null) { ScopeAnalyzer analyzer = new ScopeAnalyzer(root); IBinding[] bindings = analyzer.getDeclarationsInScope( returnOffset, ScopeAnalyzer.VARIABLES | ScopeAnalyzer.CHECK_VISIBILITY); for (int i = 0; i < bindings.length; i++) { IVariableBinding curr = (IVariableBinding) bindings[i]; ITypeBinding type = curr.getType(); if (type != null && type.isAssignmentCompatible(returnBinding) && testModifier(curr)) { if (result == null) { result = ast.newSimpleName(curr.getName()); } addLinkedPositionProposal(RETURN_EXPRESSION_KEY, curr.getName(), null); } } } Expression defaultExpression = ASTNodeFactory.newDefaultExpression( ast, fMethodDecl.getReturnType2(), fMethodDecl.getExtraDimensions()); addLinkedPositionProposal(RETURN_EXPRESSION_KEY, ASTNodes.asString(defaultExpression), null); if (result == null) { return defaultExpression; } return result; }
/** * 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 ITypeBinding[] computeTypeVariables(ITypeBinding[] bindings) { Selection selection = getSelection(); Set<ITypeBinding> result = new HashSet<>(); // first remove all type variables that come from outside of the method // or are covered by the selection CompilationUnit compilationUnit = (CompilationUnit) fEnclosingBodyDeclaration.getRoot(); for (int i = 0; i < bindings.length; i++) { ASTNode decl = compilationUnit.findDeclaringNode(bindings[i]); if (decl == null || (!selection.covers(decl) && decl.getParent() instanceof MethodDeclaration)) result.add(bindings[i]); } // all all type variables which are needed since a local variable uses it for (int i = 0; i < fArguments.length; i++) { IVariableBinding arg = fArguments[i]; ITypeBinding type = arg.getType(); if (type != null && type.isTypeVariable()) { ASTNode decl = compilationUnit.findDeclaringNode(type); if (decl == null || (!selection.covers(decl) && decl.getParent() instanceof MethodDeclaration)) result.add(type); } } return result.toArray(new ITypeBinding[result.size()]); }
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; }
public ClassType getSuperclass() { final ITypeBinding superClass = getDeclarationBinding().getSuperclass(); if (superClass == null) return null; else if (superClass.isClass() && !superClass.isRecovered()) return (ClassType) Factory.createReferenceType(superClass, _env); else // catch error case where user extends some interface instead of a class. return Factory.createErrorClassType(superClass); }
/** Returns the inner type with the specified name. */ public static ITypeBinding findDeclaredType(ITypeBinding type, String name) { for (ITypeBinding innerType : type.getDeclaredTypes()) { if (innerType.getName().equals(name)) { return innerType; } } return null; }
private static boolean typeImplementsInterfaceDirectly(ITypeBinding type, String interfaceName) { for (ITypeBinding implementedInterface : type.getInterfaces()) { if (interfaceName.equals(implementedInterface.getErasure().getQualifiedName())) { return true; } } return false; }
protected void createResourceURIForClass(ITypeBinding typeBinding, StringBuilder uriBuilder) { ITypeBinding declaringClass = typeBinding.getDeclaringClass(); if (declaringClass != null) { createResourceURIForClass(declaringClass, uriBuilder); } else { createResourceURIForClassImpl2(typeBinding.getErasure().getQualifiedName(), uriBuilder); } }
private static void appendParameterSignature(ITypeBinding parameter, StringBuilder sb) { if (!parameter.isPrimitive() && !parameter.isArray()) { sb.append('L'); } sb.append(parameter.getBinaryName().replace('.', '/')); if (!parameter.isPrimitive() && !parameter.isArray()) { sb.append(';'); } }
private List<IVariableBinding> getCapturedVariables(ClassInstanceCreation node) { ITypeBinding newType = node.getTypeBinding().getTypeDeclaration(); ITypeBinding owningType = TreeUtil.getOwningType(node).getTypeBinding().getTypeDeclaration(); // Test for the recursive construction of a local class. if (owningType.isEqualTo(newType)) { return OuterReferenceResolver.getInnerFields(newType); } return OuterReferenceResolver.getCapturedVars(newType); }
public static boolean hasAnnotation(Class<?> annotationClass, List<Annotation> annotations) { for (Annotation annotation : annotations) { ITypeBinding annotationType = annotation.getAnnotationBinding().getAnnotationType(); if (annotationType.getQualifiedName().equals(annotationClass.getName())) { return true; } } return false; }
@Override protected void initialize(ITypeBinding binding) { Assert.isTrue(binding.isWildcardType()); super.initialize(binding); ITypeBinding bound = binding.getBound(); if (bound != null) { fBound = getEnvironment().create(bound); } }
protected void createFragmentForTypeVariable(ITypeBinding typeBinding, StringBuilder uriBuilder) { if (typeBinding.getDeclaringMethod() != null) { createFragmentForMethod(typeBinding.getDeclaringMethod(), uriBuilder); } else { createFragment(typeBinding.getDeclaringClass(), uriBuilder); } uriBuilder.append('/'); uriBuilder.append(typeBinding.getName()); }
/** * Tests if this type is private to it's source file. A public type declared within a private type * is considered private. */ public static boolean isPrivateInnerType(ITypeBinding type) { if (isPrivate(type) || type.isLocal() || type.isAnonymous()) { return true; } ITypeBinding declaringClass = type.getDeclaringClass(); if (declaringClass != null) { return isPrivateInnerType(declaringClass); } return false; }
/** * Check if the method can be called with the given arguments. Used when we don't have a direct * link to the method that is invoked (for example: Action.create(...)). */ public static boolean isApplicableToCall(Iterable<ITypeBinding> args, IMethodBinding meth) { int[] i = {0}; ITypeBinding[] params = meth.getParameterTypes(); for (ITypeBinding arg : args) { if (params.length <= i[0] || !arg.isAssignmentCompatible(params[i[0]++])) { return false; } } return true; }
public URI getFullURI(ITypeBinding typeBinding) { // The URIs for primitive types are cached and indexed by their one character key // representation. // if (typeBinding.isPrimitive()) { return PRIMITIVE_URIS[typeBinding.getKey().charAt(0) - 'B']; } if (typeBinding.isClass() || typeBinding.isInterface() || typeBinding.isAnnotation() || typeBinding.isEnum()) { ITypeBinding declaringClass = typeBinding.getDeclaringClass(); if (declaringClass == null) { // This special case handling for common case of top level types that avoids creating a // builder. // String qualifiedName = typeBinding.getErasure().getQualifiedName(); URI uri = COMMON_URIS.get(qualifiedName); if (uri != null) { return uri; } uri = OBJECTS_URI.appendSegment(qualifiedName); return uri.appendFragment(uri.lastSegment()); } SegmentSequence.Builder builder = SegmentSequence.newBuilder(""); URI uri = getFullURI(declaringClass, builder); builder.append("$"); builder.append(typeBinding.getName()); return uri.appendFragment(builder.toString()); } SegmentSequence.Builder builder = SegmentSequence.newBuilder(""); URI uri = getFullURI(typeBinding, builder); return uri.appendFragment(builder.toString()); }
protected void createResourceURIForTypeVariable( ITypeBinding typeBinding, StringBuilder uriBuilder) { if (typeBinding.getDeclaringClass() != null) { ITypeBinding declaringClass = typeBinding.getDeclaringClass(); createResourceURIForClass(declaringClass, uriBuilder); } else { IMethodBinding declaringMethod = typeBinding.getDeclaringMethod(); ITypeBinding declaringClass = declaringMethod.getDeclaringClass(); createResourceURIForClass(declaringClass, uriBuilder); } }
private SmellyClass getPossibleRole(ITypeBinding tb) { String className = tb.getQualifiedName(); SmellyClass possibleService; if (tb.isInterface()) { List<SmellyClass> subtypes = repo.getSubtypesOf(className); possibleService = findRole(subtypes); } else { possibleService = repo.getByClass(className); } return possibleService; }
private ITypeBinding getDeclaredReturnType(IMethodBinding method, ITypeBinding receiverType) { IMethodBinding actualDeclaration = getFirstDeclaration(getObjCMethodSignature(method), receiverType); if (actualDeclaration == null) { actualDeclaration = method.getMethodDeclaration(); } ITypeBinding returnType = actualDeclaration.getReturnType(); if (returnType.isTypeVariable()) { return typeEnv.resolveIOSType("id"); } return returnType.getErasure(); }
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)); } }
/** Determines if a type can access fields and methods from an outer class. */ public static boolean hasOuterContext(ITypeBinding type) { if (type.getDeclaringClass() == null) { return false; } // Local types can't be declared static, but if the declaring method is // static then the local type is effectively static. IMethodBinding declaringMethod = type.getTypeDeclaration().getDeclaringMethod(); if (declaringMethod != null) { return !BindingUtil.isStatic(declaringMethod); } return !BindingUtil.isStatic(type); }
/** Returns the type binding for a specific interface of a specific type. */ public static ITypeBinding findInterface(ITypeBinding implementingType, String qualifiedName) { if (implementingType.isInterface() && implementingType.getErasure().getQualifiedName().equals(qualifiedName)) { return implementingType; } for (ITypeBinding interfaze : getAllInterfaces(implementingType)) { if (interfaze.getErasure().getQualifiedName().equals(qualifiedName)) { return interfaze; } } return null; }
private Type evaluateVariableType( AST ast, ImportRewrite imports, ImportRewriteContext importRewriteContext, IBinding targetContext) { if (fOriginalNode.getParent() instanceof MethodInvocation) { MethodInvocation parent = (MethodInvocation) fOriginalNode.getParent(); if (parent.getExpression() == fOriginalNode) { // _x_.foo() -> guess qualifier type by looking for a type with method 'foo' ITypeBinding[] bindings = ASTResolving.getQualifierGuess( fOriginalNode.getRoot(), parent.getName().getIdentifier(), parent.arguments(), targetContext); if (bindings.length > 0) { for (int i = 0; i < bindings.length; i++) { addLinkedPositionProposal(KEY_TYPE, bindings[i]); } return imports.addImport(bindings[0], ast, importRewriteContext); } } } ITypeBinding binding = ASTResolving.guessBindingForReference(fOriginalNode); if (binding != null) { if (binding.isWildcardType()) { binding = ASTResolving.normalizeWildcardType(binding, isVariableAssigned(), ast); if (binding == null) { // only null binding applies binding = ast.resolveWellKnownType("java.lang.Object"); // $NON-NLS-1$ } } if (isVariableAssigned()) { ITypeBinding[] typeProposals = ASTResolving.getRelaxingTypes(ast, binding); for (int i = 0; i < typeProposals.length; i++) { addLinkedPositionProposal(KEY_TYPE, typeProposals[i]); } } return imports.addImport(binding, ast, importRewriteContext); } // no binding, find type AST node instead -> ABC a= x-> use 'ABC' as is Type type = ASTResolving.guessTypeForReference(ast, fOriginalNode); if (type != null) { return type; } if (fVariableKind == CONST_FIELD) { return ast.newSimpleType(ast.newSimpleName("String")); // $NON-NLS-1$ } return ast.newSimpleType(ast.newSimpleName("Object")); // $NON-NLS-1$ }
private static String fqTypeName(ITypeBinding type) { type = canonicalType(type); // TODO Need to get rid of type variables String type_name = type.getQualifiedName(); if ("".equals(type_name)) { // only okay if this is a top-level class. if (type.getDeclaringClass() != null) { return null; } // Will the 'else' branch _ever_ be taken? } return type.getQualifiedName(); }