Java Code Examples for org.codehaus.groovy.ast.ClassNode#setRedirect()
The following examples show how to use
org.codehaus.groovy.ast.ClassNode#setRedirect() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Java8.java From groovy with Apache License 2.0 | 6 votes |
private ClassNode configureWildcardType(final WildcardType wildcardType) { ClassNode base = ClassHelper.makeWithoutCaching("?"); base.setRedirect(ClassHelper.OBJECT_TYPE); ClassNode[] lowers = configureTypes(wildcardType.getLowerBounds()); ClassNode[] uppers = configureTypes(wildcardType.getUpperBounds()); // beware of [Object] upper bounds; often it's <?> or <? super T> if (lowers != null || wildcardType.getTypeName().equals("?")) { uppers = null; } GenericsType gt = new GenericsType(base, uppers, lowers != null ? lowers[0] : null); gt.setWildcard(true); ClassNode wt = ClassHelper.makeWithoutCaching(Object.class, false); wt.setGenericsTypes(new GenericsType[]{gt}); return wt; }
Example 2
Source File: ResolveVisitor.java From groovy with Apache License 2.0 | 6 votes |
protected boolean resolveFromDefaultImports(final ClassNode type, final String[] packagePrefixes) { String typeName = type.getName(); for (String packagePrefix : packagePrefixes) { // We limit the inner class lookups here by using ConstructedClassWithPackage. // This way only the name will change, the packagePrefix will // not be included in the lookup. The case where the // packagePrefix is really a class is handled elsewhere. // WARNING: This code does not expect a class that has a static // inner class in DEFAULT_IMPORTS ConstructedClassWithPackage tmp = new ConstructedClassWithPackage(packagePrefix, typeName); if (resolve(tmp, false, false, false)) { type.setRedirect(tmp.redirect()); if (DEFAULT_IMPORTS == packagePrefixes) { // Only the non-cached type and packages should be cached Set<String> packagePrefixSet = DEFAULT_IMPORT_CLASS_AND_PACKAGES_CACHE.getAndPut(typeName, key -> new HashSet<>(2)); packagePrefixSet.add(packagePrefix); } return true; } } return false; }
Example 3
Source File: StaticTypeCheckingSupport.java From groovy with Apache License 2.0 | 6 votes |
static ClassNode applyGenericsContext(final Map<GenericsTypeName, GenericsType> spec, final ClassNode bound) { if (bound == null) return null; if (bound.isArray()) { return applyGenericsContext(spec, bound.getComponentType()).makeArray(); } if (!bound.isUsingGenerics()) return bound; ClassNode newBound = bound.getPlainNodeReference(); newBound.setGenericsTypes(applyGenericsContext(spec, bound.getGenericsTypes())); if (bound.isGenericsPlaceHolder()) { GenericsType[] gt = newBound.getGenericsTypes(); boolean hasBounds = hasNonTrivialBounds(gt[0]); if (hasBounds || !gt[0].isPlaceholder()) return getCombinedBoundType(gt[0]); String placeHolderName = newBound.getGenericsTypes()[0].getName(); if (!placeHolderName.equals(newBound.getUnresolvedName())) { // we should produce a clean placeholder ClassNode here ClassNode clean = make(placeHolderName); clean.setGenericsTypes(newBound.getGenericsTypes()); clean.setRedirect(newBound); newBound = clean; } newBound.setGenericsPlaceHolder(true); } return newBound; }
Example 4
Source File: SuperCallTraitTransformer.java From groovy with Apache License 2.0 | 6 votes |
private ClassNode getHelper(final ClassNode traitReceiver) { if (helperClassNotCreatedYet(traitReceiver)) { // GROOVY-7909 A Helper class in same compilation unit may have not been created when referenced // Here create a symbol as a "placeholder" and it will be resolved later. ClassNode ret = new InnerClassNode( traitReceiver, Traits.helperClassName(traitReceiver), ACC_PUBLIC | ACC_STATIC | ACC_ABSTRACT | ACC_SYNTHETIC, ClassHelper.OBJECT_TYPE, ClassNode.EMPTY_ARRAY, null ).getPlainNodeReference(); ret.setRedirect(null); traitReceiver.redirect().setNodeMetaData(UNRESOLVED_HELPER_CLASS, ret); return ret; } else { TraitHelpersTuple helpers = Traits.findHelpers(traitReceiver); return helpers.getHelper(); } }
Example 5
Source File: TraitASTTransformation.java From groovy with Apache License 2.0 | 5 votes |
private void resolveHelperClassIfNecessary(final ClassNode helperClassNode) { if (helperClassNode == null) { return; } for (ClassNode cNode : unit.getAST().getClasses()) { ClassNode unresolvedHelperNode = cNode.getNodeMetaData(UNRESOLVED_HELPER_CLASS); if (unresolvedHelperNode != null && unresolvedHelperNode.getName().equals(helperClassNode.getName())) { unresolvedHelperNode.setRedirect(helperClassNode); } } }
Example 6
Source File: Java8.java From groovy with Apache License 2.0 | 5 votes |
private ClassNode makeClassNode(CompileUnit cu, Type t, Class<?> c) { ClassNode back = null; if (cu != null) back = cu.getClass(c.getName()); if (back == null) back = ClassHelper.make(c); if (!(t instanceof Class)) { ClassNode front = configureType(t); front.setRedirect(back); return front; } return back.getPlainNodeReference(); }
Example 7
Source File: Java8.java From groovy with Apache License 2.0 | 5 votes |
public static ClassNode configureTypeVariableReference(String name) { ClassNode cn = ClassHelper.makeWithoutCaching(name); cn.setGenericsPlaceHolder(true); ClassNode cn2 = ClassHelper.makeWithoutCaching(name); cn2.setGenericsPlaceHolder(true); GenericsType[] gts = new GenericsType[]{new GenericsType(cn2)}; cn.setGenericsTypes(gts); cn.setRedirect(ClassHelper.OBJECT_TYPE); return cn; }
Example 8
Source File: Java8.java From groovy with Apache License 2.0 | 5 votes |
public static GenericsType configureTypeVariableDefinition(ClassNode base, ClassNode[] cBounds) { ClassNode redirect = base.redirect(); base.setRedirect(null); GenericsType gt; if (cBounds == null || cBounds.length == 0) { gt = new GenericsType(base); } else { gt = new GenericsType(base, cBounds, null); gt.setName(base.getName()); gt.setPlaceholder(true); } base.setRedirect(redirect); return gt; }
Example 9
Source File: StatementMetaTypeChooser.java From groovy with Apache License 2.0 | 5 votes |
@Override public ClassNode resolveType(final Expression exp, final ClassNode current) { ClassNode type = null; if (exp instanceof ClassExpression) { type = exp.getType(); ClassNode classType = ClassHelper.makeWithoutCaching("java.lang.Class"); classType.setGenericsTypes(new GenericsType[] {new GenericsType(type)}); classType.setRedirect(ClassHelper.CLASS_Type); return classType; } OptimizingStatementWriter.StatementMeta meta = exp.getNodeMetaData(OptimizingStatementWriter.StatementMeta.class); if (meta != null) type = meta.type; if (type != null) return type; if (exp instanceof VariableExpression) { VariableExpression ve = (VariableExpression) exp; if (ve.isClosureSharedVariable()) return ve.getType(); if (ve.isSuperExpression()) return current.getSuperClass(); type = ve.getOriginType(); } else if (exp instanceof Variable) { Variable v = (Variable) exp; type = v.getOriginType(); } else { type = exp.getType(); } return type.redirect(); }
Example 10
Source File: GenericsUtils.java From groovy with Apache License 2.0 | 5 votes |
public static Map<String, ClassNode> addMethodGenerics(MethodNode current, Map<String, ClassNode> oldSpec) { Map<String, ClassNode> newSpec = new HashMap<>(oldSpec); GenericsType[] gts = current.getGenericsTypes(); if (gts != null) { for (GenericsType gt : gts) { String name = gt.getName(); ClassNode type = gt.getType(); if (gt.isPlaceholder()) { ClassNode redirect; if (gt.getUpperBounds() != null) { redirect = gt.getUpperBounds()[0]; } else if (gt.getLowerBound() != null) { redirect = gt.getLowerBound(); } else { redirect = ClassHelper.OBJECT_TYPE; } if (redirect.isGenericsPlaceHolder()) { // "T extends U" or "T super U" type = redirect; } else { // "T" or "T extends Thing" or "T super Thing" type = ClassHelper.makeWithoutCaching(name); type.setGenericsPlaceHolder(true); type.setRedirect(redirect); } } newSpec.put(name, type); } } return newSpec; }
Example 11
Source File: GenericsUtils.java From groovy with Apache License 2.0 | 5 votes |
public static ClassNode nonGeneric(ClassNode type) { if (type.isUsingGenerics()) { final ClassNode nonGen = ClassHelper.makeWithoutCaching(type.getName()); nonGen.setRedirect(type); nonGen.setGenericsTypes(null); nonGen.setUsingGenerics(false); return nonGen; } if (type.isArray() && type.getComponentType().isUsingGenerics()) { return type.getComponentType().getPlainNodeReference().makeArray(); } return type; }
Example 12
Source File: MemberSignatureParser.java From groovy with Apache License 2.0 | 5 votes |
private static ClassNode applyErasure(ClassNode genericType, ClassNode erasure) { if (genericType.isArray() && erasure.isArray() && genericType.getComponentType().isGenericsPlaceHolder()) { genericType.setRedirect(erasure); genericType.getComponentType().setRedirect(erasure.getComponentType()); } else if (genericType.isGenericsPlaceHolder()) { genericType.setRedirect(erasure); } return genericType; }
Example 13
Source File: ResolveVisitor.java From groovy with Apache License 2.0 | 5 votes |
protected boolean resolve(final ClassNode type, final boolean testModuleImports, final boolean testDefaultImports, final boolean testStaticInnerClasses) { resolveGenericsTypes(type.getGenericsTypes()); if (type.isResolved() || type.isPrimaryClassNode()) return true; if (type.isArray()) { ClassNode element = type.getComponentType(); boolean resolved = resolve(element, testModuleImports, testDefaultImports, testStaticInnerClasses); if (resolved) { ClassNode cn = element.makeArray(); type.setRedirect(cn); } return resolved; } // test if vanilla name is current class name if (currentClass == type) return true; String typeName = type.getName(); GenericsType genericsType = genericParameterNames.get(new GenericsTypeName(typeName)); if (genericsType != null) { type.setRedirect(genericsType.getType()); type.setGenericsTypes(new GenericsType[]{genericsType}); type.setGenericsPlaceHolder(true); return true; } if (currentClass.getNameWithoutPackage().equals(typeName)) { type.setRedirect(currentClass); return true; } return (!type.hasPackageName() && resolveNestedClass(type)) || resolveFromModule(type, testModuleImports) || resolveFromCompileUnit(type) || (testDefaultImports && !type.hasPackageName() && resolveFromDefaultImports(type)) || resolveToOuter(type) || (testStaticInnerClasses && type.hasPackageName() && resolveFromStaticInnerClasses(type)); }
Example 14
Source File: ResolveVisitor.java From groovy with Apache License 2.0 | 5 votes |
private boolean resolveGenericsType(final GenericsType genericsType) { if (genericsType.isResolved()) return true; currentClass.setUsingGenerics(true); ClassNode type = genericsType.getType(); // save name before redirect GenericsTypeName name = new GenericsTypeName(type.getName()); ClassNode[] bounds = genericsType.getUpperBounds(); if (!genericParameterNames.containsKey(name)) { if (bounds != null) { for (ClassNode upperBound : bounds) { resolveOrFail(upperBound, genericsType); type.setRedirect(upperBound); resolveGenericsTypes(upperBound.getGenericsTypes()); } } else if (genericsType.isWildcard()) { type.setRedirect(ClassHelper.OBJECT_TYPE); } else { resolveOrFail(type, genericsType); } } else { GenericsType gt = genericParameterNames.get(name); type.setRedirect(gt.getType()); genericsType.setPlaceholder(true); } if (genericsType.getLowerBound() != null) { resolveOrFail(genericsType.getLowerBound(), genericsType); } if (resolveGenericsTypes(type.getGenericsTypes())) { genericsType.setResolved(genericsType.getType().isResolved()); } return genericsType.isResolved(); }
Example 15
Source File: ResolveVisitor.java From groovy with Apache License 2.0 | 5 votes |
protected boolean resolveToOuter(final ClassNode type) { String name = type.getName(); // We do not need to check instances of LowerCaseClass // to be a Class, because unless there was an import for // for this we do not lookup these cases. This was a decision // made on the mailing list. To ensure we will not visit this // method again we set a NO_CLASS for this name if (type instanceof LowerCaseClass) { classNodeResolver.cacheClass(name, ClassNodeResolver.NO_CLASS); return false; } if (currentClass.getModule().hasPackageName() && name.indexOf('.') == -1) return false; LookupResult lr = classNodeResolver.resolveName(name, compilationUnit); if (lr != null) { if (lr.isSourceUnit()) { SourceUnit su = lr.getSourceUnit(); currentClass.getCompileUnit().addClassNodeToCompile(type, su); } else { type.setRedirect(lr.getClassNode()); } return true; } return false; }
Example 16
Source File: ResolveVisitor.java From groovy with Apache License 2.0 | 5 votes |
private void ambiguousClass(final ClassNode type, final ClassNode iType, final String name) { if (type.getName().equals(iType.getName())) { addError("reference to " + name + " is ambiguous, both class " + type.getName() + " and " + iType.getName() + " match", type); } else { type.setRedirect(iType); } }
Example 17
Source File: ResolveVisitor.java From groovy with Apache License 2.0 | 5 votes |
protected boolean resolveFromCompileUnit(final ClassNode type) { // look into the compile unit if there is a class with that name CompileUnit compileUnit = currentClass.getCompileUnit(); if (compileUnit == null) return false; ClassNode cuClass = compileUnit.getClass(type.getName()); if (cuClass != null) { if (type != cuClass) type.setRedirect(cuClass); return true; } return false; }
Example 18
Source File: ResolveVisitor.java From groovy with Apache License 2.0 | 5 votes |
protected boolean resolveFromDefaultImports(final ClassNode type) { // we do not resolve a vanilla name starting with a lower case letter // try to resolve against a default import, because we know that the // default packages do not contain classes like these if (!(type instanceof LowerCaseClass)) { String typeName = type.getName(); Set<String> packagePrefixSet = DEFAULT_IMPORT_CLASS_AND_PACKAGES_CACHE.get(typeName); if (packagePrefixSet != null) { // if the type name was resolved before, we can try the successfully resolved packages first, which are much less and very likely successful to resolve. // As a result, we can avoid trying other default import packages and further resolving, which can improve the resolving performance to some extent. if (resolveFromDefaultImports(type, packagePrefixSet.toArray(EMPTY_STRING_ARRAY))) { return true; } } if (resolveFromDefaultImports(type, DEFAULT_IMPORTS)) { return true; } if (BIGINTEGER_STR.equals(typeName)) { type.setRedirect(ClassHelper.BigInteger_TYPE); return true; } if (BIGDECIMAL_STR.equals(typeName)) { type.setRedirect(ClassHelper.BigDecimal_TYPE); return true; } } return false; }
Example 19
Source File: ResolveVisitor.java From groovy with Apache License 2.0 | 5 votes |
private boolean setRedirect(final ClassNode type, final ClassNode classToCheck) { String typeName = type.getName(); Predicate<ClassNode> resolver = (ClassNode maybeOuter) -> { if (!typeName.equals(maybeOuter.getName())) { ClassNode maybeNested = new ConstructedNestedClass(maybeOuter, typeName); if (resolveFromCompileUnit(maybeNested) || resolveToOuter(maybeNested)) { type.setRedirect(maybeNested); return true; } } return false; }; if (resolver.test(classToCheck)) { if (currentClass != classToCheck && !currentClass.getOuterClasses().contains(classToCheck) && !isVisibleNestedClass(type.redirect(), currentClass)) { type.setRedirect(null); } else { return true; } } for (ClassNode face : classToCheck.getAllInterfaces()) { if (resolver.test(face)) { return true; } } return false; }
Example 20
Source File: InitializerStrategy.java From groovy with Apache License 2.0 | 4 votes |
private static GenericsType makePlaceholder(int i) { ClassNode type = ClassHelper.makeWithoutCaching("T" + i); type.setRedirect(OBJECT_TYPE); type.setGenericsPlaceHolder(true); return new GenericsType(type); }