use of org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference in project lombok by rzwitserloot.
the class EclipseHandlerUtil method copyType.
/**
* You can't share TypeReference objects or subtle errors start happening.
* Unfortunately the TypeReference type hierarchy is complicated and there's no clone
* method on TypeReference itself. This method can clone them.
*/
public static TypeReference copyType(TypeReference ref, ASTNode source) {
if (ref instanceof ParameterizedQualifiedTypeReference) {
ParameterizedQualifiedTypeReference iRef = (ParameterizedQualifiedTypeReference) ref;
TypeReference[][] args = null;
if (iRef.typeArguments != null) {
args = new TypeReference[iRef.typeArguments.length][];
int idx = 0;
for (TypeReference[] inRefArray : iRef.typeArguments) {
if (inRefArray == null)
args[idx++] = null;
else {
TypeReference[] outRefArray = new TypeReference[inRefArray.length];
int idx2 = 0;
for (TypeReference inRef : inRefArray) {
outRefArray[idx2++] = copyType(inRef, source);
}
args[idx++] = outRefArray;
}
}
}
TypeReference typeRef = new ParameterizedQualifiedTypeReference(iRef.tokens, args, iRef.dimensions(), copy(iRef.sourcePositions));
if (source != null)
setGeneratedBy(typeRef, source);
return typeRef;
}
if (ref instanceof ArrayQualifiedTypeReference) {
ArrayQualifiedTypeReference iRef = (ArrayQualifiedTypeReference) ref;
TypeReference typeRef = new ArrayQualifiedTypeReference(iRef.tokens, iRef.dimensions(), copy(iRef.sourcePositions));
if (source != null)
setGeneratedBy(typeRef, source);
return typeRef;
}
if (ref instanceof QualifiedTypeReference) {
QualifiedTypeReference iRef = (QualifiedTypeReference) ref;
TypeReference typeRef = new QualifiedTypeReference(iRef.tokens, copy(iRef.sourcePositions));
if (source != null)
setGeneratedBy(typeRef, source);
return typeRef;
}
if (ref instanceof ParameterizedSingleTypeReference) {
ParameterizedSingleTypeReference iRef = (ParameterizedSingleTypeReference) ref;
TypeReference[] args = null;
if (iRef.typeArguments != null) {
args = new TypeReference[iRef.typeArguments.length];
int idx = 0;
for (TypeReference inRef : iRef.typeArguments) {
if (inRef == null)
args[idx++] = null;
else
args[idx++] = copyType(inRef, source);
}
}
TypeReference typeRef = new ParameterizedSingleTypeReference(iRef.token, args, iRef.dimensions(), (long) iRef.sourceStart << 32 | iRef.sourceEnd);
if (source != null)
setGeneratedBy(typeRef, source);
return typeRef;
}
if (ref instanceof ArrayTypeReference) {
ArrayTypeReference iRef = (ArrayTypeReference) ref;
TypeReference typeRef = new ArrayTypeReference(iRef.token, iRef.dimensions(), (long) iRef.sourceStart << 32 | iRef.sourceEnd);
if (source != null)
setGeneratedBy(typeRef, source);
return typeRef;
}
if (ref instanceof Wildcard) {
Wildcard original = (Wildcard) ref;
Wildcard wildcard = new Wildcard(original.kind);
wildcard.sourceStart = original.sourceStart;
wildcard.sourceEnd = original.sourceEnd;
if (original.bound != null)
wildcard.bound = copyType(original.bound, source);
if (source != null)
setGeneratedBy(wildcard, source);
return wildcard;
}
if (ref instanceof SingleTypeReference) {
SingleTypeReference iRef = (SingleTypeReference) ref;
TypeReference typeRef = new SingleTypeReference(iRef.token, (long) iRef.sourceStart << 32 | iRef.sourceEnd);
if (source != null)
setGeneratedBy(typeRef, source);
return typeRef;
}
return ref;
}
use of org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference in project lombok by rzwitserloot.
the class EclipseHandlerUtil method generateDeprecatedAnnotation.
public static MarkerAnnotation generateDeprecatedAnnotation(ASTNode source) {
QualifiedTypeReference qtr = new QualifiedTypeReference(new char[][] { { 'j', 'a', 'v', 'a' }, { 'l', 'a', 'n', 'g' }, { 'D', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd' } }, poss(source, 3));
setGeneratedBy(qtr, source);
MarkerAnnotation ma = new MarkerAnnotation(qtr, source.sourceStart);
// No matter what value you input for sourceEnd, the AST->DOM converter of eclipse will reparse to find the end, and will fail as
// it can't find code that isn't really there. This results in the end position being set to 2 or 0 or some weird magic value, and thus,
// length, as calculated by end-start, is all screwed up, resulting in IllegalArgumentException during a setSourceRange call MUCH later in the process.
// We solve it by going with a voodoo magic source start value such that the calculated length so happens to exactly be 0. 0 lengths are accepted
// by eclipse. For some reason.
// TL;DR: Don't change 1. 1 is sacred. Trust the 1.
// issue: #408.
ma.sourceStart = 1;
setGeneratedBy(ma, source);
return ma;
}
use of org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference in project lombok by rzwitserloot.
the class EclipseHandlerUtil method generateNullCheck.
/**
* Generates a new statement that checks if the given variable is null, and if so, throws a specified exception with the
* variable name as message.
*
* @param exName The name of the exception to throw; normally {@code java.lang.NullPointerException}.
*/
public static Statement generateNullCheck(AbstractVariableDeclaration variable, EclipseNode sourceNode) {
NullCheckExceptionType exceptionType = sourceNode.getAst().readConfiguration(ConfigurationKeys.NON_NULL_EXCEPTION_TYPE);
if (exceptionType == null)
exceptionType = NullCheckExceptionType.NULL_POINTER_EXCEPTION;
ASTNode source = sourceNode.get();
int pS = source.sourceStart, pE = source.sourceEnd;
long p = (long) pS << 32 | pE;
if (isPrimitive(variable.type))
return null;
AllocationExpression exception = new AllocationExpression();
setGeneratedBy(exception, source);
int partCount = 1;
String exceptionTypeStr = exceptionType.getExceptionType();
for (int i = 0; i < exceptionTypeStr.length(); i++) if (exceptionTypeStr.charAt(i) == '.')
partCount++;
long[] ps = new long[partCount];
Arrays.fill(ps, 0L);
exception.type = new QualifiedTypeReference(fromQualifiedName(exceptionTypeStr), ps);
setGeneratedBy(exception.type, source);
exception.arguments = new Expression[] { new StringLiteral(exceptionType.toExceptionMessage(new String(variable.name)).toCharArray(), pS, pE, 0) };
setGeneratedBy(exception.arguments[0], source);
ThrowStatement throwStatement = new ThrowStatement(exception, pS, pE);
setGeneratedBy(throwStatement, source);
SingleNameReference varName = new SingleNameReference(variable.name, p);
setGeneratedBy(varName, source);
NullLiteral nullLiteral = new NullLiteral(pS, pE);
setGeneratedBy(nullLiteral, source);
EqualExpression equalExpression = new EqualExpression(varName, nullLiteral, OperatorIds.EQUAL_EQUAL);
equalExpression.sourceStart = pS;
equalExpression.statementEnd = equalExpression.sourceEnd = pE;
setGeneratedBy(equalExpression, source);
Block throwBlock = new Block(0);
throwBlock.statements = new Statement[] { throwStatement };
throwBlock.sourceStart = pS;
throwBlock.sourceEnd = pE;
setGeneratedBy(throwBlock, source);
IfStatement ifStatement = new IfStatement(equalExpression, throwBlock, 0, 0);
setGeneratedBy(ifStatement, source);
return ifStatement;
}
use of org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference in project lombok by rzwitserloot.
the class HandleEqualsAndHashCode method createTypeReference.
/**
* @param type Type to 'copy' into a typeref
* @param p position
* @param addWildcards If false, all generics are cut off. If true, replaces all genericparams with a ?.
* @return
*/
public TypeReference createTypeReference(EclipseNode type, long p, ASTNode source, boolean addWildcards) {
int pS = source.sourceStart;
int pE = source.sourceEnd;
List<String> list = new ArrayList<String>();
list.add(type.getName());
EclipseNode tNode = type.up();
while (tNode != null && tNode.getKind() == Kind.TYPE) {
list.add(tNode.getName());
tNode = tNode.up();
}
Collections.reverse(list);
TypeDeclaration typeDecl = (TypeDeclaration) type.get();
int typeParamCount = typeDecl.typeParameters == null ? 0 : typeDecl.typeParameters.length;
if (typeParamCount == 0)
addWildcards = false;
TypeReference[] typeArgs = null;
if (addWildcards) {
typeArgs = new TypeReference[typeParamCount];
for (int i = 0; i < typeParamCount; i++) {
typeArgs[i] = new Wildcard(Wildcard.UNBOUND);
typeArgs[i].sourceStart = pS;
typeArgs[i].sourceEnd = pE;
setGeneratedBy(typeArgs[i], source);
}
}
if (list.size() == 1) {
if (addWildcards) {
return new ParameterizedSingleTypeReference(list.get(0).toCharArray(), typeArgs, 0, p);
} else {
return new SingleTypeReference(list.get(0).toCharArray(), p);
}
}
long[] ps = new long[list.size()];
char[][] tokens = new char[list.size()][];
for (int i = 0; i < list.size(); i++) {
ps[i] = p;
tokens[i] = list.get(i).toCharArray();
}
if (addWildcards) {
TypeReference[][] typeArgs2 = new TypeReference[tokens.length][];
typeArgs2[typeArgs2.length - 1] = typeArgs;
return new ParameterizedQualifiedTypeReference(tokens, typeArgs2, 0, ps);
} else {
return new QualifiedTypeReference(tokens, ps);
}
}
use of org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference in project lombok by rzwitserloot.
the class HandleEqualsAndHashCode method createCanEqual.
public MethodDeclaration createCanEqual(EclipseNode type, ASTNode source, List<Annotation> onParam) {
/* protected boolean canEqual(final java.lang.Object other) {
* return other instanceof Outer.Inner.MyType;
* }
*/
int pS = source.sourceStart;
int pE = source.sourceEnd;
long p = (long) pS << 32 | pE;
char[] otherName = "other".toCharArray();
MethodDeclaration method = new MethodDeclaration(((CompilationUnitDeclaration) type.top().get()).compilationResult);
setGeneratedBy(method, source);
method.modifiers = toEclipseModifier(AccessLevel.PROTECTED);
method.returnType = TypeReference.baseTypeReference(TypeIds.T_boolean, 0);
method.returnType.sourceStart = pS;
method.returnType.sourceEnd = pE;
setGeneratedBy(method.returnType, source);
method.selector = "canEqual".toCharArray();
method.thrownExceptions = null;
method.typeParameters = null;
method.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG;
method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart;
method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd;
TypeReference objectRef = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { p, p, p });
setGeneratedBy(objectRef, source);
method.arguments = new Argument[] { new Argument(otherName, 0, objectRef, Modifier.FINAL) };
method.arguments[0].sourceStart = pS;
method.arguments[0].sourceEnd = pE;
if (!onParam.isEmpty())
method.arguments[0].annotations = onParam.toArray(new Annotation[0]);
setGeneratedBy(method.arguments[0], source);
SingleNameReference otherRef = new SingleNameReference(otherName, p);
setGeneratedBy(otherRef, source);
TypeReference typeReference = createTypeReference(type, p, source, false);
setGeneratedBy(typeReference, source);
InstanceOfExpression instanceOf = new InstanceOfExpression(otherRef, typeReference);
instanceOf.sourceStart = pS;
instanceOf.sourceEnd = pE;
setGeneratedBy(instanceOf, source);
ReturnStatement returnStatement = new ReturnStatement(instanceOf, pS, pE);
setGeneratedBy(returnStatement, source);
method.statements = new Statement[] { returnStatement };
return method;
}
Aggregations