org.eclipse.jdt.core.dom.ASTNode#INITIALIZER源码实例Demo

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

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$
	}
}
 
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;
}
 
源代码3 项目: eclipse.jdt.ls   文件: LocalVariableIndex.java
/**
 * 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());
	}
}
 
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;
	}
}
 
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()));
	}
}
 
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;
	}
}
 
/**
 * 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());
	}
}
 
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;
	}
}
 
/**
 * 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;
}
 
/**
 * 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;
}
 
@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);
}
 
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);

	}
}
 
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;
}