use of org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration in project lombok by rzwitserloot.
the class PatchValEclipse method copyInitializationOfLocalDeclaration.
public static void copyInitializationOfLocalDeclaration(Parser parser) {
ASTNode[] astStack;
int astPtr;
try {
astStack = (ASTNode[]) Reflection.astStackField.get(parser);
astPtr = (Integer) Reflection.astPtrField.get(parser);
} catch (Exception e) {
// Most likely we're in ecj or some other plugin usage of the eclipse compiler. No need for this.
return;
}
AbstractVariableDeclaration variableDecl = (AbstractVariableDeclaration) astStack[astPtr];
if (!(variableDecl instanceof LocalDeclaration))
return;
ASTNode init = variableDecl.initialization;
if (init == null)
return;
boolean val = couldBeVal(variableDecl.type);
boolean var = couldBeVar(variableDecl.type);
if (!(val || var))
return;
try {
if (Reflection.initCopyField != null)
Reflection.initCopyField.set(variableDecl, init);
} catch (Exception e) {
// In ecj mode this field isn't there and we don't need the copy anyway, so, we ignore the exception.
}
}
use of org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration in project lombok by rzwitserloot.
the class HandleBuilder method getSingularData.
/**
* Returns the explicitly requested singular annotation on this node (field
* or parameter), or null if there's no {@code @Singular} annotation on it.
*
* @param node The node (field or method param) to inspect for its name and potential {@code @Singular} annotation.
*/
private SingularData getSingularData(EclipseNode node, ASTNode source) {
for (EclipseNode child : node.down()) {
if (!annotationTypeMatches(Singular.class, child))
continue;
char[] pluralName = node.getKind() == Kind.FIELD ? removePrefixFromField(node) : ((AbstractVariableDeclaration) node.get()).name;
AnnotationValues<Singular> ann = createAnnotation(Singular.class, child);
String explicitSingular = ann.getInstance().value();
if (explicitSingular.isEmpty()) {
if (Boolean.FALSE.equals(node.getAst().readConfiguration(ConfigurationKeys.SINGULAR_AUTO))) {
node.addError("The singular must be specified explicitly (e.g. @Singular(\"task\")) because auto singularization is disabled.");
explicitSingular = new String(pluralName);
} else {
explicitSingular = autoSingularize(new String(pluralName));
if (explicitSingular == null) {
node.addError("Can't singularize this name; please specify the singular explicitly (i.e. @Singular(\"sheep\"))");
explicitSingular = new String(pluralName);
}
}
}
char[] singularName = explicitSingular.toCharArray();
TypeReference type = ((AbstractVariableDeclaration) node.get()).type;
TypeReference[] typeArgs = null;
String typeName;
if (type instanceof ParameterizedSingleTypeReference) {
typeArgs = ((ParameterizedSingleTypeReference) type).typeArguments;
typeName = new String(((ParameterizedSingleTypeReference) type).token);
} else if (type instanceof ParameterizedQualifiedTypeReference) {
TypeReference[][] tr = ((ParameterizedQualifiedTypeReference) type).typeArguments;
if (tr != null)
typeArgs = tr[tr.length - 1];
char[][] tokens = ((ParameterizedQualifiedTypeReference) type).tokens;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < tokens.length; i++) {
if (i > 0)
sb.append(".");
sb.append(tokens[i]);
}
typeName = sb.toString();
} else {
typeName = type.toString();
}
String targetFqn = EclipseSingularsRecipes.get().toQualified(typeName);
EclipseSingularizer singularizer = EclipseSingularsRecipes.get().getSingularizer(targetFqn);
if (singularizer == null) {
node.addError("Lombok does not know how to create the singular-form builder methods for type '" + typeName + "'; they won't be generated.");
return null;
}
return new SingularData(child, singularName, pluralName, typeArgs == null ? Collections.<TypeReference>emptyList() : Arrays.asList(typeArgs), targetFqn, singularizer, source);
}
return null;
}
use of org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration in project spoon by INRIA.
the class PositionBuilder method buildPositionCtElement.
SourcePosition buildPositionCtElement(CtElement e, ASTNode node) {
CoreFactory cf = this.jdtTreeBuilder.getFactory().Core();
CompilationUnit cu = this.jdtTreeBuilder.getFactory().CompilationUnit().getOrCreate(new String(this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.getFileName()));
CompilationResult cr = this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.compilationResult;
int[] lineSeparatorPositions = cr.lineSeparatorPositions;
char[] contents = cr.compilationUnit.getContents();
int sourceStart = node.sourceStart;
int sourceEnd = node.sourceEnd;
if ((node instanceof Annotation)) {
Annotation ann = (Annotation) node;
int declEnd = ann.declarationSourceEnd;
if (declEnd > 0) {
sourceEnd = declEnd;
}
} else if ((node instanceof Expression)) {
Expression expression = (Expression) node;
int statementEnd = expression.statementEnd;
if (statementEnd > 0) {
sourceEnd = statementEnd;
}
}
if (node instanceof AbstractVariableDeclaration) {
AbstractVariableDeclaration variableDeclaration = (AbstractVariableDeclaration) node;
int modifiersSourceStart = variableDeclaration.modifiersSourceStart;
int declarationSourceStart = variableDeclaration.declarationSourceStart;
int declarationSourceEnd = variableDeclaration.declarationSourceEnd;
int declarationEnd = variableDeclaration.declarationEnd;
Annotation[] annotations = variableDeclaration.annotations;
if (annotations != null && annotations.length > 0) {
if (annotations[0].sourceStart() == sourceStart) {
modifiersSourceStart = annotations[annotations.length - 1].sourceEnd() + 2;
}
}
if (modifiersSourceStart == 0) {
modifiersSourceStart = declarationSourceStart;
}
int modifiersSourceEnd;
if (variableDeclaration.type != null) {
modifiersSourceEnd = variableDeclaration.type.sourceStart() - 2;
} else {
// variable that has no type such as TypeParameter
modifiersSourceEnd = declarationSourceStart - 1;
}
// when no modifier
if (modifiersSourceStart > modifiersSourceEnd) {
modifiersSourceEnd = modifiersSourceStart - 1;
}
return cf.createDeclarationSourcePosition(cu, sourceStart, sourceEnd, modifiersSourceStart, modifiersSourceEnd, declarationSourceStart, declarationSourceEnd, lineSeparatorPositions);
} else if (node instanceof TypeDeclaration) {
TypeDeclaration typeDeclaration = (TypeDeclaration) node;
int declarationSourceStart = typeDeclaration.declarationSourceStart;
int declarationSourceEnd = typeDeclaration.declarationSourceEnd;
int modifiersSourceStart = typeDeclaration.modifiersSourceStart;
int bodyStart = typeDeclaration.bodyStart;
int bodyEnd = typeDeclaration.bodyEnd;
Annotation[] annotations = typeDeclaration.annotations;
if (annotations != null && annotations.length > 0) {
if (annotations[0].sourceStart() == declarationSourceStart) {
modifiersSourceStart = findNextNonWhitespace(contents, annotations[annotations.length - 1].declarationSourceEnd + 1);
}
}
if (modifiersSourceStart == 0) {
modifiersSourceStart = declarationSourceStart;
}
// look for start of first keyword before the type keyword e.g. "class". `sourceStart` points at first char of type name
int modifiersSourceEnd = findPrevNonWhitespace(contents, findPrevWhitespace(contents, findPrevNonWhitespace(contents, sourceStart - 1)));
if (modifiersSourceEnd < modifiersSourceStart) {
// there is no modifier
modifiersSourceEnd = modifiersSourceStart - 1;
}
return cf.createBodyHolderSourcePosition(cu, sourceStart, sourceEnd, modifiersSourceStart, modifiersSourceEnd, declarationSourceStart, declarationSourceEnd, bodyStart - 1, bodyEnd, lineSeparatorPositions);
} else if (node instanceof AbstractMethodDeclaration) {
AbstractMethodDeclaration methodDeclaration = (AbstractMethodDeclaration) node;
int bodyStart = methodDeclaration.bodyStart;
int bodyEnd = methodDeclaration.bodyEnd;
int declarationSourceStart = methodDeclaration.declarationSourceStart;
int declarationSourceEnd = methodDeclaration.declarationSourceEnd;
int modifiersSourceStart = methodDeclaration.modifiersSourceStart;
if (modifiersSourceStart == 0) {
modifiersSourceStart = declarationSourceStart;
}
if (node instanceof AnnotationMethodDeclaration && bodyStart == bodyEnd) {
// The ";" at the end of annotation method declaration is not part of body
// let it behave same like in abstract MethodDeclaration
bodyEnd--;
}
Javadoc javadoc = methodDeclaration.javadoc;
if (javadoc != null && javadoc.sourceEnd() > declarationSourceStart) {
modifiersSourceStart = javadoc.sourceEnd() + 1;
}
Annotation[] annotations = methodDeclaration.annotations;
if (annotations != null && annotations.length > 0) {
if (annotations[0].sourceStart() == declarationSourceStart) {
modifiersSourceStart = annotations[annotations.length - 1].sourceEnd() + 2;
}
}
int modifiersSourceEnd = sourceStart - 1;
if (methodDeclaration instanceof MethodDeclaration && ((MethodDeclaration) methodDeclaration).returnType != null) {
modifiersSourceEnd = ((MethodDeclaration) methodDeclaration).returnType.sourceStart() - 2;
}
TypeParameter[] typeParameters = methodDeclaration.typeParameters();
if (typeParameters != null && typeParameters.length > 0) {
modifiersSourceEnd = typeParameters[0].declarationSourceStart - 3;
}
if (getModifiers(methodDeclaration.modifiers, false, true).isEmpty()) {
modifiersSourceStart = modifiersSourceEnd + 1;
}
sourceEnd = sourceStart + methodDeclaration.selector.length - 1;
if (e instanceof CtStatementList) {
return cf.createSourcePosition(cu, bodyStart - 1, bodyEnd + 1, lineSeparatorPositions);
} else {
if (bodyStart == 0) {
return SourcePosition.NOPOSITION;
} else {
if (bodyStart < bodyEnd) {
// include brackets if they are there
if (contents[bodyStart - 1] == '{') {
bodyStart--;
if (contents[bodyEnd + 1] == '}') {
bodyEnd++;
} else {
throw new SpoonException("Missing body end in\n" + new String(contents, sourceStart, sourceEnd - sourceStart));
}
}
}
return cf.createBodyHolderSourcePosition(cu, sourceStart, sourceEnd, modifiersSourceStart, modifiersSourceEnd, declarationSourceStart, declarationSourceEnd, bodyStart, bodyEnd, lineSeparatorPositions);
}
}
}
return cf.createSourcePosition(cu, sourceStart, sourceEnd, lineSeparatorPositions);
}
Aggregations