Java Code Examples for org.eclipse.jdt.core.dom.StringLiteral#setLiteralValue()

The following examples show how to use org.eclipse.jdt.core.dom.StringLiteral#setLiteralValue() . 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: StringFormatGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void complete() throws CoreException {
	super.complete();
	ReturnStatement rStatement= fAst.newReturnStatement();
	String formatClass;
	if (getContext().is50orHigher())
		formatClass= "java.lang.String"; //$NON-NLS-1$
	else
		formatClass= "java.text.MessageFormat"; //$NON-NLS-1$
	MethodInvocation formatInvocation= createMethodInvocation(addImport(formatClass), "format", null); //$NON-NLS-1$ 
	StringLiteral literal= fAst.newStringLiteral();
	literal.setLiteralValue(buffer.toString());
	formatInvocation.arguments().add(literal);
	if (getContext().is50orHigher()) {
		formatInvocation.arguments().addAll(arguments);
	} else {
		ArrayCreation arrayCreation= fAst.newArrayCreation();
		arrayCreation.setType(fAst.newArrayType(fAst.newSimpleType(addImport("java.lang.Object")))); //$NON-NLS-1$
		ArrayInitializer initializer= fAst.newArrayInitializer();
		arrayCreation.setInitializer(initializer);
		initializer.expressions().addAll(arguments);
		formatInvocation.arguments().add(arrayCreation);
	}
	rStatement.setExpression(formatInvocation);
	toStringMethod.getBody().statements().add(rStatement);
}
 
Example 2
Source File: EnsuresPredicateFix.java    From CogniCrypt with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * This method builds a {@link ClassInstanceCreation} object (i.e. "new Ensuerer(varName, predicate")
 * 
 * @param ast
 * @param varName
 * @param predicate
 * @return
 */
private ClassInstanceCreation createEnsurerClassInstance(final AST ast, String varName, String predicate) {
	final ClassInstanceCreation classInstance = ast.newClassInstanceCreation();
	classInstance.setType(createAstType(INJAR_CLASS_NAME, ast));
	final StringLiteral literalPredicate = ast.newStringLiteral();
	literalPredicate.setLiteralValue(predicate);
	classInstance.arguments().add(ast.newSimpleName(varName));
	classInstance.arguments().add(literalPredicate);

	return classInstance;
}
 
Example 3
Source File: GeneratedAnnotationPopulator.java    From SparkBuilderGenerator with MIT License 5 votes vote down vote up
private SingleMemberAnnotation createGeneratedAnnotation(AST ast) {
    SingleMemberAnnotation generatedAnnotation = ast.newSingleMemberAnnotation();
    generatedAnnotation.setTypeName(ast.newSimpleName("Generated"));
    StringLiteral annotationValue = ast.newStringLiteral();
    annotationValue.setLiteralValue(StaticPreferences.PLUGIN_GENERATED_ANNOTATION_NAME);
    generatedAnnotation.setValue(annotationValue);
    return generatedAnnotation;
}
 
Example 4
Source File: LocalCorrectionsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static ThrowStatement getThrowForUnsupportedCase(Expression switchExpr, AST ast, ASTRewrite astRewrite) {
	ThrowStatement newThrowStatement = ast.newThrowStatement();
	ClassInstanceCreation newCic = ast.newClassInstanceCreation();
	newCic.setType(ast.newSimpleType(ast.newSimpleName("UnsupportedOperationException"))); //$NON-NLS-1$
	InfixExpression newInfixExpr = ast.newInfixExpression();
	StringLiteral newStringLiteral = ast.newStringLiteral();
	newStringLiteral.setLiteralValue("Unimplemented case: "); //$NON-NLS-1$
	newInfixExpr.setLeftOperand(newStringLiteral);
	newInfixExpr.setOperator(InfixExpression.Operator.PLUS);
	newInfixExpr.setRightOperand((Expression) astRewrite.createCopyTarget(switchExpr));
	newCic.arguments().add(newInfixExpr);
	newThrowStatement.setExpression(newCic);
	return newThrowStatement;
}
 
Example 5
Source File: LocalCorrectionsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static ThrowStatement getThrowForUnexpectedDefault(Expression switchExpression, AST ast, ASTRewrite astRewrite) {
	ThrowStatement newThrowStatement = ast.newThrowStatement();
	ClassInstanceCreation newCic = ast.newClassInstanceCreation();
	newCic.setType(ast.newSimpleType(ast.newSimpleName("IllegalArgumentException"))); //$NON-NLS-1$
	InfixExpression newInfixExpr = ast.newInfixExpression();
	StringLiteral newStringLiteral = ast.newStringLiteral();
	newStringLiteral.setLiteralValue("Unexpected value: "); //$NON-NLS-1$
	newInfixExpr.setLeftOperand(newStringLiteral);
	newInfixExpr.setOperator(InfixExpression.Operator.PLUS);
	newInfixExpr.setRightOperand((Expression) astRewrite.createCopyTarget(switchExpression));
	newCic.arguments().add(newInfixExpr);
	newThrowStatement.setExpression(newCic);
	return newThrowStatement;
}
 
Example 6
Source File: ClientBundleResource.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
public MethodDeclaration createMethodDeclaration(IType clientBundle, ASTRewrite astRewrite,
    ImportRewrite importRewrite, boolean addComments) throws CoreException {
  AST ast = astRewrite.getAST();
  MethodDeclaration methodDecl = ast.newMethodDeclaration();

  // Method is named after the resource it accesses
  methodDecl.setName(ast.newSimpleName(getMethodName()));

  // Method return type is a ResourcePrototype subtype
  ITypeBinding resourceTypeBinding = JavaASTUtils.resolveType(clientBundle.getJavaProject(), getReturnTypeName());
  Type resourceType = importRewrite.addImport(resourceTypeBinding, ast);
  methodDecl.setReturnType2(resourceType);

  // Add @Source annotation if necessary
  String sourceAnnotationValue = getSourceAnnotationValue(clientBundle);
  if (sourceAnnotationValue != null) {
    // Build the annotation
    SingleMemberAnnotation sourceAnnotation = ast.newSingleMemberAnnotation();
    sourceAnnotation.setTypeName(ast.newName("Source"));
    StringLiteral annotationValue = ast.newStringLiteral();
    annotationValue.setLiteralValue(sourceAnnotationValue);
    sourceAnnotation.setValue(annotationValue);

    // Add the annotation to the method
    ChildListPropertyDescriptor modifiers = methodDecl.getModifiersProperty();
    ListRewrite modifiersRewriter = astRewrite.getListRewrite(methodDecl, modifiers);
    modifiersRewriter.insertFirst(sourceAnnotation, null);
  }

  return methodDecl;
}
 
Example 7
Source File: StringConcatenationGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void flushBuffer() {
	if (buffer.length() > 0) {
		StringLiteral bufferedStringLiteral= fAst.newStringLiteral();
		bufferedStringLiteral.setLiteralValue(buffer.toString());
		buffer.setLength(0);
		expression= createSumExpression(expression, bufferedStringLiteral);
	}
}
 
Example 8
Source File: StringConcatenationGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void addMemberCheckNull(Object member, boolean addSeparator) {
	ConditionalExpression cExpression= fAst.newConditionalExpression();

	// member != null ?
	InfixExpression infExpression= fAst.newInfixExpression();
	infExpression.setLeftOperand(createMemberAccessExpression(member, true, true));
	infExpression.setRightOperand(fAst.newNullLiteral());
	infExpression.setOperator(Operator.NOT_EQUALS);
	cExpression.setExpression(infExpression);

	SumExpressionBuilder builder= new SumExpressionBuilder(null);
	String[] arrayString= getContext().getTemplateParser().getBody();
	for (int i= 0; i < arrayString.length; i++) {
		addElement(processElement(arrayString[i], member), builder);
	}
	if (addSeparator)
		addElement(getContext().getTemplateParser().getSeparator(), builder);
	cExpression.setThenExpression(builder.getExpression());

	StringLiteral literal= fAst.newStringLiteral();
	literal.setLiteralValue(getContext().isSkipNulls() ? "" : "null"); //$NON-NLS-1$ //$NON-NLS-2$
	cExpression.setElseExpression(literal);

	ParenthesizedExpression pExpression= fAst.newParenthesizedExpression();
	pExpression.setExpression(cExpression);
	toStringExpressionBuilder.addExpression(pExpression);
}
 
Example 9
Source File: StringBuilderGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected void flushBuffer(Block target) {
	if (fBuffer.length() > 0) {
		StringLiteral literal= fAst.newStringLiteral();
		literal.setLiteralValue(fBuffer.toString());
		if (target == null)
			target= toStringMethod.getBody();
		target.statements().add(fAst.newExpressionStatement(createMethodInvocation(fBuilderVariableName, APPEND_METHOD_NAME, literal)));
		fBuffer.setLength(0);
	}
}
 
Example 10
Source File: StringBuilderChainGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected void flushBuffer() {
	if (fBuffer.length() > 0) {
		if (temporaryBlock == null)
			temporaryBlock= toStringMethod.getBody();
		StringLiteral literal= fAst.newStringLiteral();
		literal.setLiteralValue(fBuffer.toString());
		appendExpression(literal);
		fBuffer.setLength(0);
	}
}
 
Example 11
Source File: SuppressWarningsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Adds a proposal to correct the name of the SuppressWarning annotation
 * @param context the context
 * @param problem the problem
 * @param proposals the resulting proposals
 */
public static void addUnknownSuppressWarningProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {

	ASTNode coveringNode= context.getCoveringNode();
	if (!(coveringNode instanceof StringLiteral))
		return;

	AST ast= coveringNode.getAST();
	StringLiteral literal= (StringLiteral) coveringNode;

	String literalValue= literal.getLiteralValue();
	String[] allWarningTokens= CorrectionEngine.getAllWarningTokens();
	for (int i= 0; i < allWarningTokens.length; i++) {
		String curr= allWarningTokens[i];
		if (NameMatcher.isSimilarName(literalValue, curr)) {
			StringLiteral newLiteral= ast.newStringLiteral();
			newLiteral.setLiteralValue(curr);
			ASTRewrite rewrite= ASTRewrite.create(ast);
			rewrite.replace(literal, newLiteral, null);
			String label= Messages.format(CorrectionMessages.SuppressWarningsSubProcessor_fix_suppress_token_label, new String[] { curr });
			Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
			ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.FIX_SUPPRESS_TOKEN, image);
			proposals.add(proposal);
		}
	}
	addRemoveUnusedSuppressWarningProposals(context, problem, proposals);
}
 
Example 12
Source File: BaseTranslator.java    From junion with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Expression returnString(String val) {
	StringLiteral str = ast.newStringLiteral();
	str.setLiteralValue(val);
	str.setProperty(TYPEBIND_PROP, StructCache.FieldType.OBJECT);
	return str;
}
 
Example 13
Source File: GenStatement.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
public static Statement genPrinter(String message) {
	StringLiteral stringLiteral = ast.newStringLiteral();
	stringLiteral.setLiteralValue(message);
	return genPrinter(stringLiteral);
}
 
Example 14
Source File: JsonPOJOBuilderAdderFragment.java    From SparkBuilderGenerator with MIT License 4 votes vote down vote up
private Expression createStringLitereal(AST ast, String literalValue) {
    StringLiteral stringLiteral = ast.newStringLiteral();
    stringLiteral.setLiteralValue(literalValue);
    return stringLiteral;
}
 
Example 15
Source File: CustomBuilderGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private MethodInvocation createAppendMethodForMember(Object member) {
	ITypeBinding memberType= getMemberType(member);
	String memberTypeName= memberType.getQualifiedName();

	Expression memberAccessExpression= null;

	AppendMethodInformation ami= appendMethodSpecificTypes.get(memberTypeName);
	if (ami == null && memberType.isPrimitive()) {
		memberTypeName= wrapperTypes[primitiveTypes.indexOf(memberTypeName)];
		memberType= fAst.resolveWellKnownType(memberTypeName);
		ami= appendMethodSpecificTypes.get(memberTypeName);
		if (!getContext().is50orHigher()) {
			ClassInstanceCreation classInstance= fAst.newClassInstanceCreation();
			classInstance.setType(fAst.newSimpleType(addImport(memberTypeName)));
			classInstance.arguments().add(createMemberAccessExpression(member, true, true));
			memberAccessExpression= classInstance;
		}
	}
	while (ami == null) {
		memberType= memberType.getSuperclass();
		if (memberType != null)
			memberTypeName= memberType.getQualifiedName();
		else
			memberTypeName= "java.lang.Object"; //$NON-NLS-1$
		ami= appendMethodSpecificTypes.get(memberTypeName);
	}

	if (memberAccessExpression == null) {
		memberAccessExpression= createMemberAccessExpression(member, false, getContext().isSkipNulls());
	}

	MethodInvocation appendInvocation= fAst.newMethodInvocation();
	appendInvocation.setName(fAst.newSimpleName(getContext().getCustomBuilderAppendMethod()));
	if (ami.methodType == 1 || ami.methodType == 2) {
		appendInvocation.arguments().add(memberAccessExpression);
	}
	if (ami.methodType == 2 || ami.methodType == 3) {
		StringLiteral literal= fAst.newStringLiteral();
		literal.setLiteralValue(getMemberName(member, ToStringTemplateParser.MEMBER_NAME_PARENTHESIS_VARIABLE));
		appendInvocation.arguments().add(literal);
	}
	if (ami.methodType == 3) {
		appendInvocation.arguments().add(memberAccessExpression);
	}

	canChainLastAppendCall= ami.returnsBuilder;

	return appendInvocation;
}
 
Example 16
Source File: ASTNodes.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Escapes a string value to a literal that can be used in Java source.
 * 
 * @param stringValue the string value 
 * @return the escaped string
 * @see StringLiteral#getEscapedValue()
 */
public static String getEscapedStringLiteral(String stringValue) {
	StringLiteral stringLiteral= AST.newAST(ASTProvider.SHARED_AST_LEVEL).newStringLiteral();
	stringLiteral.setLiteralValue(stringValue);
	return stringLiteral.getEscapedValue();
}