private void assertMembersNamesAreUnique() { Map<String, FieldNode> allDslCollectionFieldNodesOfHierarchy = new HashMap<String, FieldNode>(); for (ClassNode level : ASTHelper.getHierarchyOfDSLObjectAncestors(annotatedClass)) { for (FieldNode field : level.getFields()) { if (!ASTHelper.isListOrMap(field.getType())) continue; String memberName = getElementNameForCollectionField(field); FieldNode conflictingField = allDslCollectionFieldNodesOfHierarchy.get(memberName); if (conflictingField != null) { addCompileError( String.format( "Member name %s is used more than once: %s:%s and %s:%s", memberName, field.getOwner().getName(), field.getName(), conflictingField.getOwner().getName(), conflictingField.getName()), field); return; } allDslCollectionFieldNodesOfHierarchy.put(memberName, field); } } }
public String toString() { if (sources == null || sources.isEmpty()) return super.toString(); Set s = sources.keySet(); for (Object o : s) { return "CompilationUnit: source is " + o.toString(); } return "CompilationUnit: null"; }
/** * This method is used to add "bridge" methods for private methods of an inner/outer class, so * that the outer class is capable of calling them. It does basically the same job as access$000 * like methods in Java. * * @param node an inner/outer class node for which to generate bridge methods */ @SuppressWarnings("unchecked") private void addPrivateBridgeMethods(final ClassNode node) { Set<ASTNode> accessedMethods = (Set<ASTNode>) node.getNodeMetaData(StaticTypesMarker.PV_METHODS_ACCESS); if (accessedMethods == null) return; List<MethodNode> methods = new ArrayList<MethodNode>(node.getAllDeclaredMethods()); Map<MethodNode, MethodNode> privateBridgeMethods = (Map<MethodNode, MethodNode>) node.getNodeMetaData(PRIVATE_BRIDGE_METHODS); if (privateBridgeMethods != null) { // private bridge methods already added return; } privateBridgeMethods = new HashMap<MethodNode, MethodNode>(); int i = -1; final int access = Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC; for (MethodNode method : methods) { if (accessedMethods.contains(method)) { i++; Parameter[] methodParameters = method.getParameters(); Parameter[] newParams = new Parameter[methodParameters.length + 1]; System.arraycopy(methodParameters, 0, newParams, 1, methodParameters.length); newParams[0] = new Parameter(node.getPlainNodeReference(), "$that"); Expression arguments; if (method.getParameters() == null || method.getParameters().length == 0) { arguments = ArgumentListExpression.EMPTY_ARGUMENTS; } else { List<Expression> args = new LinkedList<Expression>(); for (Parameter parameter : methodParameters) { args.add(new VariableExpression(parameter)); } arguments = new ArgumentListExpression(args); } Expression receiver = method.isStatic() ? new ClassExpression(node) : new VariableExpression(newParams[0]); MethodCallExpression mce = new MethodCallExpression(receiver, method.getName(), arguments); mce.setMethodTarget(method); ExpressionStatement returnStatement = new ExpressionStatement(mce); MethodNode bridge = node.addMethod( "access$" + i, access, method.getReturnType(), newParams, method.getExceptions(), returnStatement); privateBridgeMethods.put(method, bridge); bridge.addAnnotation(new AnnotationNode(COMPILESTATIC_CLASSNODE)); } } if (!privateBridgeMethods.isEmpty()) { node.setNodeMetaData(PRIVATE_BRIDGE_METHODS, privateBridgeMethods); } }
public Expression transform(Expression exp) { if (exp == null) return null; if (exp.getClass() == VariableExpression.class) { return transformVariableExpression((VariableExpression) exp); } if (exp.getClass() == BinaryExpression.class) { return transformBinaryExpression((BinaryExpression) exp); } if (exp.getClass() == PropertyExpression.class) { return transformPropertyExpression((PropertyExpression) exp); } if (exp.getClass() == MethodCallExpression.class) { return transformMethodCallExpression((MethodCallExpression) exp); } if (exp.getClass() == ClosureExpression.class) { return transformClosureExpression((ClosureExpression) exp); } if (exp.getClass() == ConstructorCallExpression.class) { return transformConstructorCallExpression((ConstructorCallExpression) exp); } if (exp.getClass() == ArgumentListExpression.class) { Expression result = exp.transformExpression(this); if (inPropertyExpression) { foundArgs = result; } return result; } if (exp instanceof ConstantExpression) { Expression result = exp.transformExpression(this); if (inPropertyExpression) { foundConstant = result; } if (inAnnotation && exp instanceof AnnotationConstantExpression) { ConstantExpression ce = (ConstantExpression) result; if (ce.getValue() instanceof AnnotationNode) { // replicate a little bit of AnnotationVisitor here // because we can't wait until later to do this AnnotationNode an = (AnnotationNode) ce.getValue(); Map<String, Expression> attributes = an.getMembers(); for (Map.Entry<String, Expression> entry : attributes.entrySet()) { Expression attrExpr = transform(entry.getValue()); entry.setValue(attrExpr); } } } return result; } return exp.transformExpression(this); }
private Expression transformMapEntryExpression( MapEntryExpression me, ClassNode constructorCallType) { Expression key = me.getKeyExpression(); Expression value = me.getValueExpression(); ModuleNode module = currentClass.getModule(); if (module != null && key instanceof ConstantExpression) { Map<String, ImportNode> importNodes = module.getStaticImports(); if (importNodes.containsKey(key.getText())) { ImportNode importNode = importNodes.get(key.getText()); if (importNode.getType().equals(constructorCallType)) { String newKey = importNode.getFieldName(); return new MapEntryExpression( new ConstantExpression(newKey), value.transformExpression(this)); } } } return me; }
private Expression findStaticFieldOrPropAccessorImportFromModule(String name) { ModuleNode module = currentClass.getModule(); if (module == null) return null; Map<String, ImportNode> importNodes = module.getStaticImports(); Expression expression; String accessorName = getAccessorName(name); // look for one of these: // import static MyClass.setProp [as setOtherProp] // import static MyClass.getProp [as getOtherProp] // when resolving prop reference if (importNodes.containsKey(accessorName)) { ImportNode importNode = importNodes.get(accessorName); expression = findStaticPropertyAccessorByFullName(importNode.getType(), importNode.getFieldName()); if (expression != null) return expression; expression = findStaticPropertyAccessor( importNode.getType(), getPropNameForAccessor(importNode.getFieldName())); if (expression != null) return expression; } if (accessorName.startsWith("get")) { accessorName = "is" + accessorName.substring(3); if (importNodes.containsKey(accessorName)) { ImportNode importNode = importNodes.get(accessorName); expression = findStaticPropertyAccessorByFullName(importNode.getType(), importNode.getFieldName()); if (expression != null) return expression; expression = findStaticPropertyAccessor( importNode.getType(), getPropNameForAccessor(importNode.getFieldName())); if (expression != null) return expression; } } // look for one of these: // import static MyClass.prop [as otherProp] // when resolving prop or field reference if (importNodes.containsKey(name)) { ImportNode importNode = importNodes.get(name); expression = findStaticPropertyAccessor(importNode.getType(), importNode.getFieldName()); if (expression != null) return expression; expression = findStaticField(importNode.getType(), importNode.getFieldName()); if (expression != null) return expression; } // look for one of these: // import static MyClass.* // when resolving prop or field reference for (ImportNode importNode : module.getStaticStarImports().values()) { ClassNode node = importNode.getType(); expression = findStaticPropertyAccessor(node, name); if (expression != null) return expression; expression = findStaticField(node, name); if (expression != null) return expression; } return null; }
@SuppressWarnings("unchecked") private boolean makeGetPrivateFieldWithBridgeMethod( final Expression receiver, final ClassNode receiverType, final String fieldName, final boolean safe, final boolean implicitThis) { FieldNode field = receiverType.getField(fieldName); ClassNode classNode = controller.getClassNode(); if (field != null && Modifier.isPrivate(field.getModifiers()) && (StaticInvocationWriter.isPrivateBridgeMethodsCallAllowed(receiverType, classNode) || StaticInvocationWriter.isPrivateBridgeMethodsCallAllowed(classNode, receiverType)) && !receiverType.equals(classNode)) { Map<String, MethodNode> accessors = (Map<String, MethodNode>) receiverType .redirect() .getNodeMetaData(StaticCompilationMetadataKeys.PRIVATE_FIELDS_ACCESSORS); if (accessors != null) { MethodNode methodNode = accessors.get(fieldName); if (methodNode != null) { MethodCallExpression mce = new MethodCallExpression( receiver, methodNode.getName(), new ArgumentListExpression( field.isStatic() ? new ConstantExpression(null) : receiver)); mce.setMethodTarget(methodNode); mce.setSafe(safe); mce.setImplicitThis(implicitThis); mce.visit(controller.getAcg()); return true; } } } return false; }
/** Adds special accessors for private constants so that inner classes can retrieve them. */ @SuppressWarnings("unchecked") private void addPrivateFieldsAccessors(ClassNode node) { Set<ASTNode> accessedFields = (Set<ASTNode>) node.getNodeMetaData(StaticTypesMarker.PV_FIELDS_ACCESS); if (accessedFields == null) return; Map<String, MethodNode> privateConstantAccessors = (Map<String, MethodNode>) node.getNodeMetaData(PRIVATE_FIELDS_ACCESSORS); if (privateConstantAccessors != null) { // already added return; } int acc = -1; privateConstantAccessors = new HashMap<String, MethodNode>(); final int access = Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC; for (FieldNode fieldNode : node.getFields()) { if (accessedFields.contains(fieldNode)) { acc++; Parameter param = new Parameter(node.getPlainNodeReference(), "$that"); Expression receiver = fieldNode.isStatic() ? new ClassExpression(node) : new VariableExpression(param); Statement stmt = new ExpressionStatement(new PropertyExpression(receiver, fieldNode.getName())); MethodNode accessor = node.addMethod( "pfaccess$" + acc, access, fieldNode.getOriginType(), new Parameter[] {param}, ClassNode.EMPTY_ARRAY, stmt); privateConstantAccessors.put(fieldNode.getName(), accessor); } } node.setNodeMetaData(PRIVATE_FIELDS_ACCESSORS, privateConstantAccessors); }
/** * A loop driver for applying operations to all SourceUnits. Automatically skips units that have * already been processed through the current phase. */ public void applyToSourceUnits(SourceUnitOperation body) throws CompilationFailedException { // GRECLIPSE: start try { iterating = true; // end for (String name : names) { SourceUnit source = sources.get(name); if ((source.phase < phase) || (source.phase == phase && !source.phaseComplete)) { try { body.call(source); // GRECLIPSE: start if (phase == Phases.CONVERSION && getProgressListener() != null && body == phaseOperations[phase].getLast()) { getProgressListener().parseComplete(phase, name); } // end } catch (CompilationFailedException e) { throw e; } catch (Exception e) { GroovyBugError gbe = new GroovyBugError(e); changeBugText(gbe, source); throw gbe; } catch (GroovyBugError e) { changeBugText(e, source); throw e; } } } // GRECLIPSE: start } finally { iterating = false; } // end getErrorCollector().failIfErrors(); }
/** * Dequeues any source units add through addSource and resets the compiler phase to * initialization. * * <p>Note: this does not mean a file is recompiled. If a SourceUnit has already passed a phase it * is skipped until a higher phase is reached. * * @return true if there was a queued source * @throws CompilationFailedException */ protected boolean dequeued() throws CompilationFailedException { boolean dequeue = !queuedSources.isEmpty(); while (!queuedSources.isEmpty()) { SourceUnit su = queuedSources.removeFirst(); String name = su.getName(); // GRECLIPSE: start if (iterating) { GroovyBugError gbe = new GroovyBugError( "Damaging 'names' whilst already iterating. Name getting added is '" + su.getName() + "'"); gbe.printStackTrace(); throw gbe; } // end names.add(name); sources.put(name, su); } if (dequeue) { gotoPhase(Phases.INITIALIZATION); } return dequeue; }
private static void addPhaseOperationsForGlobalTransforms( CompilationUnit compilationUnit, Map<String, URL> transformNames, boolean isFirstScan) { GroovyClassLoader transformLoader = compilationUnit.getTransformLoader(); for (Map.Entry<String, URL> entry : transformNames.entrySet()) { try { Class gTransClass = transformLoader.loadClass(entry.getKey(), false, true, false); // no inspection unchecked GroovyASTTransformation transformAnnotation = (GroovyASTTransformation) gTransClass.getAnnotation(GroovyASTTransformation.class); if (transformAnnotation == null) { compilationUnit .getErrorCollector() .addWarning( new WarningMessage( WarningMessage.POSSIBLE_ERRORS, "Transform Class " + entry.getKey() + " is specified as a global transform in " + entry.getValue().toExternalForm() + " but it is not annotated by " + GroovyASTTransformation.class.getName() + " the global transform associated with it may fail and cause the compilation to fail.", null, null)); continue; } if (ASTTransformation.class.isAssignableFrom(gTransClass)) { final ASTTransformation instance = (ASTTransformation) gTransClass.newInstance(); if (instance instanceof CompilationUnitAware) { ((CompilationUnitAware) instance).setCompilationUnit(compilationUnit); } CompilationUnit.SourceUnitOperation suOp = new CompilationUnit.SourceUnitOperation() { public void call(SourceUnit source) throws CompilationFailedException { instance.visit(new ASTNode[] {source.getAST()}, source); } }; if (isFirstScan) { compilationUnit.addPhaseOperation(suOp, transformAnnotation.phase().getPhaseNumber()); } else { compilationUnit.addNewPhaseOperation( suOp, transformAnnotation.phase().getPhaseNumber()); } } else { compilationUnit .getErrorCollector() .addError( new SimpleMessage( "Transform Class " + entry.getKey() + " specified at " + entry.getValue().toExternalForm() + " is not an ASTTransformation.", null)); } } catch (Exception e) { compilationUnit .getErrorCollector() .addError( new SimpleMessage( "Could not instantiate global transform class " + entry.getKey() + " specified at " + entry.getValue().toExternalForm() + " because of exception " + e.toString(), null)); } } }
private static void doAddGlobalTransforms( ASTTransformationsContext context, boolean isFirstScan) { final CompilationUnit compilationUnit = context.getCompilationUnit(); GroovyClassLoader transformLoader = compilationUnit.getTransformLoader(); Map<String, URL> transformNames = new LinkedHashMap<String, URL>(); try { Enumeration<URL> globalServices = transformLoader.getResources( "META-INF/services/org.codehaus.groovy.transform.ASTTransformation"); while (globalServices.hasMoreElements()) { URL service = globalServices.nextElement(); String className; BufferedReader svcIn = null; try { svcIn = new BufferedReader(new InputStreamReader(service.openStream())); try { className = svcIn.readLine(); } catch (IOException ioe) { compilationUnit .getErrorCollector() .addError( new SimpleMessage( "IOException reading the service definition at " + service.toExternalForm() + " because of exception " + ioe.toString(), null)); continue; } Set<String> disabledGlobalTransforms = compilationUnit.getConfiguration().getDisabledGlobalASTTransformations(); if (disabledGlobalTransforms == null) disabledGlobalTransforms = Collections.emptySet(); while (className != null) { if (!className.startsWith("#") && className.length() > 0) { if (!disabledGlobalTransforms.contains(className)) { if (transformNames.containsKey(className)) { if (!service.equals(transformNames.get(className))) { compilationUnit .getErrorCollector() .addWarning( WarningMessage.POSSIBLE_ERRORS, "The global transform for class " + className + " is defined in both " + transformNames.get(className).toExternalForm() + " and " + service.toExternalForm() + " - the former definition will be used and the latter ignored.", null, null); } } else { transformNames.put(className, service); } } } try { className = svcIn.readLine(); } catch (IOException ioe) { compilationUnit .getErrorCollector() .addError( new SimpleMessage( "IOException reading the service definition at " + service.toExternalForm() + " because of exception " + ioe.toString(), null)); //noinspection UnnecessaryContinue continue; } } } finally { if (svcIn != null) svcIn.close(); } } } catch (IOException e) { // FIXME the warning message will NPE with what I have :( compilationUnit .getErrorCollector() .addError( new SimpleMessage( "IO Exception attempting to load global transforms:" + e.getMessage(), null)); } try { Class.forName("java.lang.annotation.Annotation"); // test for 1.5 JVM } catch (Exception e) { // we failed, notify the user StringBuffer sb = new StringBuffer(); sb.append("Global ASTTransformations are not enabled in retro builds of groovy.\n"); sb.append("The following transformations will be ignored:"); for (Map.Entry<String, URL> entry : transformNames.entrySet()) { sb.append('\t'); sb.append(entry.getKey()); sb.append('\n'); } compilationUnit .getErrorCollector() .addWarning( new WarningMessage(WarningMessage.POSSIBLE_ERRORS, sb.toString(), null, null)); return; } // record the transforms found in the first scan, so that in the 2nd scan, phase operations // can be added for only for new transforms that have come in if (isFirstScan) { for (Map.Entry<String, URL> entry : transformNames.entrySet()) { context.getGlobalTransformNames().add(entry.getKey()); } addPhaseOperationsForGlobalTransforms( context.getCompilationUnit(), transformNames, isFirstScan); } else { Iterator<Map.Entry<String, URL>> it = transformNames.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, URL> entry = it.next(); if (!context.getGlobalTransformNames().add(entry.getKey())) { // phase operations for this transform class have already been added before, so remove // from current scan cycle it.remove(); } } addPhaseOperationsForGlobalTransforms( context.getCompilationUnit(), transformNames, isFirstScan); } }
private Expression findStaticMethodImportFromModule(Expression method, Expression args) { ModuleNode module = currentClass.getModule(); if (module == null || !(method instanceof ConstantExpression)) return null; Map<String, ImportNode> importNodes = module.getStaticImports(); ConstantExpression ce = (ConstantExpression) method; Expression expression; Object value = ce.getValue(); // skip non-Strings, e.g. Integer if (!(value instanceof String)) return null; final String name = (String) value; // look for one of these: // import static SomeClass.method [as otherName] // when resolving methodCall() or getProp() or setProp() if (importNodes.containsKey(name)) { ImportNode importNode = importNodes.get(name); expression = findStaticMethod(importNode.getType(), importNode.getFieldName(), args); if (expression != null) return expression; expression = findStaticPropertyAccessorGivenArgs( importNode.getType(), getPropNameForAccessor(importNode.getFieldName()), args); if (expression != null) { return new StaticMethodCallExpression( importNode.getType(), importNode.getFieldName(), args); } } // look for one of these: // import static SomeClass.someProp [as otherName] // when resolving getProp() or setProp() if (validPropName(name)) { String propName = getPropNameForAccessor(name); if (importNodes.containsKey(propName)) { ImportNode importNode = importNodes.get(propName); expression = findStaticMethod( importNode.getType(), prefix(name) + capitalize(importNode.getFieldName()), args); if (expression != null) return expression; expression = findStaticPropertyAccessorGivenArgs( importNode.getType(), importNode.getFieldName(), args); if (expression != null) { return new StaticMethodCallExpression( importNode.getType(), prefix(name) + capitalize(importNode.getFieldName()), args); } } } Map<String, ImportNode> starImports = module.getStaticStarImports(); ClassNode starImportType; if (currentClass.isEnum() && starImports.containsKey(currentClass.getName())) { ImportNode importNode = starImports.get(currentClass.getName()); starImportType = importNode == null ? null : importNode.getType(); expression = findStaticMethod(starImportType, name, args); if (expression != null) return expression; } else { for (ImportNode importNode : starImports.values()) { starImportType = importNode == null ? null : importNode.getType(); expression = findStaticMethod(starImportType, name, args); if (expression != null) return expression; expression = findStaticPropertyAccessorGivenArgs(starImportType, getPropNameForAccessor(name), args); if (expression != null) { return new StaticMethodCallExpression(starImportType, name, args); } } } return null; }
public boolean isPublicClass(String className) { return summariesByPublicClassName.containsKey(className); }
private Expression findStaticFieldOrPropAccessorImportFromModule(String name) { ModuleNode module = currentClass.getModule(); if (module == null) return null; Map<String, ImportNode> importNodes = module.getStaticImports(); Expression expression = null; String accessorName = getAccessorName(name); // look for one of these: // import static MyClass.setProp [as setOtherProp] // import static MyClass.getProp [as getOtherProp] // when resolving prop reference if (importNodes.containsKey(accessorName)) { ImportNode importNode = importNodes.get(accessorName); expression = findStaticPropertyAccessorByFullName(importNode.getType(), importNode.getFieldName()); if (expression != null) return expression; expression = findStaticPropertyAccessor( importNode.getType(), getPropNameForAccessor(importNode.getFieldName())); if (expression != null) return expression; } if (accessorName.startsWith("get")) { accessorName = "is" + accessorName.substring(3); if (importNodes.containsKey(accessorName)) { ImportNode importNode = importNodes.get(accessorName); expression = findStaticPropertyAccessorByFullName(importNode.getType(), importNode.getFieldName()); if (expression != null) return expression; expression = findStaticPropertyAccessor( importNode.getType(), getPropNameForAccessor(importNode.getFieldName())); if (expression != null) return expression; } } // look for one of these: // import static MyClass.prop [as otherProp] // when resolving prop or field reference if (importNodes.containsKey(name)) { ImportNode importNode = importNodes.get(name); // GRECLIPSE add try { if (!isReconcile) { // GRECLIPSE end expression = findStaticPropertyAccessor(importNode.getType(), importNode.getFieldName()); if (expression != null) return expression; // GRECLIPSE add } // end expression = findStaticField(importNode.getType(), importNode.getFieldName()); if (expression != null) return expression; // GRECLIPSE add } finally { // store the identifier to facilitate organizing static imports if (expression != null) expression.setNodeMetaData("static.import.alias", name); } // GRECLIPSE end } // look for one of these: // import static MyClass.* // when resolving prop or field reference for (ImportNode importNode : module.getStaticStarImports().values()) { ClassNode node = importNode.getType(); expression = findStaticPropertyAccessor(node, name); if (expression != null) return expression; expression = findStaticField(node, name); if (expression != null) return expression; } return null; }