/** * Creates a literal with a given value. * * @param <T> * the type of the literal * @param value * the value of the literal * @return a new literal */ public <T> CtLiteral<T> createLiteral(T value) { CtLiteral<T> literal = factory.Core().<T>createLiteral(); literal.setValue(value); if (value != null) { literal.setType((CtTypeReference<T>) factory.Type().<T>createReference((Class<T>) value.getClass()).unbox()); } else { literal.setType((CtTypeReference<T>) factory.Type().nullType()); } return literal; }
public <T> void visitCtLiteral(final spoon.reflect.code.CtLiteral<T> literal) { spoon.reflect.code.CtLiteral<T> aCtLiteral = literal.getFactory().Core().createLiteral(); this.builder.copy(literal, aCtLiteral); aCtLiteral.setAnnotations(this.cloneHelper.clone(literal.getAnnotations())); aCtLiteral.setType(this.cloneHelper.clone(literal.getType())); aCtLiteral.setTypeCasts(this.cloneHelper.clone(literal.getTypeCasts())); aCtLiteral.setComments(this.cloneHelper.clone(literal.getComments())); this.cloneHelper.tailor(literal, aCtLiteral); this.other = aCtLiteral; }
@Override public boolean visit(LongLiteral longLiteral, BlockScope scope) { CtLiteral<Long> l = factory.Core().createLiteral(); l.setValue(longLiteral.constant.longValue()); CtTypeReference<Long> r = references.getTypeReference(longLiteral.resolvedType); l.setType(r); context.enter(l, longLiteral); return true; }
@Override public boolean visit(IntLiteral intLiteral, BlockScope scope) { CtLiteral<Integer> l = factory.Core().createLiteral(); CtTypeReference<Integer> r = references.getTypeReference(intLiteral.resolvedType); l.setType(r); if (intLiteral.constant != null) { // check required for noclasspath mode l.setValue(intLiteral.constant.intValue()); } context.enter(l, intLiteral); return true; }
@Override public boolean visit(NullLiteral nullLiteral, BlockScope scope) { CtLiteral<Object> lit = factory.Core().createLiteral(); CtTypeReference<Object> ref = factory.Core().createTypeReference(); ref.setSimpleName(CtTypeReference.NULL_TYPE_NAME); lit.setType(ref); context.enter(lit, nullLiteral); return true; }
@Override public boolean visit(StringLiteral stringLiteral, BlockScope scope) { CtLiteral<String> s = factory.Core().createLiteral(); // references.getTypeReference(stringLiteral.resolvedType) can be null s.setType(factory.Type().createReference(String.class)); // there are two methods in JDT: source() and toString() // source() seems better but actually does not return the real source // (for instance \n are not \n but newline) // toString seems better (see StringLiteralTest) // here there is a contract between JDTTreeBuilder and // DefaultJavaPrettyPrinter: // JDTTreeBuilder si responsible for adding the double quotes // s.setValue(new String(stringLiteral.toString())); // RP: this is not a good idea but many other usages of the value can be // done (apart from the pretty printer). So I moved back the // responsibility of pretty printing the string inside the pretty // printer (i.e. where it belongs) s.setValue(new String(stringLiteral.source())); context.enter(s, stringLiteral); return true; }