use of org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration in project lombok by rzwitserloot.
the class HandleBuilder method makeSimpleSetterMethodForBuilder.
private void makeSimpleSetterMethodForBuilder(EclipseNode builderType, boolean deprecate, EclipseNode fieldNode, char[] nameOfSetFlag, EclipseNode sourceNode, boolean fluent, boolean chain) {
TypeDeclaration td = (TypeDeclaration) builderType.get();
AbstractMethodDeclaration[] existing = td.methods;
if (existing == null)
existing = EMPTY;
int len = existing.length;
FieldDeclaration fd = (FieldDeclaration) fieldNode.get();
char[] name = fd.name;
for (int i = 0; i < len; i++) {
if (!(existing[i] instanceof MethodDeclaration))
continue;
char[] existingName = existing[i].selector;
if (Arrays.equals(name, existingName) && !isTolerate(fieldNode, existing[i]))
return;
}
String setterName = fluent ? fieldNode.getName() : HandlerUtil.buildAccessorName("set", fieldNode.getName());
MethodDeclaration setter = HandleSetter.createSetter(td, deprecate, fieldNode, setterName, nameOfSetFlag, chain, ClassFileConstants.AccPublic, sourceNode, Collections.<Annotation>emptyList(), Collections.<Annotation>emptyList());
injectMethod(builderType, setter);
}
use of org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration in project lombok by rzwitserloot.
the class HandleCleanup method handle.
public void handle(AnnotationValues<Cleanup> annotation, Annotation ast, EclipseNode annotationNode) {
handleFlagUsage(annotationNode, ConfigurationKeys.CLEANUP_FLAG_USAGE, "@Cleanup");
String cleanupName = annotation.getInstance().value();
if (cleanupName.length() == 0) {
annotationNode.addError("cleanupName cannot be the empty string.");
return;
}
if (annotationNode.up().getKind() != Kind.LOCAL) {
annotationNode.addError("@Cleanup is legal only on local variable declarations.");
return;
}
LocalDeclaration decl = (LocalDeclaration) annotationNode.up().get();
if (decl.initialization == null) {
annotationNode.addError("@Cleanup variable declarations need to be initialized.");
return;
}
EclipseNode ancestor = annotationNode.up().directUp();
ASTNode blockNode = ancestor.get();
final boolean isSwitch;
final Statement[] statements;
if (blockNode instanceof AbstractMethodDeclaration) {
isSwitch = false;
statements = ((AbstractMethodDeclaration) blockNode).statements;
} else if (blockNode instanceof Block) {
isSwitch = false;
statements = ((Block) blockNode).statements;
} else if (blockNode instanceof SwitchStatement) {
isSwitch = true;
statements = ((SwitchStatement) blockNode).statements;
} else {
annotationNode.addError("@Cleanup is legal only on a local variable declaration inside a block.");
return;
}
if (statements == null) {
annotationNode.addError("LOMBOK BUG: Parent block does not contain any statements.");
return;
}
int start = 0;
for (; start < statements.length; start++) {
if (statements[start] == decl)
break;
}
if (start == statements.length) {
annotationNode.addError("LOMBOK BUG: Can't find this local variable declaration inside its parent.");
return;
}
// We start with try{} *AFTER* the var declaration.
start++;
int end;
if (isSwitch) {
end = start + 1;
for (; end < statements.length; end++) {
if (statements[end] instanceof CaseStatement) {
break;
}
}
} else
end = statements.length;
// At this point:
// start-1 = Local Declaration marked with @Cleanup
// start = first instruction that needs to be wrapped into a try block
// end = last instruction of the scope -OR- last instruction before the next case label in switch statements.
// hence:
// [start, end) = statements for the try block.
Statement[] tryBlock = new Statement[end - start];
System.arraycopy(statements, start, tryBlock, 0, end - start);
// Remove the stuff we just dumped into the tryBlock, and then leave room for the try node.
// Remove room for every statement moved into try block...
int newStatementsLength = statements.length - (end - start);
// But add room for the TryStatement node itself.
newStatementsLength += 1;
Statement[] newStatements = new Statement[newStatementsLength];
// copy all statements before the try block verbatim.
System.arraycopy(statements, 0, newStatements, 0, start);
// For switch statements.
System.arraycopy(statements, end, newStatements, start + 1, statements.length - end);
doAssignmentCheck(annotationNode, tryBlock, decl.name);
TryStatement tryStatement = new TryStatement();
setGeneratedBy(tryStatement, ast);
tryStatement.tryBlock = new Block(0);
tryStatement.tryBlock.statements = tryBlock;
setGeneratedBy(tryStatement.tryBlock, ast);
// Positions for in-method generated nodes are special
int ss = decl.declarationSourceEnd + 1;
int se = ss;
if (tryBlock.length > 0) {
// +1 for the closing semicolon. Yes, there could be spaces. Bummer.
se = tryBlock[tryBlock.length - 1].sourceEnd + 1;
tryStatement.sourceStart = ss;
tryStatement.sourceEnd = se;
tryStatement.tryBlock.sourceStart = ss;
tryStatement.tryBlock.sourceEnd = se;
}
newStatements[start] = tryStatement;
Statement[] finallyBlock = new Statement[1];
MessageSend unsafeClose = new MessageSend();
setGeneratedBy(unsafeClose, ast);
unsafeClose.sourceStart = ast.sourceStart;
unsafeClose.sourceEnd = ast.sourceEnd;
SingleNameReference receiver = new SingleNameReference(decl.name, 0);
setGeneratedBy(receiver, ast);
unsafeClose.receiver = receiver;
long nameSourcePosition = (long) ast.sourceStart << 32 | ast.sourceEnd;
if (ast.memberValuePairs() != null)
for (MemberValuePair pair : ast.memberValuePairs()) {
if (pair.name != null && new String(pair.name).equals("value")) {
nameSourcePosition = (long) pair.value.sourceStart << 32 | pair.value.sourceEnd;
break;
}
}
unsafeClose.nameSourcePosition = nameSourcePosition;
unsafeClose.selector = cleanupName.toCharArray();
int pS = ast.sourceStart, pE = ast.sourceEnd;
long p = (long) pS << 32 | pE;
SingleNameReference varName = new SingleNameReference(decl.name, p);
setGeneratedBy(varName, ast);
NullLiteral nullLiteral = new NullLiteral(pS, pE);
setGeneratedBy(nullLiteral, ast);
MessageSend preventNullAnalysis = preventNullAnalysis(ast, varName);
EqualExpression equalExpression = new EqualExpression(preventNullAnalysis, nullLiteral, OperatorIds.NOT_EQUAL);
equalExpression.sourceStart = pS;
equalExpression.sourceEnd = pE;
setGeneratedBy(equalExpression, ast);
Block closeBlock = new Block(0);
closeBlock.statements = new Statement[1];
closeBlock.statements[0] = unsafeClose;
setGeneratedBy(closeBlock, ast);
IfStatement ifStatement = new IfStatement(equalExpression, closeBlock, 0, 0);
setGeneratedBy(ifStatement, ast);
finallyBlock[0] = ifStatement;
tryStatement.finallyBlock = new Block(0);
// Positions for in-method generated nodes are special
if (!isSwitch) {
tryStatement.finallyBlock.sourceStart = blockNode.sourceEnd;
tryStatement.finallyBlock.sourceEnd = blockNode.sourceEnd;
}
setGeneratedBy(tryStatement.finallyBlock, ast);
tryStatement.finallyBlock.statements = finallyBlock;
tryStatement.catchArguments = null;
tryStatement.catchBlocks = null;
if (blockNode instanceof AbstractMethodDeclaration) {
((AbstractMethodDeclaration) blockNode).statements = newStatements;
} else if (blockNode instanceof Block) {
((Block) blockNode).statements = newStatements;
} else if (blockNode instanceof SwitchStatement) {
((SwitchStatement) blockNode).statements = newStatements;
}
ancestor.rebuild();
}
use of org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration in project che by eclipse.
the class SourceTypeConverter method convert.
/*
* Convert a method source element into a parsed method/constructor declaration
*/
private AbstractMethodDeclaration convert(SourceMethod methodHandle, SourceMethodElementInfo methodInfo, CompilationResult compilationResult) throws JavaModelException {
AbstractMethodDeclaration method;
/* only source positions available */
int start = methodInfo.getNameSourceStart();
int end = methodInfo.getNameSourceEnd();
/* https://bugs.eclipse.org/bugs/show_bug.cgi?id=324850, Even when this type is being constructed
on behalf of a 1.4 project we must internalize type variables properly in order to be able to
recognize usages of them in the method signature, to apply substitutions and thus to be able to
detect overriding in the presence of generics. If we simply drop them, when the method signature
refers to the type parameter, we won't know it should be bound to the type parameter and perform
incorrect lookup and may mistakenly end up with missing types
*/
TypeParameter[] typeParams = null;
char[][] typeParameterNames = methodInfo.getTypeParameterNames();
if (typeParameterNames != null) {
int parameterCount = typeParameterNames.length;
if (parameterCount > 0) {
// method's type parameters must be null if no type parameter
char[][][] typeParameterBounds = methodInfo.getTypeParameterBounds();
typeParams = new TypeParameter[parameterCount];
for (int i = 0; i < parameterCount; i++) {
typeParams[i] = createTypeParameter(typeParameterNames[i], typeParameterBounds[i], start, end);
}
}
}
int modifiers = methodInfo.getModifiers();
if (methodInfo.isConstructor()) {
ConstructorDeclaration decl = new ConstructorDeclaration(compilationResult);
decl.bits &= ~ASTNode.IsDefaultConstructor;
method = decl;
decl.typeParameters = typeParams;
} else {
MethodDeclaration decl;
if (methodInfo.isAnnotationMethod()) {
AnnotationMethodDeclaration annotationMethodDeclaration = new AnnotationMethodDeclaration(compilationResult);
/* conversion of default value */
SourceAnnotationMethodInfo annotationMethodInfo = (SourceAnnotationMethodInfo) methodInfo;
boolean hasDefaultValue = annotationMethodInfo.defaultValueStart != -1 || annotationMethodInfo.defaultValueEnd != -1;
if ((this.flags & FIELD_INITIALIZATION) != 0) {
if (hasDefaultValue) {
char[] defaultValueSource = CharOperation.subarray(getSource(), annotationMethodInfo.defaultValueStart, annotationMethodInfo.defaultValueEnd + 1);
if (defaultValueSource != null) {
Expression expression = parseMemberValue(defaultValueSource);
if (expression != null) {
annotationMethodDeclaration.defaultValue = expression;
}
} else {
// could not retrieve the default value
hasDefaultValue = false;
}
}
}
if (hasDefaultValue)
modifiers |= ClassFileConstants.AccAnnotationDefault;
decl = annotationMethodDeclaration;
} else {
decl = new MethodDeclaration(compilationResult);
}
// convert return type
decl.returnType = createTypeReference(methodInfo.getReturnTypeName(), start, end);
// type parameters
decl.typeParameters = typeParams;
method = decl;
}
method.selector = methodHandle.getElementName().toCharArray();
boolean isVarargs = (modifiers & ClassFileConstants.AccVarargs) != 0;
method.modifiers = modifiers & ~ClassFileConstants.AccVarargs;
method.sourceStart = start;
method.sourceEnd = end;
method.declarationSourceStart = methodInfo.getDeclarationSourceStart();
method.declarationSourceEnd = methodInfo.getDeclarationSourceEnd();
// convert 1.5 specific constructs only if compliance is 1.5 or above
if (this.has1_5Compliance) {
/* convert annotations */
method.annotations = convertAnnotations(methodHandle);
}
/* convert arguments */
String[] argumentTypeSignatures = methodHandle.getParameterTypes();
char[][] argumentNames = methodInfo.getArgumentNames();
int argumentCount = argumentTypeSignatures == null ? 0 : argumentTypeSignatures.length;
if (argumentCount > 0) {
ILocalVariable[] parameters = methodHandle.getParameters();
long position = ((long) start << 32) + end;
method.arguments = new Argument[argumentCount];
for (int i = 0; i < argumentCount; i++) {
TypeReference typeReference = createTypeReference(argumentTypeSignatures[i], start, end);
if (isVarargs && i == argumentCount - 1) {
typeReference.bits |= ASTNode.IsVarArgs;
}
method.arguments[i] = new Argument(argumentNames[i], position, typeReference, ClassFileConstants.AccDefault);
// convert 1.5 specific constructs only if compliance is 1.5 or above
if (this.has1_5Compliance) {
/* convert annotations */
method.arguments[i].annotations = convertAnnotations(parameters[i]);
}
}
}
/* convert thrown exceptions */
char[][] exceptionTypeNames = methodInfo.getExceptionTypeNames();
int exceptionCount = exceptionTypeNames == null ? 0 : exceptionTypeNames.length;
if (exceptionCount > 0) {
method.thrownExceptions = new TypeReference[exceptionCount];
for (int i = 0; i < exceptionCount; i++) {
method.thrownExceptions[i] = createTypeReference(exceptionTypeNames[i], start, end);
}
}
/* convert local and anonymous types */
if ((this.flags & LOCAL_TYPE) != 0) {
IJavaElement[] children = methodInfo.getChildren();
int typesLength = children.length;
if (typesLength != 0) {
Statement[] statements = new Statement[typesLength];
for (int i = 0; i < typesLength; i++) {
SourceType type = (SourceType) children[i];
TypeDeclaration localType = convert(type, compilationResult);
if ((localType.bits & ASTNode.IsAnonymousType) != 0) {
QualifiedAllocationExpression expression = new QualifiedAllocationExpression(localType);
expression.type = localType.superclass;
localType.superclass = null;
localType.superInterfaces = null;
localType.allocation = expression;
statements[i] = expression;
} else {
statements[i] = localType;
}
}
method.statements = statements;
}
}
return method;
}
use of org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration in project che by eclipse.
the class SourceTypeConverter method convert.
/*
* Convert a source element type into a parsed type declaration
*/
private TypeDeclaration convert(SourceType typeHandle, CompilationResult compilationResult) throws JavaModelException {
SourceTypeElementInfo typeInfo = (SourceTypeElementInfo) typeHandle.getElementInfo();
if (typeInfo.isAnonymousMember())
throw new AnonymousMemberFound();
/* create type declaration - can be member type */
TypeDeclaration type = new TypeDeclaration(compilationResult);
if (typeInfo.getEnclosingType() == null) {
if (typeHandle.isAnonymous()) {
type.name = CharOperation.NO_CHAR;
type.bits |= (ASTNode.IsAnonymousType | ASTNode.IsLocalType);
} else {
if (typeHandle.isLocal()) {
type.bits |= ASTNode.IsLocalType;
}
}
} else {
type.bits |= ASTNode.IsMemberType;
}
if ((type.bits & ASTNode.IsAnonymousType) == 0) {
type.name = typeInfo.getName();
}
type.name = typeInfo.getName();
// only positions available
int start, end;
type.sourceStart = start = typeInfo.getNameSourceStart();
type.sourceEnd = end = typeInfo.getNameSourceEnd();
type.modifiers = typeInfo.getModifiers();
type.declarationSourceStart = typeInfo.getDeclarationSourceStart();
type.declarationSourceEnd = typeInfo.getDeclarationSourceEnd();
type.bodyEnd = type.declarationSourceEnd;
// convert 1.5 specific constructs only if compliance is 1.5 or above
if (this.has1_5Compliance) {
/* convert annotations */
type.annotations = convertAnnotations(typeHandle);
}
/* https://bugs.eclipse.org/bugs/show_bug.cgi?id=324850, even in a 1.4 project, we
must internalize type variables and observe any parameterization of super class
and/or super interfaces in order to be able to detect overriding in the presence
of generics.
*/
char[][] typeParameterNames = typeInfo.getTypeParameterNames();
if (typeParameterNames.length > 0) {
int parameterCount = typeParameterNames.length;
char[][][] typeParameterBounds = typeInfo.getTypeParameterBounds();
type.typeParameters = new TypeParameter[parameterCount];
for (int i = 0; i < parameterCount; i++) {
type.typeParameters[i] = createTypeParameter(typeParameterNames[i], typeParameterBounds[i], start, end);
}
}
/* set superclass and superinterfaces */
if (typeInfo.getSuperclassName() != null) {
type.superclass = createTypeReference(typeInfo.getSuperclassName(), start, end, true);
type.superclass.bits |= ASTNode.IsSuperType;
}
char[][] interfaceNames = typeInfo.getInterfaceNames();
int interfaceCount = interfaceNames == null ? 0 : interfaceNames.length;
if (interfaceCount > 0) {
type.superInterfaces = new TypeReference[interfaceCount];
for (int i = 0; i < interfaceCount; i++) {
type.superInterfaces[i] = createTypeReference(interfaceNames[i], start, end, true);
type.superInterfaces[i].bits |= ASTNode.IsSuperType;
}
}
/* convert member types */
if ((this.flags & MEMBER_TYPE) != 0) {
SourceType[] sourceMemberTypes = typeInfo.getMemberTypeHandles();
int sourceMemberTypeCount = sourceMemberTypes.length;
type.memberTypes = new TypeDeclaration[sourceMemberTypeCount];
for (int i = 0; i < sourceMemberTypeCount; i++) {
type.memberTypes[i] = convert(sourceMemberTypes[i], compilationResult);
type.memberTypes[i].enclosingType = type;
}
}
/* convert intializers and fields*/
InitializerElementInfo[] initializers = null;
int initializerCount = 0;
if ((this.flags & LOCAL_TYPE) != 0) {
initializers = typeInfo.getInitializers();
initializerCount = initializers.length;
}
SourceField[] sourceFields = null;
int sourceFieldCount = 0;
if ((this.flags & FIELD) != 0) {
sourceFields = typeInfo.getFieldHandles();
sourceFieldCount = sourceFields.length;
}
int length = initializerCount + sourceFieldCount;
if (length > 0) {
type.fields = new FieldDeclaration[length];
for (int i = 0; i < initializerCount; i++) {
type.fields[i] = convert(initializers[i], compilationResult);
}
int index = 0;
for (int i = initializerCount; i < length; i++) {
type.fields[i] = convert(sourceFields[index++], type, compilationResult);
}
}
/* convert methods - need to add default constructor if necessary */
boolean needConstructor = (this.flags & CONSTRUCTOR) != 0;
boolean needMethod = (this.flags & METHOD) != 0;
if (needConstructor || needMethod) {
SourceMethod[] sourceMethods = typeInfo.getMethodHandles();
int sourceMethodCount = sourceMethods.length;
/* source type has a constructor ? */
/* by default, we assume that one is needed. */
int extraConstructor = 0;
int methodCount = 0;
int kind = TypeDeclaration.kind(type.modifiers);
boolean isAbstract = kind == TypeDeclaration.INTERFACE_DECL || kind == TypeDeclaration.ANNOTATION_TYPE_DECL;
if (!isAbstract) {
extraConstructor = needConstructor ? 1 : 0;
for (int i = 0; i < sourceMethodCount; i++) {
if (sourceMethods[i].isConstructor()) {
if (needConstructor) {
// Does not need the extra constructor since one constructor already exists.
extraConstructor = 0;
methodCount++;
}
} else if (needMethod) {
methodCount++;
}
}
} else {
methodCount = needMethod ? sourceMethodCount : 0;
}
type.methods = new AbstractMethodDeclaration[methodCount + extraConstructor];
if (extraConstructor != 0) {
// add default constructor in first position
type.methods[0] = type.createDefaultConstructor(false, false);
}
int index = 0;
boolean hasAbstractMethods = false;
for (int i = 0; i < sourceMethodCount; i++) {
SourceMethod sourceMethod = sourceMethods[i];
SourceMethodElementInfo methodInfo = (SourceMethodElementInfo) sourceMethod.getElementInfo();
boolean isConstructor = methodInfo.isConstructor();
if ((methodInfo.getModifiers() & ClassFileConstants.AccAbstract) != 0) {
hasAbstractMethods = true;
}
if ((isConstructor && needConstructor) || (!isConstructor && needMethod)) {
AbstractMethodDeclaration method = convert(sourceMethod, methodInfo, compilationResult);
if (isAbstract || method.isAbstract()) {
// fix-up flag
method.modifiers |= ExtraCompilerModifiers.AccSemicolonBody;
}
type.methods[extraConstructor + index++] = method;
}
}
if (hasAbstractMethods)
type.bits |= ASTNode.HasAbstractMethods;
}
return type;
}
use of org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration in project lombok by rzwitserloot.
the class HandleBuilder method makeSimpleSetterMethodForBuilder.
private void makeSimpleSetterMethodForBuilder(EclipseNode builderType, EclipseNode fieldNode, EclipseNode sourceNode, boolean fluent, boolean chain) {
TypeDeclaration td = (TypeDeclaration) builderType.get();
AbstractMethodDeclaration[] existing = td.methods;
if (existing == null)
existing = EMPTY;
int len = existing.length;
FieldDeclaration fd = (FieldDeclaration) fieldNode.get();
char[] name = fd.name;
for (int i = 0; i < len; i++) {
if (!(existing[i] instanceof MethodDeclaration))
continue;
char[] existingName = existing[i].selector;
if (Arrays.equals(name, existingName) && !isTolerate(fieldNode, existing[i]))
return;
}
String setterName = fluent ? fieldNode.getName() : HandlerUtil.buildAccessorName("set", fieldNode.getName());
MethodDeclaration setter = HandleSetter.createSetter(td, fieldNode, setterName, chain, ClassFileConstants.AccPublic, sourceNode, Collections.<Annotation>emptyList(), Collections.<Annotation>emptyList());
injectMethod(builderType, setter);
}
Aggregations