org.eclipse.jdt.core.dom.ClassInstanceCreation#TYPE_PROPERTY源码实例Demo

下面列出了org.eclipse.jdt.core.dom.ClassInstanceCreation#TYPE_PROPERTY 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

/**
 * Finds and returns the <code>ASTNode</code> for the given source text
 * selection, if it is an entire constructor call or the class name portion
 * of a constructor call or constructor declaration, or null otherwise.
 * @param unit The compilation unit in which the selection was made
 * @param offset The textual offset of the start of the selection
 * @param length The length of the selection in characters
 * @return ClassInstanceCreation or MethodDeclaration
 */
private ASTNode getTargetNode(ICompilationUnit unit, int offset, int length) {
	ASTNode node= ASTNodes.getNormalizedNode(NodeFinder.perform(fCU, offset, length));
	if (node.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION)
		return node;
	if (node.getNodeType() == ASTNode.METHOD_DECLARATION && ((MethodDeclaration)node).isConstructor())
		return node;
	// we have some sub node. Make sure its the right child of the parent
	StructuralPropertyDescriptor location= node.getLocationInParent();
	ASTNode parent= node.getParent();
	if (location == ClassInstanceCreation.TYPE_PROPERTY) {
		return parent;
	} else if (location == MethodDeclaration.NAME_PROPERTY && ((MethodDeclaration)parent).isConstructor()) {
		return parent;
	}
	return null;
}
 
private static IBinding resolveBinding(ASTNode node) {
	if (node instanceof SimpleName) {
		SimpleName simpleName= (SimpleName) node;
		// workaround for https://bugs.eclipse.org/62605 (constructor name resolves to type, not method)
		ASTNode normalized= ASTNodes.getNormalizedNode(simpleName);
		if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
			ClassInstanceCreation cic= (ClassInstanceCreation) normalized.getParent();
			IMethodBinding constructorBinding= cic.resolveConstructorBinding();
			if (constructorBinding == null)
				return null;
			ITypeBinding declaringClass= constructorBinding.getDeclaringClass();
			if (!declaringClass.isAnonymous())
				return constructorBinding;
			ITypeBinding superTypeDeclaration= declaringClass.getSuperclass().getTypeDeclaration();
			return resolveSuperclassConstructor(superTypeDeclaration, constructorBinding);
		}
		return simpleName.resolveBinding();
		
	} else if (node instanceof SuperConstructorInvocation) {
		return ((SuperConstructorInvocation) node).resolveConstructorBinding();
	} else if (node instanceof ConstructorInvocation) {
		return ((ConstructorInvocation) node).resolveConstructorBinding();
	} else {
		return null;
	}
}
 
源代码3 项目: eclipse.jdt.ls   文件: SemanticHighlightings.java
/**
 * Extracts the binding from the token's simple name. Works around bug 62605 to
 * return the correct constructor binding in a ClassInstanceCreation.
 *
 * @param token
 *            the token to extract the binding from
 * @return the token's binding, or <code>null</code>
 */
private static IBinding getBinding(SemanticToken token) {
	ASTNode node = token.getNode();
	ASTNode normalized = ASTNodes.getNormalizedNode(node);
	if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
		// work around: https://bugs.eclipse.org/bugs/show_bug.cgi?id=62605
		return ((ClassInstanceCreation) normalized.getParent()).resolveConstructorBinding();
	}
	return token.getBinding();
}
 
源代码4 项目: eclipse.jdt.ls   文件: JDTUtils.java
private static IBinding resolveBinding(ASTNode node) {
	if (node instanceof SimpleName) {
		SimpleName simpleName = (SimpleName) node;
		// workaround for https://bugs.eclipse.org/62605 (constructor name resolves to type, not method)
		ASTNode normalized = ASTNodes.getNormalizedNode(simpleName);
		if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
			ClassInstanceCreation cic = (ClassInstanceCreation) normalized.getParent();
			IMethodBinding constructorBinding = cic.resolveConstructorBinding();
			if (constructorBinding == null) {
				return null;
			}
			ITypeBinding declaringClass = constructorBinding.getDeclaringClass();
			if (!declaringClass.isAnonymous()) {
				return constructorBinding;
			}
			ITypeBinding superTypeDeclaration = declaringClass.getSuperclass().getTypeDeclaration();
			return resolveSuperclassConstructor(superTypeDeclaration, constructorBinding);
		}
		return simpleName.resolveBinding();

	} else if (node instanceof SuperConstructorInvocation) {
		return ((SuperConstructorInvocation) node).resolveConstructorBinding();
	} else if (node instanceof ConstructorInvocation) {
		return ((ConstructorInvocation) node).resolveConstructorBinding();
	} else if (node instanceof LambdaExpression) {
		return ((LambdaExpression) node).resolveMethodBinding();
	} else {
		return null;
	}
}
 
public static boolean isIntroduceFactoryAvailable(final JavaTextSelection selection) throws JavaModelException {
	final IJavaElement[] elements= selection.resolveElementAtOffset();
	if (elements.length == 1 && elements[0] instanceof IMethod)
		return isIntroduceFactoryAvailable((IMethod) elements[0]);

	// there's no IMethod for the default constructor
	if (!Checks.isAvailable(selection.resolveEnclosingElement()))
		return false;
	ASTNode node= selection.resolveCoveringNode();
	if (node == null) {
		ASTNode[] selectedNodes= selection.resolveSelectedNodes();
		if (selectedNodes != null && selectedNodes.length == 1) {
			node= selectedNodes[0];
			if (node == null)
				return false;
		} else {
			return false;
		}
	}

	if (node.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION)
		return true;

	node= ASTNodes.getNormalizedNode(node);
	if (node.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY)
		return true;

	return false;
}
 
/**
 * Extracts the binding from the token's simple name.
 * Works around bug 62605 to return the correct constructor binding in a ClassInstanceCreation.
 *
 * @param token the token to extract the binding from
 * @return the token's binding, or <code>null</code>
 */
private static IBinding getBinding(SemanticToken token) {
	ASTNode node= token.getNode();
	ASTNode normalized= ASTNodes.getNormalizedNode(node);
	if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
		// work around: https://bugs.eclipse.org/bugs/show_bug.cgi?id=62605
		return ((ClassInstanceCreation) normalized.getParent()).resolveConstructorBinding();
	}
	return token.getBinding();
}
 
/**
 * Checks whether the given name belongs to a {@link ClassInstanceCreation} and if so, returns
 * its constructor binding.
 * 
 * @param nameNode the name node
 * @return the constructor binding or <code>null</code> if not found
 * @since 3.7
 */
private IBinding getConstructorBindingIfAvailable(Name nameNode) {
	ASTNode type= ASTNodes.getNormalizedNode(nameNode);
	StructuralPropertyDescriptor loc= type.getLocationInParent();
	if (loc == ClassInstanceCreation.TYPE_PROPERTY) {
		return ((ClassInstanceCreation) type.getParent()).resolveConstructorBinding();
	}
	return null;
}
 
源代码8 项目: eclipse.jdt.ls   文件: ExtractTempRefactoring.java
private RefactoringStatus checkSelection(IProgressMonitor pm) throws JavaModelException {
	try {
		pm.beginTask("", 8); //$NON-NLS-1$

		IExpressionFragment selectedExpression = getSelectedExpression();

		if (selectedExpression == null) {
			String message = RefactoringCoreMessages.ExtractTempRefactoring_select_expression;
			return CodeRefactoringUtil.checkMethodSyntaxErrors(fSelectionStart, fSelectionLength, fCompilationUnitNode, message);
		}
		pm.worked(1);

		if (isUsedInExplicitConstructorCall()) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_explicit_constructor);
		}
		pm.worked(1);

		ASTNode associatedNode = selectedExpression.getAssociatedNode();
		if (getEnclosingBodyNode() == null || ASTNodes.getParent(associatedNode, Annotation.class) != null) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_expr_in_method_or_initializer);
		}
		pm.worked(1);

		if (associatedNode instanceof Name && associatedNode.getParent() instanceof ClassInstanceCreation && associatedNode.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_name_in_new);
		}
		pm.worked(1);

		RefactoringStatus result = new RefactoringStatus();
		result.merge(checkExpression());
		if (result.hasFatalError()) {
			return result;
		}
		pm.worked(1);

		result.merge(checkExpressionFragmentIsRValue());
		if (result.hasFatalError()) {
			return result;
		}
		pm.worked(1);

		if (isUsedInForInitializerOrUpdater(getSelectedExpression().getAssociatedExpression())) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_for_initializer_updater);
		}
		pm.worked(1);

		if (isReferringToLocalVariableFromFor(getSelectedExpression().getAssociatedExpression())) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_refers_to_for_variable);
		}
		pm.worked(1);

		return result;
	} finally {
		pm.done();
	}
}
 
源代码9 项目: eclipse.jdt.ls   文件: ExtractFieldRefactoring.java
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException {
	try {
		pm.beginTask("", 16); //$NON-NLS-1$

		RefactoringStatus result = Checks.validateModifiesFiles(ResourceUtil.getFiles(new ICompilationUnit[] { fCu }), getValidationContext(), pm);
		if (result.hasFatalError()) {
			return result;
		}

		if (fCompilationUnitNode == null) {
			fCompilationUnitNode = RefactoringASTParser.parseWithASTProvider(fCu, true, new SubProgressMonitor(pm, 3));
		}
		pm.worked(1);

		if (fCURewrite == null) {
			fCURewrite = new CompilationUnitRewrite(fCu, fCompilationUnitNode);
			fCURewrite.setFormattingOptions(fFormatterOptions);
			fCURewrite.getASTRewrite().setTargetSourceRangeComputer(new NoCommentSourceRangeComputer());
		}
		pm.worked(1);

		// Check the conditions for extracting an expression to a variable.
		IExpressionFragment selectedExpression = getSelectedExpression();
		if (selectedExpression == null) {
			String message = RefactoringCoreMessages.ExtractTempRefactoring_select_expression;
			return CodeRefactoringUtil.checkMethodSyntaxErrors(fSelectionStart, fSelectionLength, fCompilationUnitNode, message);
		}
		pm.worked(1);

		if (isUsedInExplicitConstructorCall()) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_explicit_constructor);
		}
		pm.worked(1);

		ASTNode associatedNode = selectedExpression.getAssociatedNode();
		if (getEnclosingBodyNode() == null || ASTNodes.getParent(associatedNode, Annotation.class) != null) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_expr_in_method_or_initializer);
		}
		pm.worked(1);

		if (associatedNode instanceof Name && associatedNode.getParent() instanceof ClassInstanceCreation && associatedNode.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_name_in_new);
		}
		pm.worked(1);

		result.merge(checkExpression());
		if (result.hasFatalError()) {
			return result;
		}
		pm.worked(1);

		result.merge(checkExpressionFragmentIsRValue());
		if (result.hasFatalError()) {
			return result;
		}
		pm.worked(1);

		Expression associatedExpression = selectedExpression.getAssociatedExpression();
		if (isUsedInForInitializerOrUpdater(associatedExpression)) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_for_initializer_updater);
		}
		pm.worked(1);

		if (isReferringToLocalVariableFromFor(associatedExpression)) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_refers_to_for_variable);
		}
		pm.worked(1);

		// Check the conditions for extracting an expression to field.
		ASTNode declaringType = getEnclosingTypeDeclaration();
		if (declaringType instanceof TypeDeclaration && ((TypeDeclaration) declaringType).isInterface()) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractFieldRefactoring_interface_methods);
		}
		pm.worked(1);

		result.merge(checkTempTypeForLocalTypeUsage());
		if (result.hasFatalError()) {
			return result;
		}
		pm.worked(1);

		checkTempInitializerForLocalTypeUsage();
		initializeDefaults();
		pm.worked(1);

		return result;
	} finally {
		pm.done();
	}
}
 
源代码10 项目: eclipse.jdt.ls   文件: RefactorProcessor.java
public static RefactoringCorrectionProposal getConvertAnonymousToNestedProposal(CodeActionParams params, IInvocationContext context, final ASTNode node, boolean returnAsCommand) throws CoreException {
	String label = CorrectionMessages.QuickAssistProcessor_convert_anonym_to_nested;
	ASTNode normalized = ASTNodes.getNormalizedNode(node);
	if (normalized.getLocationInParent() != ClassInstanceCreation.TYPE_PROPERTY) {
		return null;
	}

	final AnonymousClassDeclaration anonymTypeDecl = ((ClassInstanceCreation) normalized.getParent()).getAnonymousClassDeclaration();
	if (anonymTypeDecl == null || anonymTypeDecl.resolveBinding() == null) {
		return null;
	}

	final ConvertAnonymousToNestedRefactoring refactoring = new ConvertAnonymousToNestedRefactoring(anonymTypeDecl);
	if (!refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
		return null;
	}

	if (returnAsCommand) {
		return new RefactoringCorrectionCommandProposal(label, CodeActionKind.Refactor, context.getCompilationUnit(), IProposalRelevance.CONVERT_ANONYMOUS_TO_NESTED, RefactorProposalUtility.APPLY_REFACTORING_COMMAND_ID,
				Arrays.asList(CONVERT_ANONYMOUS_CLASS_TO_NESTED_COMMAND, params));
	}

	String extTypeName = ASTNodes.getSimpleNameIdentifier((Name) node);
	ITypeBinding anonymTypeBinding = anonymTypeDecl.resolveBinding();
	String className;
	if (anonymTypeBinding.getInterfaces().length == 0) {
		className = Messages.format(CorrectionMessages.QuickAssistProcessor_name_extension_from_interface, extTypeName);
	} else {
		className = Messages.format(CorrectionMessages.QuickAssistProcessor_name_extension_from_class, extTypeName);
	}
	String[][] existingTypes = ((IType) anonymTypeBinding.getJavaElement()).resolveType(className);
	int i = 1;
	while (existingTypes != null) {
		i++;
		existingTypes = ((IType) anonymTypeBinding.getJavaElement()).resolveType(className + i);
	}
	refactoring.setClassName(i == 1 ? className : className + i);

	LinkedProposalModelCore linkedProposalModel = new LinkedProposalModelCore();
	refactoring.setLinkedProposalModel(linkedProposalModel);

	final ICompilationUnit cu = context.getCompilationUnit();
	RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, CodeActionKind.Refactor, cu, refactoring, IProposalRelevance.CONVERT_ANONYMOUS_TO_NESTED);
	proposal.setLinkedProposalModel(linkedProposalModel);
	return proposal;

}
 
private RefactoringStatus checkSelection(IProgressMonitor pm) throws JavaModelException {
	try {
		pm.beginTask("", 8); //$NON-NLS-1$

		IExpressionFragment selectedExpression= getSelectedExpression();

		if (selectedExpression == null) {
			String message= RefactoringCoreMessages.ExtractTempRefactoring_select_expression;
			return CodeRefactoringUtil.checkMethodSyntaxErrors(fSelectionStart, fSelectionLength, fCompilationUnitNode, message);
		}
		pm.worked(1);

		if (isUsedInExplicitConstructorCall())
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_explicit_constructor);
		pm.worked(1);

		ASTNode associatedNode= selectedExpression.getAssociatedNode();
		if (getEnclosingBodyNode() == null || ASTNodes.getParent(associatedNode, Annotation.class) != null)
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_expr_in_method_or_initializer);
		pm.worked(1);

		if (associatedNode instanceof Name && associatedNode.getParent() instanceof ClassInstanceCreation && associatedNode.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY)
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_name_in_new);
		pm.worked(1);

		RefactoringStatus result= new RefactoringStatus();
		result.merge(checkExpression());
		if (result.hasFatalError())
			return result;
		pm.worked(1);

		result.merge(checkExpressionFragmentIsRValue());
		if (result.hasFatalError())
			return result;
		pm.worked(1);

		if (isUsedInForInitializerOrUpdater(getSelectedExpression().getAssociatedExpression()))
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_for_initializer_updater);
		pm.worked(1);

		if (isReferringToLocalVariableFromFor(getSelectedExpression().getAssociatedExpression()))
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_refers_to_for_variable);
		pm.worked(1);

		return result;
	} finally {
		pm.done();
	}
}
 
private static boolean getConvertAnonymousToNestedProposal(IInvocationContext context, final ASTNode node, Collection<ICommandAccess> proposals) throws CoreException {
	if (!(node instanceof Name))
		return false;

	ASTNode normalized= ASTNodes.getNormalizedNode(node);
	if (normalized.getLocationInParent() != ClassInstanceCreation.TYPE_PROPERTY)
		return false;

	final AnonymousClassDeclaration anonymTypeDecl= ((ClassInstanceCreation) normalized.getParent()).getAnonymousClassDeclaration();
	if (anonymTypeDecl == null || anonymTypeDecl.resolveBinding() == null) {
		return false;
	}

	if (proposals == null) {
		return true;
	}

	final ICompilationUnit cu= context.getCompilationUnit();
	final ConvertAnonymousToNestedRefactoring refactoring= new ConvertAnonymousToNestedRefactoring(anonymTypeDecl);
	
	String extTypeName= ASTNodes.getSimpleNameIdentifier((Name) node);
	ITypeBinding anonymTypeBinding= anonymTypeDecl.resolveBinding();
	String className;
	if (anonymTypeBinding.getInterfaces().length == 0) {
		className= Messages.format(CorrectionMessages.QuickAssistProcessor_name_extension_from_interface, extTypeName);
	} else {
		className= Messages.format(CorrectionMessages.QuickAssistProcessor_name_extension_from_class, extTypeName);
	}
	String[][] existingTypes= ((IType) anonymTypeBinding.getJavaElement()).resolveType(className);
	int i= 1;
	while (existingTypes != null) {
		i++;
		existingTypes= ((IType) anonymTypeBinding.getJavaElement()).resolveType(className + i);
	}
	refactoring.setClassName(i == 1 ? className : className + i);

	if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
		LinkedProposalModel linkedProposalModel= new LinkedProposalModel();
		refactoring.setLinkedProposalModel(linkedProposalModel);

		String label= CorrectionMessages.QuickAssistProcessor_convert_anonym_to_nested;
		Image image= JavaPlugin.getImageDescriptorRegistry().get(JavaElementImageProvider.getTypeImageDescriptor(true, false, Flags.AccPrivate, false));
		RefactoringCorrectionProposal proposal= new RefactoringCorrectionProposal(label, cu, refactoring, IProposalRelevance.CONVERT_ANONYMOUS_TO_NESTED, image);
		proposal.setLinkedProposalModel(linkedProposalModel);
		proposal.setCommandId(CONVERT_ANONYMOUS_TO_LOCAL_ID);
		proposals.add(proposal);
	}
	return false;
}