Java Code Examples for org.codehaus.groovy.ast.ClassNode#isEnum()
The following examples show how to use
org.codehaus.groovy.ast.ClassNode#isEnum() .
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: GroovyLanguageServerUtils.java From groovy-language-server with Apache License 2.0 | 6 votes |
public static CompletionItemKind astNodeToCompletionItemKind(ASTNode node) { if (node instanceof ClassNode) { ClassNode classNode = (ClassNode) node; if (classNode.isInterface()) { return CompletionItemKind.Interface; } else if (classNode.isEnum()) { return CompletionItemKind.Enum; } return CompletionItemKind.Class; } else if (node instanceof MethodNode) { return CompletionItemKind.Method; } else if (node instanceof Variable) { if (node instanceof FieldNode || node instanceof PropertyNode) { return CompletionItemKind.Field; } return CompletionItemKind.Variable; } return CompletionItemKind.Property; }
Example 2
Source File: GroovyLanguageServerUtils.java From groovy-language-server with Apache License 2.0 | 6 votes |
public static SymbolKind astNodeToSymbolKind(ASTNode node) { if (node instanceof ClassNode) { ClassNode classNode = (ClassNode) node; if (classNode.isInterface()) { return SymbolKind.Interface; } else if (classNode.isEnum()) { return SymbolKind.Enum; } return SymbolKind.Class; } else if (node instanceof MethodNode) { return SymbolKind.Method; } else if (node instanceof Variable) { if (node instanceof FieldNode || node instanceof PropertyNode) { return SymbolKind.Field; } return SymbolKind.Variable; } return SymbolKind.Property; }
Example 3
Source File: ImmutablePropertyUtils.java From groovy with Apache License 2.0 | 6 votes |
public static boolean isKnownImmutableType(ClassNode fieldType, List<String> knownImmutableClasses) { if (builtinOrDeemedType(fieldType, knownImmutableClasses)) return true; if (!fieldType.isResolved()) return false; if ("java.util.Optional".equals(fieldType.getName()) && fieldType.getGenericsTypes() != null && fieldType.getGenericsTypes().length == 1) { GenericsType optionalType = fieldType.getGenericsTypes()[0]; if (optionalType.isResolved() && !optionalType.isPlaceholder() && !optionalType.isWildcard()) { ClassNode valueType = optionalType.getType(); if (builtinOrDeemedType(valueType, knownImmutableClasses)) return true; if (valueType.isEnum()) return true; } } return fieldType.isEnum() || ClassHelper.isPrimitiveType(fieldType) || hasImmutableAnnotation(fieldType); }
Example 4
Source File: EnumTypeCheckingExtension.java From groovy with Apache License 2.0 | 6 votes |
@Override public boolean handleUnresolvedVariableExpression(VariableExpression vexp) { SwitchStatement switchStatement = this.typeCheckingVisitor.typeCheckingContext.getEnclosingSwitchStatement(); if (null == switchStatement) return false; ClassNode type = switchStatement.getExpression().getNodeMetaData(StaticTypesMarker.TYPE); if (null == type) return false; if (type.isEnum()) { FieldNode fieldNode = type.redirect().getField(vexp.getName()); if (null != fieldNode) { int modifiers = fieldNode.getModifiers(); if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers) && type.equals(fieldNode.getType())) { vexp.putNodeMetaData(SWITCH_CONDITION_EXPRESSION_TYPE, type); return true; } } } return false; }
Example 5
Source File: AnnotationVisitor.java From groovy with Apache License 2.0 | 6 votes |
private boolean validateEnumConstant(Expression exp) { if (exp instanceof PropertyExpression) { PropertyExpression pe = (PropertyExpression) exp; String name = pe.getPropertyAsString(); if (pe.getObjectExpression() instanceof ClassExpression && name != null) { ClassExpression ce = (ClassExpression) pe.getObjectExpression(); ClassNode type = ce.getType(); if (type.isEnum()) { boolean ok = false; try { FieldNode enumField = type.getDeclaredField(name); ok = enumField != null && enumField.getType().equals(type); } catch(Exception ex) { // ignore } if(!ok) { addError("No enum const " + type.getName() + "." + name, pe); return false; } } } } return true; }
Example 6
Source File: StaticTypesCallSiteWriter.java From groovy with Apache License 2.0 | 6 votes |
private boolean getField(final PropertyExpression expression, final Expression receiver, ClassNode receiverType, final String name) { boolean safe = expression.isSafe(); boolean implicitThis = expression.isImplicitThis(); if (makeGetField(receiver, receiverType, name, safe, implicitThis)) return true; if (receiver instanceof ClassExpression) { if (makeGetField(receiver, receiver.getType(), name, safe, implicitThis)) return true; if (makeGetPrivateFieldWithBridgeMethod(receiver, receiver.getType(), name, safe, implicitThis)) return true; } if (makeGetPrivateFieldWithBridgeMethod(receiver, receiverType, name, safe, implicitThis)) return true; boolean isClassReceiver = false; if (isClassClassNodeWrappingConcreteType(receiverType)) { isClassReceiver = true; receiverType = receiverType.getGenericsTypes()[0].getType(); } if (isClassReceiver && makeGetField(receiver, CLASS_Type, name, safe, false)) return true; if (receiverType.isEnum()) { controller.getMethodVisitor().visitFieldInsn(GETSTATIC, BytecodeHelper.getClassInternalName(receiverType), name, BytecodeHelper.getTypeDescription(receiverType)); controller.getOperandStack().push(receiverType); return true; } return false; }
Example 7
Source File: InnerClassVisitor.java From groovy with Apache License 2.0 | 6 votes |
@Override public void visitClass(ClassNode node) { classNode = node; InnerClassNode innerClass = null; if (!node.isEnum() && !node.isInterface() && node instanceof InnerClassNode) { innerClass = (InnerClassNode) node; if (innerClass.getVariableScope() == null && (innerClass.getModifiers() & ACC_STATIC) == 0) { innerClass.addField("this$0", ACC_FINAL | ACC_SYNTHETIC, node.getOuterClass().getPlainNodeReference(), null); } } super.visitClass(node); if (node.isEnum() || node.isInterface()) return; if (innerClass == null) return; if (node.getSuperClass().isInterface() || Traits.isAnnotatedWithTrait(node.getSuperClass())) { node.addInterface(node.getUnresolvedSuperClass()); node.setUnresolvedSuperClass(ClassHelper.OBJECT_TYPE); } }
Example 8
Source File: InnerClassCompletionVisitor.java From groovy with Apache License 2.0 | 6 votes |
private boolean shouldImplicitlyPassThisPara(ConstructorCallExpression cce) { boolean pass = false; ClassNode superCN = classNode.getSuperClass(); if (cce.isThisCall()) { pass = true; } else if (cce.isSuperCall()) { // if the super class is another non-static inner class in the same outer class hierarchy, implicit this // needs to be passed if (!superCN.isEnum() && !superCN.isInterface() && superCN instanceof InnerClassNode) { InnerClassNode superInnerCN = (InnerClassNode) superCN; if (!isStatic(superInnerCN) && classNode.getOuterClass().isDerivedFrom(superCN.getOuterClass())) { pass = true; } } } return pass; }
Example 9
Source File: InnerClassCompletionVisitor.java From groovy with Apache License 2.0 | 6 votes |
@Override public void visitClass(ClassNode node) { classNode = node; thisField = null; InnerClassNode innerClass = null; if (!node.isEnum() && !node.isInterface() && node instanceof InnerClassNode) { innerClass = (InnerClassNode) node; thisField = innerClass.getField("this$0"); if (innerClass.getVariableScope() == null && innerClass.getDeclaredConstructors().isEmpty()) { // add empty default constructor addGeneratedConstructor(innerClass, ACC_PUBLIC, Parameter.EMPTY_ARRAY, null, null); } } if (node.isEnum() || node.isInterface()) return; // use Iterator.hasNext() to check for available inner classes if (node.getInnerClasses().hasNext()) addDispatcherMethods(node); if (innerClass == null) return; super.visitClass(node); addMopMethods(innerClass); }
Example 10
Source File: ExpressionUtils.java From groovy with Apache License 2.0 | 5 votes |
/** * The attribute values of annotations must be primitive, String or Enum constants. * In various places, such constants can be seen during type resolution but won't be * readily accessible in later phases, e.g. they might be embedded into constructor code. * This method transforms constants that would appear in annotations early so they aren't lost. * Subsequent processing determines whether they are valid, this method simply retains * the constant value as a constant expression. * * @param exp the original expression * @return the converted expression */ public static Expression transformInlineConstants(final Expression exp) { if (exp instanceof PropertyExpression) { PropertyExpression pe = (PropertyExpression) exp; if (pe.getObjectExpression() instanceof ClassExpression) { ClassExpression ce = (ClassExpression) pe.getObjectExpression(); ClassNode type = ce.getType(); FieldNode field = ClassNodeUtils.getField(type, pe.getPropertyAsString()); if (type.isEnum() && field != null && field.isEnum()) return exp; Expression constant = findConstant(field); if (constant != null) return constant; } } else if (exp instanceof BinaryExpression) { BinaryExpression be = (BinaryExpression) exp; be.setLeftExpression(transformInlineConstants(be.getLeftExpression())); be.setRightExpression(transformInlineConstants(be.getRightExpression())); return be; } else if (exp instanceof ListExpression) { ListExpression origList = (ListExpression) exp; ListExpression newList = new ListExpression(); boolean changed = false; for (Expression e : origList.getExpressions()) { Expression transformed = transformInlineConstants(e); newList.addExpression(transformed); if (transformed != e) changed = true; } if (changed) { newList.setSourcePosition(origList); return newList; } return origList; } return exp; }
Example 11
Source File: GroovyNodeToStringUtils.java From groovy-language-server with Apache License 2.0 | 5 votes |
public static String classToString(ClassNode classNode, ASTNodeVisitor ast) { StringBuilder builder = new StringBuilder(); String packageName = classNode.getPackageName(); if (packageName != null && packageName.length() > 0) { builder.append("package "); builder.append(packageName); builder.append("\n"); } if (!classNode.isSyntheticPublic()) { builder.append("public "); } if (classNode.isAbstract()) { builder.append("abstract "); } if (classNode.isInterface()) { builder.append("interface "); } else if (classNode.isEnum()) { builder.append("enum "); } else { builder.append("class "); } builder.append(classNode.getNameWithoutPackage()); ClassNode superClass = null; try { superClass = classNode.getSuperClass(); } catch (NoClassDefFoundError e) { //this is fine, we'll just treat it as null } if (superClass != null && !superClass.getName().equals(JAVA_OBJECT)) { builder.append(" extends "); builder.append(superClass.getNameWithoutPackage()); } return builder.toString(); }
Example 12
Source File: InnerClassVisitorHelper.java From groovy with Apache License 2.0 | 5 votes |
protected static boolean shouldHandleImplicitThisForInnerClass(ClassNode cn) { if (cn.isEnum() || cn.isInterface()) return false; if ((cn.getModifiers() & Opcodes.ACC_STATIC) != 0) return false; if (!(cn instanceof InnerClassNode)) return false; InnerClassNode innerClass = (InnerClassNode) cn; // scope != null means aic, we don't handle that here if (innerClass.getVariableScope() != null) return false; // static inner classes don't need this$0 return (innerClass.getModifiers() & Opcodes.ACC_STATIC) == 0; }
Example 13
Source File: JavaStubGenerator.java From groovy with Apache License 2.0 | 4 votes |
private void printMethod(PrintWriter out, ClassNode clazz, MethodNode methodNode) { if (methodNode.getName().equals("<clinit>")) return; if (methodNode.isPrivate() || !Utilities.isJavaIdentifier(methodNode.getName())) return; if (methodNode.isSynthetic() && methodNode.getName().equals("$getStaticMetaClass")) return; printAnnotations(out, methodNode); if (!isInterfaceOrTrait(clazz)) { int modifiers = methodNode.getModifiers(); if (isDefaultTraitImpl(methodNode)) { modifiers ^= Opcodes.ACC_ABSTRACT; } printModifiers(out, modifiers & ~(clazz.isEnum() ? Opcodes.ACC_ABSTRACT : 0)); } printGenericsBounds(out, methodNode.getGenericsTypes()); out.print(" "); printType(out, methodNode.getReturnType()); out.print(" "); out.print(methodNode.getName()); printParams(out, methodNode); ClassNode[] exceptions = methodNode.getExceptions(); printExceptions(out, exceptions); if (Traits.isTrait(clazz)) { out.println(";"); } else if (isAbstract(methodNode) && !clazz.isEnum()) { if (clazz.isAnnotationDefinition() && methodNode.hasAnnotationDefault()) { Statement fs = methodNode.getFirstStatement(); if (fs instanceof ExpressionStatement) { ExpressionStatement es = (ExpressionStatement) fs; Expression re = es.getExpression(); out.print(" default "); ClassNode rt = methodNode.getReturnType(); boolean classReturn = ClassHelper.CLASS_Type.equals(rt) || (rt.isArray() && ClassHelper.CLASS_Type.equals(rt.getComponentType())); if (re instanceof ListExpression) { out.print("{ "); ListExpression le = (ListExpression) re; boolean first = true; for (Expression expression : le.getExpressions()) { if (first) first = false; else out.print(", "); printValue(out, expression, classReturn); } out.print(" }"); } else { printValue(out, re, classReturn); } } } out.println(";"); } else { out.print(" { "); ClassNode retType = methodNode.getReturnType(); printReturn(out, retType); out.println("}"); } }
Example 14
Source File: EnumVisitor.java From groovy with Apache License 2.0 | 4 votes |
@Override public void visitClass(final ClassNode node) { if (!node.isEnum()) return; completeEnum(node); }
Example 15
Source File: VariableScopeVisitor.java From groovy with Apache License 2.0 | 4 votes |
private static boolean isAnonymous(final ClassNode node) { return (node instanceof InnerClassNode && ((InnerClassNode) node).isAnonymous() && !node.isEnum()); }
Example 16
Source File: Verifier.java From groovy with Apache License 2.0 | 4 votes |
protected void addInitialization(ClassNode node, ConstructorNode constructorNode) { Statement firstStatement = constructorNode.getFirstStatement(); // if some transformation decided to generate constructor then it probably knows who it does if (firstStatement instanceof BytecodeSequence) return; ConstructorCallExpression first = getFirstIfSpecialConstructorCall(firstStatement); // in case of this(...) let the other constructor do the init if (first != null && (first.isThisCall())) return; List<Statement> statements = new ArrayList<Statement>(); List<Statement> staticStatements = new ArrayList<Statement>(); final boolean isEnum = node.isEnum(); List<Statement> initStmtsAfterEnumValuesInit = new ArrayList<Statement>(); Set<String> explicitStaticPropsInEnum = new HashSet<String>(); if (isEnum) { for (PropertyNode propNode : node.getProperties()) { if (!propNode.isSynthetic() && propNode.getField().isStatic()) { explicitStaticPropsInEnum.add(propNode.getField().getName()); } } for (FieldNode fieldNode : node.getFields()) { if (!fieldNode.isSynthetic() && fieldNode.isStatic() && fieldNode.getType() != node) { explicitStaticPropsInEnum.add(fieldNode.getName()); } } } if (!Traits.isTrait(node)) { for (FieldNode fn : node.getFields()) { addFieldInitialization(statements, staticStatements, fn, isEnum, initStmtsAfterEnumValuesInit, explicitStaticPropsInEnum); } } statements.addAll(node.getObjectInitializerStatements()); BlockStatement block = getCodeAsBlock(constructorNode); List<Statement> otherStatements = block.getStatements(); if (!otherStatements.isEmpty()) { if (first != null) { // it is super(..) since this(..) is already covered otherStatements.remove(0); statements.add(0, firstStatement); } Statement stmtThis$0 = getImplicitThis$0StmtIfInnerClass(otherStatements); if (stmtThis$0 != null) { // since there can be field init statements that depend on method/property dispatching // that uses this$0, it needs to bubble up before the super call itself (GROOVY-4471) statements.add(0, stmtThis$0); } statements.addAll(otherStatements); } BlockStatement newBlock = new BlockStatement(statements, block.getVariableScope()); newBlock.setSourcePosition(block); constructorNode.setCode(newBlock); if (!staticStatements.isEmpty()) { if (isEnum) { /* * GROOVY-3161: initialize statements for explicitly declared static fields * inside an enum should come after enum values are initialized */ staticStatements.removeAll(initStmtsAfterEnumValuesInit); node.addStaticInitializerStatements(staticStatements, true); if (!initStmtsAfterEnumValuesInit.isEmpty()) { node.positionStmtsAfterEnumInitStmts(initStmtsAfterEnumValuesInit); } } else { node.addStaticInitializerStatements(staticStatements, true); } } }
Example 17
Source File: VerifierCodeVisitor.java From groovy with Apache License 2.0 | 4 votes |
public void visitConstructorCallExpression(ConstructorCallExpression call) { ClassNode callType = call.getType(); if (callType.isEnum() && !callType.equals(classNode)) { throw new RuntimeParserException("Enum constructor calls are only allowed inside the enum class", call); } }
Example 18
Source File: EnumCompletionVisitor.java From groovy with Apache License 2.0 | 4 votes |
public void visitClass(ClassNode node) { if (!node.isEnum()) return; completeEnum(node); }