diff options
author | Rawi01 <Rawi01@users.noreply.github.com> | 2021-02-07 12:01:34 +0100 |
---|---|---|
committer | Rawi01 <Rawi01@users.noreply.github.com> | 2021-02-07 12:01:34 +0100 |
commit | a62ffaa280acd09892755897c1c97f5d06f02be5 (patch) | |
tree | 38a20fcfda2eb52e2401f9961cdb587cda470c40 /src/eclipseAgent/lombok/eclipse/agent | |
parent | ed4ced1a3bc694f9a9903b5a15b2641236ff2f92 (diff) | |
download | lombok-a62ffaa280acd09892755897c1c97f5d06f02be5.tar.gz lombok-a62ffaa280acd09892755897c1c97f5d06f02be5.tar.bz2 lombok-a62ffaa280acd09892755897c1c97f5d06f02be5.zip |
Move eclipse only code for Delegate into own class
Diffstat (limited to 'src/eclipseAgent/lombok/eclipse/agent')
3 files changed, 163 insertions, 133 deletions
diff --git a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java index 488d6eee..893e49c5 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java +++ b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2020 The Project Lombok Authors. + * Copyright (C) 2009-2021 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -697,13 +697,13 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable { .decisionMethod(new Hook("lombok.launch.PatchFixesHider$Delegate", "handleDelegateForType", "boolean", "java.lang.Object")) .build()); - sm.addScript(ScriptBuilder.setSymbolDuringMethodCall() + sm.addScriptIfWitness(OSGI_TYPES, ScriptBuilder.setSymbolDuringMethodCall() .target(new MethodTarget("org.eclipse.jdt.internal.core.SelectionRequestor", "acceptSourceMethod")) .callToWrap(new Hook("org.eclipse.jdt.core.IType", "getMethods", "org.eclipse.jdt.core.IMethod[]")) .symbol("lombok.skipdelegates") .build()); - sm.addScript(ScriptBuilder.addField() + sm.addScriptIfWitness(OSGI_TYPES, ScriptBuilder.addField() .fieldName("$delegateMethods") .fieldType("Ljava/util/Map;") .setPublic() @@ -711,10 +711,10 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable { .targetClass("org.eclipse.jdt.internal.core.CompilationUnit") .build()); - sm.addScript(ScriptBuilder.wrapReturnValue() + sm.addScriptIfWitness(OSGI_TYPES, ScriptBuilder.wrapReturnValue() .target(new MethodTarget("org.eclipse.jdt.internal.core.SourceTypeElementInfo", "getChildren", "org.eclipse.jdt.core.IJavaElement[]")) .request(StackRequest.RETURN_VALUE, StackRequest.THIS) - .wrapMethod(new Hook("lombok.launch.PatchFixesHider$Delegate", "getChildren", "java.lang.Object[]", "java.lang.Object", "java.lang.Object")) + .wrapMethod(new Hook("lombok.launch.PatchFixesHider$Delegate", "addGeneratedDelegateMethods", "java.lang.Object[]", "java.lang.Object", "java.lang.Object")) .build()); } diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java index 99367a22..d7b17598 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2020 The Project Lombok Authors. + * Copyright (C) 2010-2021 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -148,7 +148,9 @@ public class PatchDelegate { CompilationUnitDeclaration cud = scope.compilationUnitScope().referenceContext; if (scope == scope.compilationUnitScope().topLevelTypes[0].scope) { - cleanupDelegateMethods(cud); + if (eclipseAvailable) { + EclipseOnlyMethods.cleanupDelegateMethods(cud); + } } if (!hasDelegateMarkedFieldsOrMethods(scope.referenceContext)) return false; @@ -195,8 +197,8 @@ public class PatchDelegate { } } finally { stack.remove(stack.size() - 1); - if (stack.isEmpty()) { - notifyDelegateMethodsAdded(cud); + if (stack.isEmpty() && eclipseAvailable) { + EclipseOnlyMethods.notifyDelegateMethodsAdded(cud); } } } @@ -204,16 +206,6 @@ public class PatchDelegate { return false; } - public static IJavaElement[] getChildren(IJavaElement[] returnValue, SourceTypeElementInfo javaElement) { - if (Symbols.hasSymbol("lombok.skipdelegates")) return returnValue; - - List<SourceMethod> delegateMethods = getDelegateMethods((SourceType) javaElement.getHandle()); - if (delegateMethods != null) { - return concat(returnValue, delegateMethods.toArray(new IJavaElement[0]), IJavaElement.class); - } - return returnValue; - } - /** * Returns a string containing the signature of a method that appears (erased) at least twice in the list. * If no duplicates are present, {@code null} is returned. @@ -459,11 +451,7 @@ public class PatchDelegate { private static void generateDelegateMethods(EclipseNode typeNode, List<BindingTuple> methods, DelegateReceiver delegateReceiver) { CompilationUnitDeclaration top = (CompilationUnitDeclaration) typeNode.top().get(); - - String qualifiedName = new String(CharOperation.concatWith(getQualifiedInnerName(typeNode.up(), typeNode.getName().toCharArray()), '$')); - SourceType sourceType = getSourceType(top, qualifiedName); - List<SourceMethod> delegateSourceMethods = getDelegateMethods(sourceType); - + List<MethodDeclaration> addedMethods = new ArrayList<MethodDeclaration>(); for (BindingTuple pair : methods) { EclipseNode annNode = typeNode.getAst().get(pair.responsible); MethodDeclaration method = createDelegateMethod(pair.fieldName, typeNode, pair, top.compilationResult, annNode, delegateReceiver); @@ -471,12 +459,12 @@ public class PatchDelegate { SetGeneratedByVisitor visitor = new SetGeneratedByVisitor(annNode.get()); method.traverse(visitor, ((TypeDeclaration)typeNode.get()).scope); injectMethod(typeNode, method); - - if (delegateSourceMethods != null) { - delegateSourceMethods.add(DelegateSourceMethod.forMethodDeclaration(sourceType, method)); - } + addedMethods.add(method); } } + if (eclipseAvailable) { + EclipseOnlyMethods.collectGeneratedDelegateMethods(top, typeNode, addedMethods); + } } public static void checkConflictOfTypeVarNames(BindingTuple binding, EclipseNode typeNode) throws CantMakeDelegates { @@ -722,127 +710,159 @@ public class PatchDelegate { return method; } - private static void cleanupDelegateMethods(CompilationUnitDeclaration cud) { - CompilationUnit compilationUnit = getCompilationUnit(cud); - if (compilationUnit != null) { - EclipseAugments.CompilationUnit_delegateMethods.clear(compilationUnit); + private static boolean eclipseAvailable = true; + static { + try { + CompilationUnit.class.getName(); + } catch (Throwable t) { + eclipseAvailable = false; } } - private static boolean javaModelManagerAvailable = true; - private static void notifyDelegateMethodsAdded(CompilationUnitDeclaration cud) { - CompilationUnit compilationUnit = getCompilationUnit(cud); - if (compilationUnit != null && javaModelManagerAvailable) { - try { - DeltaProcessor deltaProcessor = JavaModelManager.getJavaModelManager().getDeltaProcessor(); - deltaProcessor.fire(new JavaElementDelta(compilationUnit), ElementChangedEvent.POST_CHANGE); - } catch (NoClassDefFoundError e) { - javaModelManagerAvailable = false; - } - } + public static Object[] addGeneratedDelegateMethods(Object[] returnValue, Object javaElement) { + if (Symbols.hasSymbol("lombok.skipdelegates")) return returnValue; + if (!eclipseAvailable) return returnValue; + + return EclipseOnlyMethods.addGeneratedDelegateMethodsToChildren(returnValue, javaElement); } - private static CompilationUnit getCompilationUnit(Object iCompilationUnit) { - if (iCompilationUnit instanceof CompilationUnit) { - CompilationUnit compilationUnit = (CompilationUnit) iCompilationUnit; - return compilationUnit.originalFromClone(); + public static class EclipseOnlyMethods { + private static void cleanupDelegateMethods(CompilationUnitDeclaration cud) { + CompilationUnit compilationUnit = getCompilationUnit(cud); + if (compilationUnit != null) { + EclipseAugments.CompilationUnit_delegateMethods.clear(compilationUnit); + } } - return null; - } - - private static CompilationUnit getCompilationUnit(CompilationUnitDeclaration cud) { - return getCompilationUnit(cud.compilationResult.compilationUnit); - } - - private static final class DelegateSourceMethod extends SourceMethod { - private DelegateSourceMethodInfo sourceMethodInfo; - private static DelegateSourceMethod forMethodDeclaration(JavaElement parent, MethodDeclaration method) { - Argument[] arguments = method.arguments != null ? method.arguments : new Argument[0]; - String[] parameterTypes = new String[arguments.length]; - for (int i = 0; i < arguments.length; i++) { - parameterTypes[i] = Signature.createTypeSignature(CharOperation.concatWith(arguments[i].type.getParameterizedTypeName(), '.'), false); + public static void collectGeneratedDelegateMethods(CompilationUnitDeclaration top, EclipseNode typeNode, List<MethodDeclaration> addedMethods) { + String qualifiedName = new String(CharOperation.concatWith(getQualifiedInnerName(typeNode.up(), typeNode.getName().toCharArray()), '$')); + SourceType sourceType = getSourceType(top, qualifiedName); + List<SourceMethod> generatedMethods = getGeneratedMethods(sourceType); + if (generatedMethods == null) return; + + for (MethodDeclaration md : addedMethods) { + generatedMethods.add(DelegateSourceMethod.forMethodDeclaration(sourceType, md)); } - return new DelegateSourceMethod(parent, new String(method.selector), parameterTypes, method); } - private DelegateSourceMethod(JavaElement parent, String name, String[] parameterTypes, MethodDeclaration md) { - super(parent, name, parameterTypes); - sourceMethodInfo = new DelegateSourceMethodInfo(this, md); + public static Object[] addGeneratedDelegateMethodsToChildren(Object[] returnValue, Object javaElement) { + List<SourceMethod> delegateMethods = getGeneratedMethods((SourceType) ((SourceTypeElementInfo) javaElement).getHandle()); + if (delegateMethods != null) { + return concat((IJavaElement[]) returnValue, delegateMethods.toArray(new IJavaElement[0]), IJavaElement.class); + } + return returnValue; } - @Override public Object getElementInfo() throws JavaModelException { - return sourceMethodInfo; + private static void notifyDelegateMethodsAdded(CompilationUnitDeclaration cud) { + CompilationUnit compilationUnit = getCompilationUnit(cud); + if (compilationUnit != null) { + DeltaProcessor deltaProcessor = JavaModelManager.getJavaModelManager().getDeltaProcessor(); + deltaProcessor.fire(new JavaElementDelta(compilationUnit), ElementChangedEvent.POST_CHANGE); + } } - /** - * Disable refactoring for delegate methods - */ - @Override public boolean isReadOnly() { - return true; + private static CompilationUnit getCompilationUnit(Object iCompilationUnit) { + if (iCompilationUnit instanceof CompilationUnit) { + CompilationUnit compilationUnit = (CompilationUnit) iCompilationUnit; + return compilationUnit.originalFromClone(); + } + return null; } - /** - * This is required to prevent duplicate entries in the outline - */ - @Override public boolean equals(Object o) { - return this == o; + private static CompilationUnit getCompilationUnit(CompilationUnitDeclaration cud) { + return getCompilationUnit(cud.compilationResult.compilationUnit); } - public static final class DelegateSourceMethodInfo extends SourceMethodInfo { - DelegateSourceMethodInfo(DelegateSourceMethod delegateSourceMethod, MethodDeclaration md) { - int pS = md.sourceStart; - int pE = md.sourceEnd; - - Argument[] methodArguments = md.arguments != null ? md.arguments : new Argument[0]; - char[][] argumentNames = new char[methodArguments.length][]; - arguments = new ILocalVariable[methodArguments.length]; - for (int i = 0; i < methodArguments.length; i++) { - Argument argument = methodArguments[i]; - argumentNames[i] = argument.name; - arguments[i] = new LocalVariable(delegateSourceMethod, new String(argument.name), pS, pE, pS, pS, delegateSourceMethod.getParameterTypes()[i], argument.annotations, argument.modifiers, true); + private static final class DelegateSourceMethod extends SourceMethod { + private DelegateSourceMethodInfo sourceMethodInfo; + + private static DelegateSourceMethod forMethodDeclaration(JavaElement parent, MethodDeclaration method) { + Argument[] arguments = method.arguments != null ? method.arguments : new Argument[0]; + String[] parameterTypes = new String[arguments.length]; + for (int i = 0; i < arguments.length; i++) { + parameterTypes[i] = Signature.createTypeSignature(CharOperation.concatWith(arguments[i].type.getParameterizedTypeName(), '.'), false); + } + return new DelegateSourceMethod(parent, new String(method.selector), parameterTypes, method); + } + + private DelegateSourceMethod(JavaElement parent, String name, String[] parameterTypes, MethodDeclaration md) { + super(parent, name, parameterTypes); + sourceMethodInfo = new DelegateSourceMethodInfo(this, md); + } + + @Override public Object getElementInfo() throws JavaModelException { + return sourceMethodInfo; + } + + /** + * Disable refactoring for delegate methods + */ + @Override public boolean isReadOnly() { + return true; + } + + /** + * This is required to prevent duplicate entries in the outline + */ + @Override public boolean equals(Object o) { + return this == o; + } + + public static final class DelegateSourceMethodInfo extends SourceMethodInfo { + DelegateSourceMethodInfo(DelegateSourceMethod delegateSourceMethod, MethodDeclaration md) { + int pS = md.sourceStart; + int pE = md.sourceEnd; + + Argument[] methodArguments = md.arguments != null ? md.arguments : new Argument[0]; + char[][] argumentNames = new char[methodArguments.length][]; + arguments = new ILocalVariable[methodArguments.length]; + for (int i = 0; i < methodArguments.length; i++) { + Argument argument = methodArguments[i]; + argumentNames[i] = argument.name; + arguments[i] = new LocalVariable(delegateSourceMethod, new String(argument.name), pS, pE, pS, pS, delegateSourceMethod.getParameterTypes()[i], argument.annotations, argument.modifiers, true); + } + setArgumentNames(argumentNames); + + setSourceRangeStart(pS); + setSourceRangeEnd(pE); + setNameSourceStart(pS); + setNameSourceEnd(pE); + + setExceptionTypeNames(CharOperation.NO_CHAR_CHAR); + setReturnType(md.returnType == null ? new char[]{'v', 'o','i', 'd'} : CharOperation.concatWith(md.returnType.getParameterizedTypeName(), '.')); + setFlags(md.modifiers); } - setArgumentNames(argumentNames); - - setSourceRangeStart(pS); - setSourceRangeEnd(pE); - setNameSourceStart(pS); - setNameSourceEnd(pE); - - setExceptionTypeNames(CharOperation.NO_CHAR_CHAR); - setReturnType(md.returnType == null ? new char[]{'v', 'o','i', 'd'} : CharOperation.concatWith(md.returnType.getParameterizedTypeName(), '.')); - setFlags(md.modifiers); } } - } - - private static List<SourceMethod> getDelegateMethods(SourceType sourceType) { - if (sourceType != null) { - CompilationUnit compilationUnit = getCompilationUnit(sourceType.getCompilationUnit()); - if (compilationUnit != null) { - ConcurrentMap<String, List<SourceMethod>> map = EclipseAugments.CompilationUnit_delegateMethods.setIfAbsent(compilationUnit, new ConcurrentHashMap<String, List<SourceMethod>>()); - List<SourceMethod> newList = new ArrayList<SourceMethod>(); - List<SourceMethod> oldList = map.putIfAbsent(sourceType.getTypeQualifiedName(), newList); - return oldList != null ? oldList : newList; + + private static List<SourceMethod> getGeneratedMethods(SourceType sourceType) { + if (sourceType != null) { + CompilationUnit compilationUnit = getCompilationUnit(sourceType.getCompilationUnit()); + if (compilationUnit != null) { + ConcurrentMap<String, List<SourceMethod>> map = EclipseAugments.CompilationUnit_delegateMethods.setIfAbsent(compilationUnit, new ConcurrentHashMap<String, List<SourceMethod>>()); + List<SourceMethod> newList = new ArrayList<SourceMethod>(); + List<SourceMethod> oldList = map.putIfAbsent(sourceType.getTypeQualifiedName(), newList); + return oldList != null ? oldList : newList; + } } + return null; } - return null; - } - - private static SourceType getSourceType(CompilationUnitDeclaration cud, String typeName) { - CompilationUnit compilationUnit = getCompilationUnit(cud); - if (compilationUnit != null) { - try { - for (IType type : compilationUnit.getAllTypes()) { - if (type instanceof SourceType && type.getTypeQualifiedName().equals(typeName)) { - return (SourceType) type; + + private static SourceType getSourceType(CompilationUnitDeclaration cud, String typeName) { + CompilationUnit compilationUnit = getCompilationUnit(cud); + if (compilationUnit != null) { + try { + for (IType type : compilationUnit.getAllTypes()) { + if (type instanceof SourceType && type.getTypeQualifiedName().equals(typeName)) { + return (SourceType) type; + } } + } catch (JavaModelException e) { + // Ignore } - } catch (JavaModelException e) { - // Ignore } + return null; } - return null; } private static final class Reflection { diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegatePortal.java b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegatePortal.java index 3d8c86c9..01e4bb18 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegatePortal.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegatePortal.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012-2020 The Project Lombok Authors. + * Copyright (C) 2012-2021 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -31,34 +31,44 @@ public class PatchDelegatePortal { static final String SOURCE_TYPE_ELEMENT_INFO = "org.eclipse.jdt.internal.core.SourceTypeElementInfo"; public static boolean handleDelegateForType(Object classScope) { - Boolean v = (Boolean) Permit.invokeSneaky(Reflection.problem, Reflection.handleDelegateForType, null, classScope); + Boolean v = (Boolean) Permit.invokeSneaky(Reflection.problemHandleDelegate, Reflection.handleDelegateForType, null, classScope); if (v == null) return false; return v.booleanValue(); } - public static Object[] getChildren(Object returnValue, Object javaElement) { - return (Object[]) Permit.invokeSneaky(Reflection.problem, Reflection.getChildren, null, returnValue, javaElement); + public static Object[] addGeneratedDelegateMethods(Object returnValue, Object javaElement) { + return (Object[]) Permit.invokeSneaky(Reflection.problemAddGeneratedDelegateMethods, Reflection.addGeneratedDelegateMethods, null, returnValue, javaElement); } private static final class Reflection { public static final Method handleDelegateForType; - public static final Method getChildren; - public static final Throwable problem; + public static final Method addGeneratedDelegateMethods; + public static final Throwable problemHandleDelegate; + public static final Throwable problemAddGeneratedDelegateMethods; static { Method m = null, n = null; - Throwable problem_ = null; + Throwable problemHandleDelegate_ = null; + Throwable problemAddGeneratedDelegateMethods_ = null; try { m = Permit.getMethod(PatchDelegate.class, "handleDelegateForType", Class.forName(CLASS_SCOPE)); - n = Permit.getMethod(PatchDelegate.class, "getChildren", Class.forName(I_JAVA_ELEMENT_ARRAY), Class.forName(SOURCE_TYPE_ELEMENT_INFO)); } catch (Throwable t) { // That's problematic, but as long as no local classes are used we don't actually need it. // Better fail on local classes than crash altogether. - problem_ = t; + problemHandleDelegate_ = t; } handleDelegateForType = m; - getChildren = n; - problem = problem_; + problemHandleDelegate = problemHandleDelegate_; + + try { + n = Permit.getMethod(PatchDelegate.class, "addGeneratedDelegateMethods", Object[].class, Object.class); + } catch (Throwable t) { + // That's problematic, but as long as no local classes are used we don't actually need it. + // Better fail on local classes than crash altogether. + problemAddGeneratedDelegateMethods_ = t; + } + addGeneratedDelegateMethods = n; + problemAddGeneratedDelegateMethods = problemAddGeneratedDelegateMethods_; } } } |