Java Code Examples for org.eclipse.jdt.core.dom.ITypeBinding#getJavaElement()
The following examples show how to use
org.eclipse.jdt.core.dom.ITypeBinding#getJavaElement() .
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: SourceAssistProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
public static IType getSelectionType(IInvocationContext context) { ICompilationUnit unit = context.getCompilationUnit(); ASTNode node = context.getCoveredNode(); if (node == null) { node = context.getCoveringNode(); } ITypeBinding typeBinding = null; while (node != null && !(node instanceof CompilationUnit)) { if (node instanceof AbstractTypeDeclaration) { typeBinding = ((AbstractTypeDeclaration) node).resolveBinding(); break; } else if (node instanceof AnonymousClassDeclaration) { // Anonymous typeBinding = ((AnonymousClassDeclaration) node).resolveBinding(); break; } node = node.getParent(); } if (typeBinding != null && typeBinding.getJavaElement() instanceof IType) { return (IType) typeBinding.getJavaElement(); } return unit.findPrimaryType(); }
Example 2
Source File: RefactorProposalUtility.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
private static String getUniqueMethodName(ASTNode astNode, String suggestedName) throws JavaModelException { while (astNode != null && !(astNode instanceof TypeDeclaration || astNode instanceof AnonymousClassDeclaration)) { astNode = astNode.getParent(); } if (astNode instanceof TypeDeclaration) { ITypeBinding typeBinding = ((TypeDeclaration) astNode).resolveBinding(); if (typeBinding == null) { return suggestedName; } IType type = (IType) typeBinding.getJavaElement(); if (type == null) { return suggestedName; } IMethod[] methods = type.getMethods(); int suggestedPostfix = 2; String resultName = suggestedName; while (suggestedPostfix < 1000) { if (!hasMethod(methods, resultName)) { return resultName; } resultName = suggestedName + suggestedPostfix++; } } return suggestedName; }
Example 3
Source File: NLSHintHelper.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
public static IStorage getResourceBundle(IJavaProject javaProject, AccessorClassReference accessorClassReference) throws JavaModelException { String resourceBundle= accessorClassReference.getResourceBundleName(); if (resourceBundle == null) return null; String resourceName= Signature.getSimpleName(resourceBundle) + NLSRefactoring.PROPERTY_FILE_EXT; String packName= Signature.getQualifier(resourceBundle); ITypeBinding accessorClass= accessorClassReference.getBinding(); if (accessorClass.isFromSource()) return getResourceBundle(javaProject, packName, resourceName); else if (accessorClass.getJavaElement() != null) return getResourceBundle((IPackageFragmentRoot)accessorClass.getJavaElement().getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT), packName, resourceName); return null; }
Example 4
Source File: ChangeSignatureProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private RefactoringStatus createExceptionInfoList() { if (fExceptionInfos == null || fExceptionInfos.isEmpty()) { fExceptionInfos= new ArrayList<ExceptionInfo>(0); try { ASTNode nameNode= NodeFinder.perform(fBaseCuRewrite.getRoot(), fMethod.getNameRange()); if (nameNode == null || !(nameNode instanceof Name) || !(nameNode.getParent() instanceof MethodDeclaration)) return null; MethodDeclaration methodDeclaration= (MethodDeclaration) nameNode.getParent(); List<Type> exceptions= methodDeclaration.thrownExceptionTypes(); List<ExceptionInfo> result= new ArrayList<ExceptionInfo>(exceptions.size()); for (int i= 0; i < exceptions.size(); i++) { Type type= exceptions.get(i); ITypeBinding typeBinding= type.resolveBinding(); if (typeBinding == null) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ChangeSignatureRefactoring_no_exception_binding); IJavaElement element= typeBinding.getJavaElement(); result.add(ExceptionInfo.createInfoForOldException(element, typeBinding)); } fExceptionInfos= result; } catch (JavaModelException e) { JavaPlugin.log(e); } } return null; }
Example 5
Source File: JavaMethodParameterCodeMining.java From jdt-codemining with Eclipse Public License 1.0 | 5 votes |
private IMethod getMethod(IMethodBinding calledMethodBinding) throws JavaModelException { if (calledMethodBinding == null) { return null; } ITypeBinding calledTypeBinding = calledMethodBinding.getDeclaringClass(); IType calledType = (IType) calledTypeBinding.getJavaElement(); return Bindings.findMethod(calledMethodBinding, calledType); }
Example 6
Source File: ImplementationCollector.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private static IType getType(ITypeBinding typeBinding) { if (typeBinding == null) { return null; } if (typeBinding.isTypeVariable()) { ITypeBinding[] typeBounds= typeBinding.getTypeBounds(); if (typeBounds.length > 0) { typeBinding= typeBounds[0].getTypeDeclaration(); } else { return null; } } return (IType) typeBinding.getJavaElement(); }
Example 7
Source File: UiBinderJavaValidator.java From gwt-eclipse-plugin with Eclipse Public License 1.0 | 5 votes |
private static IType getType(TypeDeclaration typeDecl) { if (typeDecl == null) { return null; } ITypeBinding typeBinding = typeDecl.resolveBinding(); if (typeBinding == null) { return null; } IJavaElement javaElement = typeBinding.getJavaElement(); return (javaElement instanceof IType ? (IType) javaElement : null); }
Example 8
Source File: NLSHintHelper.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public static String getResourceBundleName(ITypeBinding accessorClassBinding) { IJavaElement je= accessorClassBinding.getJavaElement(); if (!(je instanceof IType)) return null; ITypeRoot typeRoot= ((IType) je).getTypeRoot(); CompilationUnit astRoot= SharedASTProvider.getAST(typeRoot, SharedASTProvider.WAIT_YES, null); return getResourceBundleName(astRoot); }
Example 9
Source File: TypeEnvironment.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private TypeVariable createTypeVariable(ITypeBinding binding) { IJavaElement javaElement= binding.getJavaElement(); TypeVariable result= fTypeVariables.get(javaElement); if (result != null) return result; result= new TypeVariable(this); fTypeVariables.put(javaElement, result); result.initialize(binding, (ITypeParameter)javaElement); return result; }
Example 10
Source File: SerialVersionHashOperation.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static IFile getClassfile(ITypeBinding typeBinding) throws CoreException { // bug 191943 IType type= (IType) typeBinding.getJavaElement(); if (type == null || type.getCompilationUnit() == null) { return null; } IRegion region= JavaCore.newRegion(); region.add(type.getCompilationUnit()); String name= typeBinding.getBinaryName(); if (name != null) { int packStart= name.lastIndexOf('.'); if (packStart != -1) { name= name.substring(packStart + 1); } } else { throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, CorrectionMessages.SerialVersionHashOperation_error_classnotfound)); } name += ".class"; //$NON-NLS-1$ IResource[] classFiles= JavaCore.getGeneratedResources(region, false); for (int i= 0; i < classFiles.length; i++) { IResource resource= classFiles[i]; if (resource.getType() == IResource.FILE && resource.getName().equals(name)) { return (IFile) resource; } } throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, CorrectionMessages.SerialVersionHashOperation_error_classnotfound)); }
Example 11
Source File: TypeEnvironment.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private RawType createRawType(ITypeBinding binding) { IJavaElement javaElement= binding.getJavaElement(); RawType result= fRawTypes.get(javaElement); if (result != null) return result; result= new RawType(this); fRawTypes.put(javaElement, result); result.initialize(binding, (IType)javaElement); cacheSubType(result.getSuperclass(), result); cacheSubTypes(result.getInterfaces(), result); return result; }
Example 12
Source File: TypeEnvironment.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private StandardType createStandardType(ITypeBinding binding) { IJavaElement javaElement= binding.getJavaElement(); StandardType result= fStandardTypes.get(javaElement); if (result != null) return result; result= new StandardType(this); fStandardTypes.put(javaElement, result); result.initialize(binding, (IType)javaElement); if (OBJECT_TYPE == null && result.isJavaLangObject()) OBJECT_TYPE= result; return result; }
Example 13
Source File: TypeEnvironment.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private GenericType createGenericType(ITypeBinding binding) { IJavaElement javaElement= binding.getJavaElement(); GenericType result= fGenericTypes.get(javaElement); if (result != null) return result; result= new GenericType(this); fGenericTypes.put(javaElement, result); result.initialize(binding, (IType)javaElement); cacheSubType(result.getSuperclass(), result); cacheSubTypes(result.getInterfaces(), result); return result; }
Example 14
Source File: LazyGenericTypeProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Computes the type argument proposals for this type proposals. If there is * an expected type binding that is a super type of the proposed type, the * wildcard type arguments of the proposed type that can be mapped through * to type the arguments of the expected type binding are bound accordingly. * <p> * For type arguments that cannot be mapped to arguments in the expected * type, or if there is no expected type, the upper bound of the type * argument is proposed. * </p> * <p> * The argument proposals have their <code>isAmbiguos</code> flag set to * <code>false</code> if the argument can be mapped to a non-wildcard type * argument in the expected type, otherwise the proposal is ambiguous. * </p> * * @return the type argument proposals for the proposed type * @throws JavaModelException if accessing the java model fails */ private TypeArgumentProposal[] computeTypeArgumentProposals() throws JavaModelException { if (fTypeArgumentProposals == null) { IType type= (IType) getJavaElement(); if (type == null) return new TypeArgumentProposal[0]; ITypeParameter[] parameters= type.getTypeParameters(); if (parameters.length == 0) return new TypeArgumentProposal[0]; TypeArgumentProposal[] arguments= new TypeArgumentProposal[parameters.length]; ITypeBinding expectedTypeBinding= getExpectedType(); if (expectedTypeBinding != null && expectedTypeBinding.isParameterizedType()) { // in this case, the type arguments we propose need to be compatible // with the corresponding type parameters to declared type IType expectedType= (IType) expectedTypeBinding.getJavaElement(); IType[] path= computeInheritancePath(type, expectedType); if (path == null) // proposed type does not inherit from expected type // the user might be looking for an inner type of proposed type // to instantiate -> do not add any type arguments return new TypeArgumentProposal[0]; int[] indices= new int[parameters.length]; for (int paramIdx= 0; paramIdx < parameters.length; paramIdx++) { indices[paramIdx]= mapTypeParameterIndex(path, path.length - 1, paramIdx); } // for type arguments that are mapped through to the expected type's // parameters, take the arguments of the expected type ITypeBinding[] typeArguments= expectedTypeBinding.getTypeArguments(); for (int paramIdx= 0; paramIdx < parameters.length; paramIdx++) { if (indices[paramIdx] != -1) { // type argument is mapped through ITypeBinding binding= typeArguments[indices[paramIdx]]; arguments[paramIdx]= computeTypeProposal(binding, parameters[paramIdx]); } } } // for type arguments that are not mapped through to the expected type, // take the lower bound of the type parameter for (int i= 0; i < arguments.length; i++) { if (arguments[i] == null) { arguments[i]= computeTypeProposal(parameters[i]); } } fTypeArgumentProposals= arguments; } return fTypeArgumentProposals; }
Example 15
Source File: IntroduceFactoryRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Determines what kind of AST node was selected, and returns an error status * if the kind of node is inappropriate for this refactoring. * @param pm * @return a RefactoringStatus indicating whether the selection is valid * @throws JavaModelException */ private RefactoringStatus checkSelection(IProgressMonitor pm) throws JavaModelException { try { pm.beginTask(RefactoringCoreMessages.IntroduceFactory_examiningSelection, 2); fSelectedNode= getTargetNode(fCUHandle, fSelectionStart, fSelectionLength); if (fSelectedNode == null) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_notAConstructorInvocation); // getTargetNode() must return either a ClassInstanceCreation or a // constructor MethodDeclaration; nothing else. if (fSelectedNode instanceof ClassInstanceCreation) { ClassInstanceCreation classInstanceCreation= (ClassInstanceCreation)fSelectedNode; fCtorBinding= classInstanceCreation.resolveConstructorBinding(); } else if (fSelectedNode instanceof MethodDeclaration) { MethodDeclaration methodDeclaration= (MethodDeclaration)fSelectedNode; fCtorBinding= methodDeclaration.resolveBinding(); } if (fCtorBinding == null) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_unableToResolveConstructorBinding); // If this constructor is of a generic type, get the generic version, // not some instantiation thereof. fCtorBinding= fCtorBinding.getMethodDeclaration(); pm.worked(1); // We don't handle constructors of nested types at the moment if (fCtorBinding.getDeclaringClass().isNested()) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_unsupportedNestedTypes); ITypeBinding ctorType= fCtorBinding.getDeclaringClass(); IType ctorOwningType= (IType) ctorType.getJavaElement(); if (ctorOwningType.isBinary()) // Can't modify binary CU; don't know what CU to put factory method return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_constructorInBinaryClass); if (ctorOwningType.isEnum()) // Doesn't make sense to encapsulate enum constructors return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_constructorInEnum); // Put the generated factory method inside the type that owns the constructor fFactoryUnitHandle= ctorOwningType.getCompilationUnit(); fFactoryCU= getASTFor(fFactoryUnitHandle); Name ctorOwnerName= (Name) NodeFinder.perform(fFactoryCU, ctorOwningType.getNameRange()); fCtorOwningClass= (AbstractTypeDeclaration) ASTNodes.getParent(ctorOwnerName, AbstractTypeDeclaration.class); fFactoryOwningClass= fCtorOwningClass; pm.worked(1); if (fNewMethodName == null) return setNewMethodName("create" + fCtorBinding.getName());//$NON-NLS-1$ else return new RefactoringStatus(); } finally { pm.done(); } }
Example 16
Source File: MoveInstanceMethodProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private static boolean is18OrHigherInterface(ITypeBinding binding) { if (!binding.isInterface() || binding.isAnnotation()) return false; IJavaElement javaElement= binding.getJavaElement(); return javaElement != null && JavaModelUtil.is18OrHigher(javaElement.getJavaProject()); }
Example 17
Source File: PreconditionExaminer.java From JDeodorant with MIT License | 4 votes |
public boolean isTemplateMethodApplicable() { AbstractMethodDeclaration methodObject1 = getPDG1().getMethod(); AbstractMethodDeclaration methodObject2 = getPDG2().getMethod(); MethodDeclaration methodDeclaration1 = methodObject1.getMethodDeclaration(); MethodDeclaration methodDeclaration2 = methodObject2.getMethodDeclaration(); ITypeBinding typeBinding1 = null; if(methodDeclaration1.getParent() instanceof AbstractTypeDeclaration) { typeBinding1 = ((AbstractTypeDeclaration)methodDeclaration1.getParent()).resolveBinding(); } else if(methodDeclaration1.getParent() instanceof AnonymousClassDeclaration) { typeBinding1 = ((AnonymousClassDeclaration)methodDeclaration1.getParent()).resolveBinding(); } ITypeBinding typeBinding2 = null; if(methodDeclaration2.getParent() instanceof AbstractTypeDeclaration) { typeBinding2 = ((AbstractTypeDeclaration)methodDeclaration2.getParent()).resolveBinding(); } else if(methodDeclaration2.getParent() instanceof AnonymousClassDeclaration) { typeBinding2 = ((AnonymousClassDeclaration)methodDeclaration2.getParent()).resolveBinding(); } if(typeBinding1 != null && typeBinding2 != null) { //not in the same type if(!typeBinding1.isEqualTo(typeBinding2) || !typeBinding1.getQualifiedName().equals(typeBinding2.getQualifiedName())) { ITypeBinding commonSuperTypeOfSourceTypeDeclarations = ASTNodeMatcher.commonSuperType(typeBinding1, typeBinding2); if(commonSuperTypeOfSourceTypeDeclarations != null) { ClassObject classObject = ASTReader.getSystemObject().getClassObject(commonSuperTypeOfSourceTypeDeclarations.getErasure().getQualifiedName()); //abstract system class if(classObject != null && commonSuperTypeOfSourceTypeDeclarations.getErasure().isClass() && classObject.isAbstract()) { CompilationUnitCache cache = CompilationUnitCache.getInstance(); Set<IType> subTypes = cache.getSubTypes((IType)commonSuperTypeOfSourceTypeDeclarations.getJavaElement()); IType type1 = (IType)typeBinding1.getJavaElement(); IType type2 = (IType)typeBinding2.getJavaElement(); //only two subTypes corresponding to the types of the classes containing the clones if(subTypes.size() == 2 && subTypes.contains(type1) && subTypes.contains(type2)) { return true; } } } } } return false; }
Example 18
Source File: MoveInstanceMethodProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
@Override public final boolean visit(final MethodInvocation node) { Assert.isNotNull(node); final Expression expression= node.getExpression(); final IMethodBinding method= node.resolveMethodBinding(); if (method != null) { final ASTRewrite rewrite= fRewrite; if (expression == null) { final AST ast= node.getAST(); if (!JdtFlags.isStatic(method)) rewrite.set(node, MethodInvocation.EXPRESSION_PROPERTY, ast.newSimpleName(fTargetName), null); else if (!fStaticImports.contains(method)) { ITypeBinding declaring= method.getDeclaringClass(); if (declaring != null) { IType type= (IType) declaring.getJavaElement(); if (type != null) { rewrite.set(node, MethodInvocation.EXPRESSION_PROPERTY, ast.newName(type.getTypeQualifiedName('.')), null); } } } return true; } else { if (expression instanceof FieldAccess) { final FieldAccess access= (FieldAccess) expression; if (Bindings.equals(fTarget, access.resolveFieldBinding())) { rewrite.remove(expression, null); visit(node.arguments()); return false; } } else if (expression instanceof Name) { final Name name= (Name) expression; if (Bindings.equals(fTarget, name.resolveBinding())) { rewrite.remove(expression, null); visit(node.arguments()); return false; } } } } return true; }
Example 19
Source File: CompletionProposalReplacementProvider.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private String[] computeTypeArgumentProposals(CompletionProposal proposal) { try { IType type = (IType) resolveJavaElement( compilationUnit.getJavaProject(), proposal); if (type == null) { return new String[0]; } ITypeParameter[] parameters = type.getTypeParameters(); if (parameters.length == 0) { return new String[0]; } String[] arguments = new String[parameters.length]; ITypeBinding expectedTypeBinding = getExpectedTypeForGenericParameters(); if (expectedTypeBinding != null && expectedTypeBinding.isParameterizedType()) { // in this case, the type arguments we propose need to be compatible // with the corresponding type parameters to declared type IType expectedType= (IType) expectedTypeBinding.getJavaElement(); IType[] path= TypeProposalUtils.computeInheritancePath(type, expectedType); if (path == null) { // proposed type does not inherit from expected type // the user might be looking for an inner type of proposed type // to instantiate -> do not add any type arguments return new String[0]; } int[] indices= new int[parameters.length]; for (int paramIdx= 0; paramIdx < parameters.length; paramIdx++) { indices[paramIdx]= TypeProposalUtils.mapTypeParameterIndex(path, path.length - 1, paramIdx); } // for type arguments that are mapped through to the expected type's // parameters, take the arguments of the expected type ITypeBinding[] typeArguments= expectedTypeBinding.getTypeArguments(); for (int paramIdx= 0; paramIdx < parameters.length; paramIdx++) { if (indices[paramIdx] != -1) { // type argument is mapped through ITypeBinding binding= typeArguments[indices[paramIdx]]; arguments[paramIdx]= computeTypeProposal(binding, parameters[paramIdx]); } } } // for type arguments that are not mapped through to the expected type, // take the lower bound of the type parameter for (int i = 0; i < arguments.length; i++) { if (arguments[i] == null) { arguments[i] = computeTypeProposal(parameters[i]); } } return arguments; } catch (JavaModelException e) { return new String[0]; } }
Example 20
Source File: Bindings.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 3 votes |
/** * Finds the compilation unit where the type of the given <code>ITypeBinding</code> is defined, * using the class path defined by the given Java project. Returns <code>null</code> * if no compilation unit is found (e.g. type binding is from a binary type) * @param typeBinding the type binding to search for * @param project the project used as a scope * @return the compilation unit containing the type * @throws JavaModelException if an errors occurs in the Java model */ public static ICompilationUnit findCompilationUnit(ITypeBinding typeBinding, IJavaProject project) throws JavaModelException { IJavaElement type= typeBinding.getJavaElement(); if (type instanceof IType) return ((IType) type).getCompilationUnit(); else return null; }