Java Code Examples for org.eclipse.jdt.internal.corext.dom.ASTNodes#asString()

The following examples show how to use org.eclipse.jdt.internal.corext.dom.ASTNodes#asString() . 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: ExtractTempRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private final ExtractLocalDescriptor createRefactoringDescriptor() {
	final Map<String, String> arguments = new HashMap<>();
	String project = null;
	IJavaProject javaProject = fCu.getJavaProject();
	if (javaProject != null) {
		project = javaProject.getElementName();
	}
	final String description = Messages.format(RefactoringCoreMessages.ExtractTempRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(fTempName));
	final String expression = ASTNodes.asString(fSelectedExpression.getAssociatedExpression());
	final String header = Messages.format(RefactoringCoreMessages.ExtractTempRefactoring_descriptor_description, new String[] { BasicElementLabels.getJavaElementName(fTempName), BasicElementLabels.getJavaCodeString(expression) });
	final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header);
	comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractTempRefactoring_name_pattern, BasicElementLabels.getJavaElementName(fTempName)));
	final BodyDeclaration decl = ASTNodes.getParent(fSelectedExpression.getAssociatedExpression(), BodyDeclaration.class);
	if (decl instanceof MethodDeclaration) {
		final IMethodBinding method = ((MethodDeclaration) decl).resolveBinding();
		final String label = method != null ? BindingLabelProviderCore.getBindingLabel(method, JavaElementLabels.ALL_FULLY_QUALIFIED) : BasicElementLabels.getJavaElementName('{' + JavaElementLabels.ELLIPSIS_STRING + '}');
		comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractTempRefactoring_destination_pattern, label));
	}
	comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractTempRefactoring_expression_pattern, BasicElementLabels.getJavaCodeString(expression)));
	if (fReplaceAllOccurrences) {
		comment.addSetting(RefactoringCoreMessages.ExtractTempRefactoring_replace_occurrences);
	}
	if (fDeclareFinal) {
		comment.addSetting(RefactoringCoreMessages.ExtractTempRefactoring_declare_final);
	}
	final ExtractLocalDescriptor descriptor = RefactoringSignatureDescriptorFactory.createExtractLocalDescriptor(project, description, comment.asString(), arguments, RefactoringDescriptor.NONE);
	arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fCu));
	arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME, fTempName);
	arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, new Integer(fSelectionStart).toString() + " " + new Integer(fSelectionLength).toString()); //$NON-NLS-1$
	arguments.put(ATTRIBUTE_REPLACE, Boolean.valueOf(fReplaceAllOccurrences).toString());
	arguments.put(ATTRIBUTE_FINAL, Boolean.valueOf(fDeclareFinal).toString());
	return descriptor;
}
 
Example 2
Source File: ExtractMethodRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private String getType(VariableDeclaration declaration, boolean isVarargs) {
	String type = ASTNodes.asString(ASTNodeFactory.newType(declaration.getAST(), declaration, fImportRewriter, new ContextSensitiveImportRewriteContext(declaration, fImportRewriter)));
	if (isVarargs) {
		return type + ParameterInfo.ELLIPSIS;
	} else {
		return type;
	}
}
 
Example 3
Source File: ExtractConstantRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private ExtractConstantDescriptor createRefactoringDescriptor() {
	final Map<String, String> arguments = new HashMap<>();
	String project = null;
	IJavaProject javaProject = fCu.getJavaProject();
	if (javaProject != null) {
		project = javaProject.getElementName();
	}
	int flags = JavaRefactoringDescriptor.JAR_REFACTORING | JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
	if (JdtFlags.getVisibilityCode(fVisibility) != Modifier.PRIVATE) {
		flags |= RefactoringDescriptor.STRUCTURAL_CHANGE;
	}

	final String expression = ASTNodes.asString(fSelectedExpression.getAssociatedExpression());
	final String description = Messages.format(RefactoringCoreMessages.ExtractConstantRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(fConstantName));
	final String header = Messages.format(RefactoringCoreMessages.ExtractConstantRefactoring_descriptor_description,
			new String[] { BasicElementLabels.getJavaElementName(fConstantName), BasicElementLabels.getJavaCodeString(expression) });
	final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header);
	comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractConstantRefactoring_constant_name_pattern, BasicElementLabels.getJavaElementName(fConstantName)));
	comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractConstantRefactoring_constant_expression_pattern, BasicElementLabels.getJavaCodeString(expression)));
	String visibility = fVisibility;
	if ("".equals(visibility)) {
		visibility = RefactoringCoreMessages.ExtractConstantRefactoring_default_visibility;
	}
	comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractConstantRefactoring_visibility_pattern, visibility));
	if (fReplaceAllOccurrences) {
		comment.addSetting(RefactoringCoreMessages.ExtractConstantRefactoring_replace_occurrences);
	}
	if (fQualifyReferencesWithDeclaringClassName) {
		comment.addSetting(RefactoringCoreMessages.ExtractConstantRefactoring_qualify_references);
	}
	arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fCu));
	arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME, fConstantName);
	arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, new Integer(fSelectionStart).toString() + " " + new Integer(fSelectionLength).toString()); //$NON-NLS-1$
	arguments.put(ATTRIBUTE_REPLACE, Boolean.valueOf(fReplaceAllOccurrences).toString());
	arguments.put(ATTRIBUTE_QUALIFY, Boolean.valueOf(fQualifyReferencesWithDeclaringClassName).toString());
	arguments.put(ATTRIBUTE_VISIBILITY, new Integer(JdtFlags.getVisibilityCode(fVisibility)).toString());

	ExtractConstantDescriptor descriptor = RefactoringSignatureDescriptorFactory.createExtractConstantDescriptor(project, description, comment.asString(), arguments, flags);
	return descriptor;
}
 
Example 4
Source File: SelfEncapsulateFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private String getTypeName(ASTNode type) {
	if (type instanceof AbstractTypeDeclaration) {
		return ((AbstractTypeDeclaration) type).getName().getIdentifier();
	} else if (type instanceof AnonymousClassDeclaration) {
		ClassInstanceCreation node = ASTNodes.getParent(type, ClassInstanceCreation.class);
		return ASTNodes.asString(node.getType());
	}
	Assert.isTrue(false, "Should not happen"); //$NON-NLS-1$
	return null;
}
 
Example 5
Source File: MoveInnerToTopRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void updateReferenceInImport(ImportDeclaration enclosingImport, ASTNode node, CompilationUnitRewrite rewrite) {
	final IBinding binding= enclosingImport.resolveBinding();
	if (binding instanceof ITypeBinding) {
		final ITypeBinding type= (ITypeBinding) binding;
		final ImportRewrite rewriter= rewrite.getImportRewrite();
		if (enclosingImport.isStatic()) {
			final String oldImport= ASTNodes.asString(node);
			final StringBuffer buffer= new StringBuffer(oldImport);
			final String typeName= fType.getDeclaringType().getElementName();
			final int index= buffer.indexOf(typeName);
			if (index >= 0) {
				buffer.delete(index, index + typeName.length() + 1);
				final String newImport= buffer.toString();
				if (enclosingImport.isOnDemand()) {
					rewriter.removeStaticImport(oldImport + ".*"); //$NON-NLS-1$
					rewriter.addStaticImport(newImport, "*", false); //$NON-NLS-1$
				} else {
					rewriter.removeStaticImport(oldImport);
					final int offset= newImport.lastIndexOf('.');
					if (offset >= 0 && offset < newImport.length() - 1) {
						rewriter.addStaticImport(newImport.substring(0, offset), newImport.substring(offset + 1), false);
					}
				}
			}
		} else
			rewriter.removeImport(type.getQualifiedName());
	}
}
 
Example 6
Source File: ExtractTempRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private final ExtractLocalDescriptor createRefactoringDescriptor() {
	final Map<String, String> arguments= new HashMap<String, String>();
	String project= null;
	IJavaProject javaProject= fCu.getJavaProject();
	if (javaProject != null)
		project= javaProject.getElementName();
	final String description= Messages.format(RefactoringCoreMessages.ExtractTempRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(fTempName));
	final String expression= ASTNodes.asString(fSelectedExpression.getAssociatedExpression());
	final String header= Messages.format(RefactoringCoreMessages.ExtractTempRefactoring_descriptor_description, new String[] { BasicElementLabels.getJavaElementName(fTempName), BasicElementLabels.getJavaCodeString(expression)});
	final JDTRefactoringDescriptorComment comment= new JDTRefactoringDescriptorComment(project, this, header);
	comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractTempRefactoring_name_pattern, BasicElementLabels.getJavaElementName(fTempName)));
	final BodyDeclaration decl= (BodyDeclaration) ASTNodes.getParent(fSelectedExpression.getAssociatedExpression(), BodyDeclaration.class);
	if (decl instanceof MethodDeclaration) {
		final IMethodBinding method= ((MethodDeclaration) decl).resolveBinding();
		final String label= method != null ? BindingLabelProvider.getBindingLabel(method, JavaElementLabels.ALL_FULLY_QUALIFIED) : BasicElementLabels.getJavaElementName('{' + JavaElementLabels.ELLIPSIS_STRING + '}');
		comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractTempRefactoring_destination_pattern, label));
	}
	comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractTempRefactoring_expression_pattern, BasicElementLabels.getJavaCodeString(expression)));
	if (fReplaceAllOccurrences)
		comment.addSetting(RefactoringCoreMessages.ExtractTempRefactoring_replace_occurrences);
	if (fDeclareFinal)
		comment.addSetting(RefactoringCoreMessages.ExtractTempRefactoring_declare_final);
	final ExtractLocalDescriptor descriptor= RefactoringSignatureDescriptorFactory.createExtractLocalDescriptor(project, description, comment.asString(), arguments, RefactoringDescriptor.NONE);
	arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fCu));
	arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME, fTempName);
	arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, new Integer(fSelectionStart).toString() + " " + new Integer(fSelectionLength).toString()); //$NON-NLS-1$
	arguments.put(ATTRIBUTE_REPLACE, Boolean.valueOf(fReplaceAllOccurrences).toString());
	arguments.put(ATTRIBUTE_FINAL, Boolean.valueOf(fDeclareFinal).toString());
	return descriptor;
}
 
Example 7
Source File: ExtractMethodRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the signature of the new method.
 *
 * @param methodName the method name used for the new method
 * @return the signature of the extracted method
 */
public String getSignature(String methodName) {
	MethodDeclaration methodDecl= createNewMethodDeclaration();
	methodDecl.setBody(null);
	String str= ASTNodes.asString(methodDecl);
	return str.substring(0, str.indexOf(';'));
}
 
Example 8
Source File: ExtractMethodRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private String getType(VariableDeclaration declaration, boolean isVarargs) {
	String type= ASTNodes.asString(ASTNodeFactory.newType(declaration.getAST(), declaration, fImportRewriter, new ContextSensitiveImportRewriteContext(declaration, fImportRewriter)));
	if (isVarargs)
		return type + ParameterInfo.ELLIPSIS;
	else
		return type;
}
 
Example 9
Source File: ExtractConstantRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private ExtractConstantDescriptor createRefactoringDescriptor() {
	final Map<String, String> arguments= new HashMap<String, String>();
	String project= null;
	IJavaProject javaProject= fCu.getJavaProject();
	if (javaProject != null)
		project= javaProject.getElementName();
	int flags= JavaRefactoringDescriptor.JAR_REFACTORING | JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
	if (JdtFlags.getVisibilityCode(fVisibility) != Modifier.PRIVATE)
		flags|= RefactoringDescriptor.STRUCTURAL_CHANGE;

	final String expression= ASTNodes.asString(fSelectedExpression.getAssociatedExpression());
	final String description= Messages.format(RefactoringCoreMessages.ExtractConstantRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(fConstantName));
	final String header= Messages.format(RefactoringCoreMessages.ExtractConstantRefactoring_descriptor_description, new String[] { BasicElementLabels.getJavaElementName(fConstantName), BasicElementLabels.getJavaCodeString(expression)});
	final JDTRefactoringDescriptorComment comment= new JDTRefactoringDescriptorComment(project, this, header);
	comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractConstantRefactoring_constant_name_pattern, BasicElementLabels.getJavaElementName(fConstantName)));
	comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractConstantRefactoring_constant_expression_pattern, BasicElementLabels.getJavaCodeString(expression)));
	String visibility= fVisibility;
	if ("".equals(visibility)) //$NON-NLS-1$
		visibility= RefactoringCoreMessages.ExtractConstantRefactoring_default_visibility;
	comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractConstantRefactoring_visibility_pattern, visibility));
	if (fReplaceAllOccurrences)
		comment.addSetting(RefactoringCoreMessages.ExtractConstantRefactoring_replace_occurrences);
	if (fQualifyReferencesWithDeclaringClassName)
		comment.addSetting(RefactoringCoreMessages.ExtractConstantRefactoring_qualify_references);
	arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fCu));
	arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME, fConstantName);
	arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, new Integer(fSelectionStart).toString() + " " + new Integer(fSelectionLength).toString()); //$NON-NLS-1$
	arguments.put(ATTRIBUTE_REPLACE, Boolean.valueOf(fReplaceAllOccurrences).toString());
	arguments.put(ATTRIBUTE_QUALIFY, Boolean.valueOf(fQualifyReferencesWithDeclaringClassName).toString());
	arguments.put(ATTRIBUTE_VISIBILITY, new Integer(JdtFlags.getVisibilityCode(fVisibility)).toString());

	ExtractConstantDescriptor descriptor= RefactoringSignatureDescriptorFactory.createExtractConstantDescriptor(project, description, comment.asString(), arguments, flags);
	return descriptor;
}
 
Example 10
Source File: SelfEncapsulateFieldRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private String getTypeName(ASTNode type) {
	if (type instanceof AbstractTypeDeclaration) {
		return ((AbstractTypeDeclaration)type).getName().getIdentifier();
	} else if (type instanceof AnonymousClassDeclaration) {
		ClassInstanceCreation node= (ClassInstanceCreation)ASTNodes.getParent(type, ClassInstanceCreation.class);
		return ASTNodes.asString(node.getType());
	}
	Assert.isTrue(false, "Should not happen"); //$NON-NLS-1$
	return null;
}
 
Example 11
Source File: ImplementOccurrencesFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public String getElementName() {
	if (fSelectedNode != null) {
		return ASTNodes.asString(fSelectedNode);
	}
	return null;
}
 
Example 12
Source File: BreakContinueTargetFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public String getElementName() {
	return ASTNodes.asString(fSelected);
}
 
Example 13
Source File: OccurrencesFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public String getElementName() {
	if (fSelectedNode != null) {
		return ASTNodes.asString(fSelectedNode);
	}
	return null;
}
 
Example 14
Source File: ExceptionOccurrencesFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public String getElementName() {
	if (fSelectedNode != null) {
		return ASTNodes.asString(fSelectedNode);
	}
	return null;
}
 
Example 15
Source File: SortMembersOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private String buildSignature(Type type) {
	return ASTNodes.asString(type);
}
 
Example 16
Source File: ExtractConstantRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private String getConstantTypeName() throws JavaModelException {
	return ASTNodes.asString(getConstantType());
}
 
Example 17
Source File: ExtractConstantRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private String getConstantTypeName() throws JavaModelException {
	return ASTNodes.asString(getConstantType());
}
 
Example 18
Source File: ExtractMethodRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Returns the signature of the new method.
 *
 * @param methodName
 *            the method name used for the new method
 * @return the signature of the extracted method
 */
public String getSignature(String methodName) {
	MethodDeclaration methodDecl = createNewMethodDeclaration();
	methodDecl.setBody(null);
	String str = ASTNodes.asString(methodDecl);
	return str.substring(0, str.indexOf(';'));
}