Java Code Examples for org.eclipse.jdt.core.dom.ASTParser#createBindings()

The following examples show how to use org.eclipse.jdt.core.dom.ASTParser#createBindings() . 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: JdtTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@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;
}
 
Example 2
Source File: JDTUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static IBinding getHoveredNodeBinding(IJavaElement element, ITypeRoot typeRoot, IRegion region) {
	if (typeRoot == null || region == null) {
		return null;
	}
	IBinding binding;
	ASTNode node = getHoveredASTNode(typeRoot, region);
	if (node == null) {
		ASTParser p = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);
		p.setProject(element.getJavaProject());
		p.setBindingsRecovery(true);
		try {
			binding = p.createBindings(new IJavaElement[] { element }, null)[0];
		} catch (OperationCanceledException e) {
			return null;
		}
	} else {
		binding = resolveBinding(node);
	}
	return binding;
}
 
Example 3
Source File: JavaASTUtils.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
public static ITypeBinding resolveType(IJavaProject javaProject,
    String qualifiedTypeName) throws JavaModelException {
  IType type = javaProject.findType(qualifiedTypeName);
  if (type == null || !type.exists()) {
    return null;
  }

  ASTParser parser = ASTParser.newParser(AST.JLS3);
  parser.setProject(javaProject);
  IBinding[] bindings = parser.createBindings(new IJavaElement[] {type}, null);
  if (bindings == null) {
    return null;
  }

  return (ITypeBinding) bindings[0];
}
 
Example 4
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static String[] getParameterTypeNamesForSeeTag(IMethod overridden) {
	try {
		ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
		parser.setProject(overridden.getJavaProject());
		IBinding[] bindings= parser.createBindings(new IJavaElement[] { overridden }, null);
		if (bindings.length == 1 && bindings[0] instanceof IMethodBinding) {
			return getParameterTypeNamesForSeeTag((IMethodBinding)bindings[0]);
		}
	} catch (IllegalStateException e) {
		// method does not exist
	}
	// fall back code. Not good for generic methods!
	String[] paramTypes= overridden.getParameterTypes();
	String[] paramTypeNames= new String[paramTypes.length];
	for (int i= 0; i < paramTypes.length; i++) {
		paramTypeNames[i]= Signature.toString(Signature.getTypeErasure(paramTypes[i]));
	}
	return paramTypeNames;
}
 
Example 5
Source File: TypeURIHelper.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public URI getFullURI(IJavaElement javaElement) {
	@SuppressWarnings("all")
	ASTParser parser = ASTParser.newParser(AST.JLS3);
	parser.setProject(javaElement.getJavaProject());
	IBinding[] bindings = parser.createBindings(new IJavaElement[] { javaElement }, null);
	if (bindings[0] != null) {
		return getFullURI(bindings[0]);
	}
	return null;
}
 
Example 6
Source File: TypeEnvironment.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private StandardType createStandardType(String fullyQualifiedName, IJavaProject focus) {
	try {
		IType javaElementType= focus.findType(fullyQualifiedName);
		StandardType result= fStandardTypes.get(javaElementType);
		if (result != null)
			return result;
		ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
		parser.setProject(focus);
		IBinding[] bindings= parser.createBindings(new IJavaElement[] {javaElementType} , null);
		return createStandardType((ITypeBinding)bindings[0]);
	} catch (JavaModelException e) {
		// fall through
	}
	return null;
}
 
Example 7
Source File: UseSuperTypeProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Creates the text change manager for this processor.
 *
 * @param monitor
 *            the progress monitor to display progress
 * @param status
 *            the refactoring status
 * @return the created text change manager
 * @throws JavaModelException
 *             if the method declaration could not be found
 * @throws CoreException
 *             if the changes could not be generated
 */
protected final TextEditBasedChangeManager createChangeManager(final IProgressMonitor monitor, final RefactoringStatus status) throws JavaModelException, CoreException {
	Assert.isNotNull(status);
	Assert.isNotNull(monitor);
	try {
		monitor.beginTask("", 300); //$NON-NLS-1$
		monitor.setTaskName(RefactoringCoreMessages.UseSuperTypeProcessor_creating);
		final TextEditBasedChangeManager manager= new TextEditBasedChangeManager();
		final IJavaProject project= fSubType.getJavaProject();
		final ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
		parser.setWorkingCopyOwner(fOwner);
		parser.setResolveBindings(true);
		parser.setProject(project);
		parser.setCompilerOptions(RefactoringASTParser.getCompilerOptions(project));
		if (fSubType.isBinary() || fSubType.isReadOnly()) {
			final IBinding[] bindings= parser.createBindings(new IJavaElement[] { fSubType, fSuperType }, new SubProgressMonitor(monitor, 50));
			if (bindings != null && bindings.length == 2 && bindings[0] instanceof ITypeBinding && bindings[1] instanceof ITypeBinding) {
				solveSuperTypeConstraints(null, null, fSubType, (ITypeBinding) bindings[0], (ITypeBinding) bindings[1], new SubProgressMonitor(monitor, 100), status);
				if (!status.hasFatalError())
					rewriteTypeOccurrences(manager, null, null, null, null, new HashSet<String>(), status, new SubProgressMonitor(monitor, 150));
			}
		} else {
			parser.createASTs(new ICompilationUnit[] { fSubType.getCompilationUnit() }, new String[0], new ASTRequestor() {

				@Override
				public final void acceptAST(final ICompilationUnit unit, final CompilationUnit node) {
					try {
						final CompilationUnitRewrite subRewrite= new CompilationUnitRewrite(fOwner, unit, node);
						final AbstractTypeDeclaration subDeclaration= ASTNodeSearchUtil.getAbstractTypeDeclarationNode(fSubType, subRewrite.getRoot());
						if (subDeclaration != null) {
							final ITypeBinding subBinding= subDeclaration.resolveBinding();
							if (subBinding != null) {
								final ITypeBinding superBinding= findTypeInHierarchy(subBinding, fSuperType.getFullyQualifiedName('.'));
								if (superBinding != null) {
									solveSuperTypeConstraints(subRewrite.getCu(), subRewrite.getRoot(), fSubType, subBinding, superBinding, new SubProgressMonitor(monitor, 100), status);
									if (!status.hasFatalError()) {
										rewriteTypeOccurrences(manager, this, subRewrite, subRewrite.getCu(), subRewrite.getRoot(), new HashSet<String>(), status, new SubProgressMonitor(monitor, 200));
										final TextChange change= subRewrite.createChange(true);
										if (change != null)
											manager.manage(subRewrite.getCu(), change);
									}
								}
							}
						}
					} catch (CoreException exception) {
						JavaPlugin.log(exception);
						status.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.UseSuperTypeProcessor_internal_error));
					}
				}

				@Override
				public final void acceptBinding(final String key, final IBinding binding) {
					// Do nothing
				}
			}, new NullProgressMonitor());
		}
		return manager;
	} finally {
		monitor.done();
	}
}
 
Example 8
Source File: JavadocHover.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static String getAnnotations(IJavaElement element, ITypeRoot editorInputElement, IRegion hoverRegion) throws URISyntaxException, JavaModelException {
	if (!(element instanceof IPackageFragment)) {
		if (!(element instanceof IAnnotatable))
			return null;
		
		if (((IAnnotatable)element).getAnnotations().length == 0)
			return null;
	}
	
	IBinding binding;
	ASTNode node= getHoveredASTNode(editorInputElement, hoverRegion);
	
	if (node == null) {
		ASTParser p= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
		p.setProject(element.getJavaProject());
		p.setBindingsRecovery(true);
		try {
			binding= p.createBindings(new IJavaElement[] { element }, null)[0];
		} catch (OperationCanceledException e) {
			return null;
		}
		
	} else {
		binding= resolveBinding(node);
	}
	
	if (binding == null)
		return null;
	
	IAnnotationBinding[] annotations= binding.getAnnotations();
	if (annotations.length == 0)
		return null;
	
	StringBuffer buf= new StringBuffer();
	for (int i= 0; i < annotations.length; i++) {
		//TODO: skip annotations that don't have an @Documented annotation?
		addAnnotation(buf, element, annotations[i]);
		buf.append("<br>"); //$NON-NLS-1$
	}
	
	return buf.toString();
}