Java Code Examples for org.eclipse.jdt.core.dom.ASTNode#INITIALIZER

The following examples show how to use org.eclipse.jdt.core.dom.ASTNode#INITIALIZER . 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: CallInliner.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void flowAnalysis() {
	fInvocationScope= fRootScope.findScope(fTargetNode.getStartPosition(), fTargetNode.getLength());
	fInvocationScope.setCursor(fTargetNode.getStartPosition());
	fFlowContext= new FlowContext(0, fNumberOfLocals + 1);
	fFlowContext.setConsiderAccessMode(true);
	fFlowContext.setComputeMode(FlowContext.ARGUMENTS);
	Selection selection= Selection.createFromStartLength(fInvocation.getStartPosition(), fInvocation.getLength());
	switch (fBodyDeclaration.getNodeType()) {
		case ASTNode.INITIALIZER:
		case ASTNode.FIELD_DECLARATION:
		case ASTNode.METHOD_DECLARATION:
		case ASTNode.ENUM_CONSTANT_DECLARATION:
			fFlowInfo= new InputFlowAnalyzer(fFlowContext, selection, true).perform(fBodyDeclaration);
			break;
		default:
			Assert.isTrue(false, "Should not happen");			 //$NON-NLS-1$
	}
}
 
Example 2
Source File: JavaTextSelection.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public boolean resolveInClassInitializer() {
	if (fInClassInitializerRequested)
		return fInClassInitializer;
	fInClassInitializerRequested= true;
	resolveSelectedNodes();
	ASTNode node= getStartNode();
	if (node == null) {
		fInClassInitializer= true;
	} else {
		while (node != null) {
			int nodeType= node.getNodeType();
			if (node instanceof AbstractTypeDeclaration) {
				fInClassInitializer= false;
				break;
			} else if (nodeType == ASTNode.ANONYMOUS_CLASS_DECLARATION) {
				fInClassInitializer= false;
				break;
			} else if (nodeType == ASTNode.INITIALIZER) {
				fInClassInitializer= true;
				break;
			}
			node= node.getParent();
		}
	}
	return fInClassInitializer;
}
 
Example 3
Source File: LocalVariableIndex.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Computes the maximum number of local variable declarations in the given
 * body declaration.
 *
 * @param declaration
 *            the body declaration. Must either be a method declaration, or
 *            an initializer, or a field declaration.
 * @return the maximum number of local variables
 */
public static int perform(BodyDeclaration declaration) {
	Assert.isTrue(declaration != null);
	switch (declaration.getNodeType()) {
		case ASTNode.METHOD_DECLARATION:
		case ASTNode.FIELD_DECLARATION:
		case ASTNode.INITIALIZER:
			return internalPerform(declaration);
		default:
			throw new IllegalArgumentException(declaration.toString());
	}
}
 
Example 4
Source File: CallInliner.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void initialize(BodyDeclaration declaration) {
	fBodyDeclaration= declaration;
	fRootScope= CodeScopeBuilder.perform(declaration, fSourceProvider.getDeclaration().resolveBinding());
	fNumberOfLocals= 0;
	switch (declaration.getNodeType()) {
		case ASTNode.METHOD_DECLARATION:
		case ASTNode.INITIALIZER:
			fNumberOfLocals= LocalVariableIndex.perform(declaration);
			break;
	}
}
 
Example 5
Source File: PasteAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static void insertToType(ASTRewrite rewrite, ASTNode node, AbstractTypeDeclaration typeDeclaration) {
	switch (node.getNodeType()) {
		case ASTNode.ANNOTATION_TYPE_DECLARATION:
		case ASTNode.ENUM_DECLARATION:
		case ASTNode.TYPE_DECLARATION:
		case ASTNode.METHOD_DECLARATION:
		case ASTNode.FIELD_DECLARATION:
		case ASTNode.INITIALIZER:
			rewrite.getListRewrite(typeDeclaration, typeDeclaration.getBodyDeclarationsProperty()).insertAt(node, ASTNodes.getInsertionIndex((BodyDeclaration) node, typeDeclaration.bodyDeclarations()), null);
			break;
		default:
			Assert.isTrue(false, String.valueOf(node.getNodeType()));
	}
}
 
Example 6
Source File: ASTNodes.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static int getOrderPreference(BodyDeclaration member, MembersOrderPreferenceCache store) {
	int memberType= member.getNodeType();
	int modifiers= member.getModifiers();

	switch (memberType) {
		case ASTNode.TYPE_DECLARATION:
		case ASTNode.ENUM_DECLARATION :
		case ASTNode.ANNOTATION_TYPE_DECLARATION :
			return store.getCategoryIndex(MembersOrderPreferenceCache.TYPE_INDEX) * 2;
		case ASTNode.FIELD_DECLARATION:
			if (Modifier.isStatic(modifiers)) {
				int index= store.getCategoryIndex(MembersOrderPreferenceCache.STATIC_FIELDS_INDEX) * 2;
				if (Modifier.isFinal(modifiers)) {
					return index; // first final static, then static
				}
				return index + 1;
			}
			return store.getCategoryIndex(MembersOrderPreferenceCache.FIELDS_INDEX) * 2;
		case ASTNode.INITIALIZER:
			if (Modifier.isStatic(modifiers)) {
				return store.getCategoryIndex(MembersOrderPreferenceCache.STATIC_INIT_INDEX) * 2;
			}
			return store.getCategoryIndex(MembersOrderPreferenceCache.INIT_INDEX) * 2;
		case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
			return store.getCategoryIndex(MembersOrderPreferenceCache.METHOD_INDEX) * 2;
		case ASTNode.METHOD_DECLARATION:
			if (Modifier.isStatic(modifiers)) {
				return store.getCategoryIndex(MembersOrderPreferenceCache.STATIC_METHODS_INDEX) * 2;
			}
			if (((MethodDeclaration) member).isConstructor()) {
				return store.getCategoryIndex(MembersOrderPreferenceCache.CONSTRUCTORS_INDEX) * 2;
			}
			return store.getCategoryIndex(MembersOrderPreferenceCache.METHOD_INDEX) * 2;
		default:
			return 100;
	}
}
 
Example 7
Source File: LocalVariableIndex.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Computes the maximum number of local variable declarations in the
 * given body declaration.
 *
 * @param declaration the body declaration. Must either be a method
 *  declaration, or an initializer, or a field declaration.
 * @return the maximum number of local variables
 */
public static int perform(BodyDeclaration declaration) {
	Assert.isTrue(declaration != null);
	switch (declaration.getNodeType()) {
		case ASTNode.METHOD_DECLARATION:
		case ASTNode.FIELD_DECLARATION:
		case ASTNode.INITIALIZER:
			return internalPerform(declaration);
		default:
			throw new IllegalArgumentException(declaration.toString());
	}
}
 
Example 8
Source File: SortMembersOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isSortPreserved(BodyDeclaration bodyDeclaration) {
	switch (bodyDeclaration.getNodeType()) {
		case ASTNode.FIELD_DECLARATION:
		case ASTNode.ENUM_CONSTANT_DECLARATION:
		case ASTNode.INITIALIZER:
			return true;
		default:
			return false;
	}
}
 
Example 9
Source File: JavaTextBufferNode.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the corresponding place holder type for the given element.
 * @return a place holder type (see ASTRewrite) or -1 if there is no corresponding placeholder
 */
static final int getPlaceHolderType(ITypedElement element) {

	if (element instanceof DocumentRangeNode) {
		JavaNode jn= (JavaNode) element;
		switch (jn.getTypeCode()) {

		case JavaNode.PACKAGE:
		    return ASTNode.PACKAGE_DECLARATION;

		case JavaNode.CLASS:
		case JavaNode.INTERFACE:
			return ASTNode.TYPE_DECLARATION;

		case JavaNode.ENUM:
			return ASTNode.ENUM_DECLARATION;

		case JavaNode.ANNOTATION:
			return ASTNode.ANNOTATION_TYPE_DECLARATION;

		case JavaNode.CONSTRUCTOR:
		case JavaNode.METHOD:
			return ASTNode.METHOD_DECLARATION;

		case JavaNode.FIELD:
			return ASTNode.FIELD_DECLARATION;

		case JavaNode.INIT:
			return ASTNode.INITIALIZER;

		case JavaNode.IMPORT:
		case JavaNode.IMPORT_CONTAINER:
			return ASTNode.IMPORT_DECLARATION;

		case JavaNode.CU:
		    return ASTNode.COMPILATION_UNIT;
		}
	}
	return -1;
}
 
Example 10
Source File: JavaAddElementFromHistoryImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the corresponding place holder type for the given element.
 * @return a place holder type (see ASTRewrite) or -1 if there is no corresponding placeholder
 */
private int getPlaceHolderType(ITypedElement element) {

	if (element instanceof DocumentRangeNode) {
		JavaNode jn= (JavaNode) element;
		switch (jn.getTypeCode()) {

		case JavaNode.PACKAGE:
		    return ASTNode.PACKAGE_DECLARATION;

		case JavaNode.CLASS:
		case JavaNode.INTERFACE:
			return ASTNode.TYPE_DECLARATION;

		case JavaNode.ENUM:
			return ASTNode.ENUM_DECLARATION;

		case JavaNode.ANNOTATION:
			return ASTNode.ANNOTATION_TYPE_DECLARATION;

		case JavaNode.CONSTRUCTOR:
		case JavaNode.METHOD:
			return ASTNode.METHOD_DECLARATION;

		case JavaNode.FIELD:
			return ASTNode.FIELD_DECLARATION;

		case JavaNode.INIT:
			return ASTNode.INITIALIZER;

		case JavaNode.IMPORT:
		case JavaNode.IMPORT_CONTAINER:
			return ASTNode.IMPORT_DECLARATION;

		case JavaNode.CU:
		    return ASTNode.COMPILATION_UNIT;
		}
	}
	return -1;
}
 
Example 11
Source File: ExtractMethodAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void endVisit(CompilationUnit node) {
	RefactoringStatus status= getStatus();
	superCall: {
		if (status.hasFatalError())
			break superCall;
		if (!hasSelectedNodes()) {
			ASTNode coveringNode= getLastCoveringNode();
			if (coveringNode instanceof Block && coveringNode.getParent() instanceof MethodDeclaration) {
				MethodDeclaration methodDecl= (MethodDeclaration)coveringNode.getParent();
				Message[] messages= ASTNodes.getMessages(methodDecl, ASTNodes.NODE_ONLY);
				if (messages.length > 0) {
					status.addFatalError(Messages.format(
						RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors,
						BasicElementLabels.getJavaElementName(methodDecl.getName().getIdentifier())), JavaStatusContext.create(fCUnit, methodDecl));
					break superCall;
				}
			}
			status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
			break superCall;
		}
		fEnclosingBodyDeclaration= (BodyDeclaration)ASTNodes.getParent(getFirstSelectedNode(), BodyDeclaration.class);
		if (fEnclosingBodyDeclaration == null ||
				(fEnclosingBodyDeclaration.getNodeType() != ASTNode.METHOD_DECLARATION &&
				 fEnclosingBodyDeclaration.getNodeType() != ASTNode.FIELD_DECLARATION &&
				 fEnclosingBodyDeclaration.getNodeType() != ASTNode.INITIALIZER)) {
			status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
			break superCall;
		} else if (ASTNodes.getEnclosingType(fEnclosingBodyDeclaration) == null) {
			status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors_no_parent_binding);
			break superCall;
		} else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
			fEnclosingMethodBinding= ((MethodDeclaration)fEnclosingBodyDeclaration).resolveBinding();
		}
		if (!isSingleExpressionOrStatementSet()) {
			status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_single_expression_or_set);
			break superCall;
		}
		if (isExpressionSelected()) {
			ASTNode expression= getFirstSelectedNode();
			if (expression instanceof Name) {
				Name name= (Name)expression;
				if (name.resolveBinding() instanceof ITypeBinding) {
					status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_type_reference);
					break superCall;
				}
				if (name.resolveBinding() instanceof IMethodBinding) {
					status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_method_name_reference);
					break superCall;
				}
				if (name.resolveBinding() instanceof IVariableBinding) {
					StructuralPropertyDescriptor locationInParent= name.getLocationInParent();
					if (locationInParent == QualifiedName.NAME_PROPERTY || (locationInParent == FieldAccess.NAME_PROPERTY && !(((FieldAccess) name.getParent()).getExpression() instanceof ThisExpression)))  {
						status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_part_of_qualified_name);
						break superCall;
					}
				}
				if (name.isSimpleName() && ((SimpleName)name).isDeclaration()) {
					status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_name_in_declaration);
					break superCall;
				}
			}
			fForceStatic=
				ASTNodes.getParent(expression, ASTNode.SUPER_CONSTRUCTOR_INVOCATION) != null ||
				ASTNodes.getParent(expression, ASTNode.CONSTRUCTOR_INVOCATION) != null;
		}
		status.merge(LocalTypeAnalyzer.perform(fEnclosingBodyDeclaration, getSelection()));
		computeLastStatementSelected();
	}
	super.endVisit(node);
}
 
Example 12
Source File: JavaReplaceWithEditionActionImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void performReplace(IMember input, IFile file,
		ITextFileBuffer textFileBuffer, IDocument document, ITypedElement ti)
		throws CoreException, JavaModelException,
		InvocationTargetException, InterruptedException {

	if (ti instanceof IStreamContentAccessor) {

		boolean inEditor= beingEdited(file);

		String content= JavaCompareUtilities.readString((IStreamContentAccessor)ti);
		String newContent= trimTextBlock(content, TextUtilities.getDefaultLineDelimiter(document), input.getJavaProject());
		if (newContent == null) {
			showError();
			return;
		}

		ICompilationUnit compilationUnit= input.getCompilationUnit();
		CompilationUnit root= parsePartialCompilationUnit(compilationUnit);


		ISourceRange nameRange= input.getNameRange();
		if (nameRange == null)
			nameRange= input.getSourceRange();
		// workaround for bug in getNameRange(): for AnnotationMembers length is negative
		int length= nameRange.getLength();
		if (length < 0)
			length= 1;
		ASTNode node2= NodeFinder.perform(root, new SourceRange(nameRange.getOffset(), length));
		ASTNode node;
		if (node2.getNodeType() == ASTNode.INITIALIZER)
			node= node2;
		else
			node= ASTNodes.getParent(node2, BodyDeclaration.class);
		if (node == null)
			node= ASTNodes.getParent(node2, AnnotationTypeDeclaration.class);
		if (node == null)
			node= ASTNodes.getParent(node2, EnumDeclaration.class);

		//ASTNode node= getBodyContainer(root, input);
		if (node == null) {
			showError();
			return;
		}

		ASTRewrite rewriter= ASTRewrite.create(root.getAST());
		rewriter.replace(node, rewriter.createStringPlaceholder(newContent, node.getNodeType()), null);

		if (inEditor) {
			JavaEditor je= getEditor(file);
			if (je != null)
				je.setFocus();
		}

		Map<String, String> options= null;
		IJavaProject javaProject= compilationUnit.getJavaProject();
		if (javaProject != null)
			options= javaProject.getOptions(true);
		applyChanges(rewriter, document, textFileBuffer, getShell(), inEditor, options);

	}
}
 
Example 13
Source File: CreateInitializerOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected ASTNode generateElementAST(ASTRewrite rewriter, ICompilationUnit cu) throws JavaModelException {
	ASTNode node = super.generateElementAST(rewriter, cu);
	if (node.getNodeType() != ASTNode.INITIALIZER)
		throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
	return node;
}