use of org.eclipse.jdt.internal.compiler.ast.TypeDeclaration in project lombok by rzwitserloot.
the class EclipseHandlerUtil method cloneSelfType.
public static TypeReference cloneSelfType(EclipseNode context, ASTNode source) {
int pS = source == null ? 0 : source.sourceStart, pE = source == null ? 0 : source.sourceEnd;
long p = (long) pS << 32 | pE;
EclipseNode type = context;
TypeReference result = null;
while (type != null && type.getKind() != Kind.TYPE) type = type.up();
if (type != null && type.get() instanceof TypeDeclaration) {
TypeDeclaration typeDecl = (TypeDeclaration) type.get();
if (typeDecl.typeParameters != null && typeDecl.typeParameters.length > 0) {
TypeReference[] refs = new TypeReference[typeDecl.typeParameters.length];
int idx = 0;
for (TypeParameter param : typeDecl.typeParameters) {
TypeReference typeRef = new SingleTypeReference(param.name, (long) param.sourceStart << 32 | param.sourceEnd);
if (source != null)
setGeneratedBy(typeRef, source);
refs[idx++] = typeRef;
}
result = new ParameterizedSingleTypeReference(typeDecl.name, refs, 0, p);
} else {
result = new SingleTypeReference(((TypeDeclaration) type.get()).name, p);
}
}
if (result != null && source != null)
setGeneratedBy(result, source);
return result;
}
use of org.eclipse.jdt.internal.compiler.ast.TypeDeclaration in project lombok by rzwitserloot.
the class EclipseHandlerUtil method injectType.
/**
* Adds an inner type (class, interface, enum) to the given type. Cannot inject top-level types.
*
* @param typeNode parent type to inject new type into
* @param type New type (class, interface, etc) to inject.
*/
public static EclipseNode injectType(final EclipseNode typeNode, final TypeDeclaration type) {
type.annotations = addSuppressWarningsAll(typeNode, type, type.annotations);
type.annotations = addGenerated(typeNode, type, type.annotations);
TypeDeclaration parent = (TypeDeclaration) typeNode.get();
if (parent.memberTypes == null) {
parent.memberTypes = new TypeDeclaration[] { type };
} else {
TypeDeclaration[] newArray = new TypeDeclaration[parent.memberTypes.length + 1];
System.arraycopy(parent.memberTypes, 0, newArray, 0, parent.memberTypes.length);
newArray[parent.memberTypes.length] = type;
parent.memberTypes = newArray;
}
return typeNode.add(type, Kind.TYPE);
}
use of org.eclipse.jdt.internal.compiler.ast.TypeDeclaration in project lombok by rzwitserloot.
the class PatchDelegate method fillMethodBindingsForMethods.
private static void fillMethodBindingsForMethods(CompilationUnitDeclaration cud, ClassScope scope, List<BindingTuple> methodsToDelegate) {
TypeDeclaration decl = scope.referenceContext;
if (decl == null)
return;
if (decl.methods != null)
for (AbstractMethodDeclaration methodDecl : decl.methods) {
if (methodDecl.annotations == null)
continue;
for (Annotation ann : methodDecl.annotations) {
if (!isDelegate(ann, decl))
continue;
if (Annotation_applied.getAndSet(ann, true))
continue;
if (!(methodDecl instanceof MethodDeclaration)) {
EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true);
eclipseAst.get(ann).addError(LEGALITY_OF_DELEGATE);
break;
}
if (methodDecl.arguments != null) {
EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true);
eclipseAst.get(ann).addError(LEGALITY_OF_DELEGATE);
break;
}
if ((methodDecl.modifiers & ClassFileConstants.AccStatic) != 0) {
EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true);
eclipseAst.get(ann).addError(LEGALITY_OF_DELEGATE);
break;
}
MethodDeclaration method = (MethodDeclaration) methodDecl;
List<ClassLiteralAccess> rawTypes = rawTypes(ann, "types");
List<ClassLiteralAccess> excludedRawTypes = rawTypes(ann, "excludes");
List<BindingTuple> methodsToExclude = new ArrayList<BindingTuple>();
List<BindingTuple> methodsToDelegateForThisAnn = new ArrayList<BindingTuple>();
try {
for (ClassLiteralAccess cla : excludedRawTypes) {
addAllMethodBindings(methodsToExclude, cla.type.resolveType(decl.initializerScope), new HashSet<String>(), method.selector, ann);
}
Set<String> banList = new HashSet<String>();
for (BindingTuple excluded : methodsToExclude) banList.add(printSig(excluded.parameterized));
if (rawTypes.isEmpty()) {
if (method.returnType == null)
continue;
addAllMethodBindings(methodsToDelegateForThisAnn, method.returnType.resolveType(decl.initializerScope), banList, method.selector, ann);
} else {
for (ClassLiteralAccess cla : rawTypes) {
addAllMethodBindings(methodsToDelegateForThisAnn, cla.type.resolveType(decl.initializerScope), banList, method.selector, ann);
}
}
} catch (DelegateRecursion e) {
EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true);
eclipseAst.get(ann).addError(String.format(RECURSION_NOT_ALLOWED, new String(e.member), new String(e.type)));
break;
}
// Not doing this right now because of problems - see commented-out-method for info.
// removeExistingMethods(methodsToDelegate, decl, scope);
String dupe = containsDuplicates(methodsToDelegateForThisAnn);
if (dupe != null) {
EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true);
eclipseAst.get(ann).addError("The method '" + dupe + "' is being delegated by more than one specified type.");
} else {
methodsToDelegate.addAll(methodsToDelegateForThisAnn);
}
}
}
}
use of org.eclipse.jdt.internal.compiler.ast.TypeDeclaration in project lombok by rzwitserloot.
the class PatchDelegate method handleDelegateForType.
public static boolean handleDelegateForType(ClassScope scope) {
if (TransformEclipseAST.disableLombok)
return false;
if (!hasDelegateMarkedFieldsOrMethods(scope.referenceContext))
return false;
List<ClassScopeEntry> stack = visited.get();
StringBuilder corrupted = null;
for (ClassScopeEntry entry : stack) {
if (corrupted != null) {
corrupted.append(" -> ").append(nameOfScope(entry.scope));
} else if (entry.scope == scope) {
corrupted = new StringBuilder().append(nameOfScope(scope));
}
}
if (corrupted != null) {
boolean found = false;
String path = corrupted.toString();
for (ClassScopeEntry entry : stack) {
if (!found && entry.scope == scope)
found = true;
if (found)
entry.corruptedPath = path;
}
} else {
ClassScopeEntry entry = new ClassScopeEntry(scope);
stack.add(entry);
try {
TypeDeclaration decl = scope.referenceContext;
if (decl != null) {
CompilationUnitDeclaration cud = scope.compilationUnitScope().referenceContext;
EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true);
List<BindingTuple> methodsToDelegate = new ArrayList<BindingTuple>();
fillMethodBindingsForFields(cud, scope, methodsToDelegate);
if (entry.corruptedPath != null) {
eclipseAst.get(scope.referenceContext).addError("No @Delegate methods created because there's a loop: " + entry.corruptedPath);
} else {
generateDelegateMethods(eclipseAst.get(decl), methodsToDelegate, DelegateReceiver.FIELD);
}
methodsToDelegate.clear();
fillMethodBindingsForMethods(cud, scope, methodsToDelegate);
if (entry.corruptedPath != null) {
eclipseAst.get(scope.referenceContext).addError("No @Delegate methods created because there's a loop: " + entry.corruptedPath);
} else {
generateDelegateMethods(eclipseAst.get(decl), methodsToDelegate, DelegateReceiver.METHOD);
}
}
} finally {
stack.remove(stack.size() - 1);
}
}
return false;
}
Aggregations