Java Code Examples for org.eclipse.jdt.core.dom.ClassInstanceCreation#TYPE_PROPERTY

The following examples show how to use org.eclipse.jdt.core.dom.ClassInstanceCreation#TYPE_PROPERTY . 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: IntroduceFactoryRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: JavadocHover.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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;
	}
}
 
Example 3
Source File: SemanticHighlightings.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 4
Source File: JDTUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
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;
	}
}
 
Example 5
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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;
}
 
Example 6
Source File: SemanticHighlightings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 7
Source File: CopyQualifiedNameAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 8
Source File: ExtractTempRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
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();
	}
}
 
Example 9
Source File: ExtractFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@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();
	}
}
 
Example 10
Source File: RefactorProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
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;

}
 
Example 11
Source File: ExtractTempRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
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();
	}
}
 
Example 12
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
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;
}