List of usage examples for org.objectweb.asm Opcodes ACC_PRIVATE
int ACC_PRIVATE
To view the source code for org.objectweb.asm Opcodes ACC_PRIVATE.
Click Source Link
From source file:org.codehaus.groovy.eclipse.refactoring.test.extractMethod.ExtractMethodTestCase.java
License:Apache License
@Override public void simulateUserInput() { // set refactoring parameters int modifier = 0; String newMethodName = ""; try {// w ww .j av a 2s . com newMethodName = properties.get("newMethodName"); String mod = properties.get("modifier"); if (mod.equals("private")) modifier = Opcodes.ACC_PRIVATE; if (mod.equals("def")) modifier = Opcodes.ACC_PUBLIC; if (mod.equals("protected")) modifier = Opcodes.ACC_PROTECTED; } catch (Exception e) { e.printStackTrace(); fail("Initialisation of testproperties failed! " + e.getMessage()); } refactoring.setModifier(modifier); refactoring.setNewMethodname(newMethodName); setMoveParameter(refactoring); setRenameParameter(refactoring); }
From source file:org.codehaus.groovy.eclipse.refactoring.ui.extract.ExtractMethodPageContent.java
License:Apache License
private void setModifier(MouseEvent e) { if (e.getSource() instanceof Button) { Button selectedButton = (Button) e.getSource(); if (selectedButton.getText().equals(MODIFIER_PRIVATE)) extractMethodRefactoring.setModifier(Opcodes.ACC_PRIVATE); else if (selectedButton.getText().equals(MODIFIER_DEF)) extractMethodRefactoring.setModifier(Opcodes.ACC_PUBLIC); else if (selectedButton.getText().equals(MODIFIER_PROTECTED)) extractMethodRefactoring.setModifier(Opcodes.ACC_PROTECTED); else if (selectedButton.getText().equals(MODIFIER_NONE)) extractMethodRefactoring.setModifier(0); }/* w w w . j a va 2s .co m*/ }
From source file:org.codehaus.groovy.parser.antlr4.ASTBuilder.java
License:Apache License
public AnnotatedNode parseMember(ClassNode classNode, GroovyLangParser.FieldDeclarationContext ctx) { //noinspection GroovyAssignabilityCheck final Iterator<Object> iterator = parseModifiers(ctx.memberModifier()).iterator(); int modifiers = ((Integer) (iterator.hasNext() ? iterator.next() : null)); boolean hasVisibilityModifier = ((Boolean) (iterator.hasNext() ? iterator.next() : null)); modifiers |= classNode.isInterface() ? Opcodes.ACC_STATIC | Opcodes.ACC_FINAL : 0; AnnotatedNode node = null;/*from ww w. j a va 2s . c o m*/ List<? extends GroovyLangParser.SingleDeclarationContext> variables = ctx.singleDeclaration(); for (GroovyLangParser.SingleDeclarationContext variableCtx : variables) { GroovyLangParser.ExpressionContext initExprContext = variableCtx.expression(); Expression initialierExpression = asBoolean(initExprContext) ? parseExpression(initExprContext) : null; ClassNode typeDeclaration = asBoolean(ctx.genericClassNameExpression()) ? parseExpression(ctx.genericClassNameExpression()) : ClassHelper.OBJECT_TYPE; Object defaultValue = findDefaultValueByType(typeDeclaration); Expression initialValue = classNode.isInterface() && (null == initialierExpression) ? (null == defaultValue ? null : new ConstantExpression(defaultValue)) : initialierExpression; org.codehaus.groovy.syntax.Token token; if (asBoolean(variableCtx.ASSIGN())) { token = createGroovyToken(variableCtx.ASSIGN().getSymbol(), Types.ASSIGN); } else { int line = variableCtx.start.getLine(); int col = -1; //ASSIGN TOKEN DOES NOT APPEAR, SO COL IS -1. IF NO ERROR OCCURS, THE ORIGINAL CODE CAN BE REMOVED IN THE FURTURE: variableCtx.getStart().getCharPositionInLine() + 1; // FIXME Why assignment token location is it's first occurrence. token = new org.codehaus.groovy.syntax.Token(Types.ASSIGN, "=", line, col); } if (classNode.isInterface() || hasVisibilityModifier) { modifiers |= classNode.isInterface() ? Opcodes.ACC_PUBLIC : 0; FieldNode field = classNode.addField(variableCtx.IDENTIFIER().getText(), modifiers, typeDeclaration, initialValue, token); attachAnnotations(field, ctx.annotationClause()); node = setupNodeLocation(field, variables.size() == 1 ? ctx : variableCtx); } else {// no visibility specified. Generate property node. Integer propertyModifier = modifiers | Opcodes.ACC_PUBLIC; PropertyNode propertyNode = classNode.addProperty(variableCtx.IDENTIFIER().getText(), propertyModifier, typeDeclaration, initialValue, null, null, token); propertyNode.getField().setModifiers(modifiers | Opcodes.ACC_PRIVATE); propertyNode.getField().setSynthetic(!classNode.isInterface()); node = setupNodeLocation(propertyNode.getField(), variables.size() == 1 ? ctx : variableCtx); attachAnnotations(propertyNode.getField(), ctx.annotationClause()); setupNodeLocation(propertyNode, variables.size() == 1 ? ctx : variableCtx); } } return node; }
From source file:org.codehaus.groovy.parser.antlr4.ASTBuilder.java
License:Apache License
public int parseVisibilityModifiers(TerminalNode modifier) { assert modifier.getSymbol().getType() == GroovyLangLexer.VISIBILITY_MODIFIER; if (DefaultGroovyMethods.isCase(PUBLIC, modifier.getSymbol().getText())) return Opcodes.ACC_PUBLIC; else if (DefaultGroovyMethods.isCase(PRIVATE, modifier.getSymbol().getText())) return Opcodes.ACC_PRIVATE; else if (DefaultGroovyMethods.isCase(PROTECTED, modifier.getSymbol().getText())) return Opcodes.ACC_PROTECTED; else/*from ww w . j a va 2s. c o m*/ throw new AssertionError(modifier.getSymbol().getText() + " is not a valid visibility modifier!"); }
From source file:org.codehaus.groovy.tools.javac.JavaStubGenerator.java
License:Apache License
public void generateClass(ClassNode classNode) throws FileNotFoundException { // Only attempt to render our self if our super-class is resolved, else wait for it if (requireSuperResolved && !classNode.getSuperClass().isResolved()) { return;//w w w. j a va 2 s. c om } // owner should take care for us if (classNode instanceof InnerClassNode) return; // don't generate stubs for private classes, as they are only visible in the same file if ((classNode.getModifiers() & Opcodes.ACC_PRIVATE) != 0) return; if (null == outputPath) { generateMemStub(classNode); } else { generateFileStub(classNode); } }
From source file:org.codehaus.groovy.tools.javac.JavaStubGenerator.java
License:Apache License
private void printField(PrintWriter out, FieldNode fieldNode, boolean isInterface) { if ((fieldNode.getModifiers() & Opcodes.ACC_PRIVATE) != 0) return;/* w w w .j a v a 2 s. c o m*/ printAnnotations(out, fieldNode); if (!isInterface) { printModifiers(out, fieldNode.getModifiers()); } ClassNode type = fieldNode.getType(); printType(out, type); out.print(" "); out.print(fieldNode.getName()); if (isInterface || (fieldNode.getModifiers() & Opcodes.ACC_FINAL) != 0) { out.print(" = "); Expression valueExpr = fieldNode.getInitialValueExpression(); if (valueExpr instanceof ConstantExpression) { valueExpr = Verifier.transformToPrimitiveConstantIfPossible((ConstantExpression) valueExpr); } if (valueExpr instanceof ConstantExpression && fieldNode.isStatic() && fieldNode.isFinal() && ClassHelper.isStaticConstantInitializerType(valueExpr.getType()) && valueExpr.getType().equals(fieldNode.getType())) { // GROOVY-5150 : Initialize value with a dummy constant so that Java cross compiles correctly if (ClassHelper.STRING_TYPE.equals(valueExpr.getType())) { out.print(formatString(valueExpr.getText())); } else if (ClassHelper.char_TYPE.equals(valueExpr.getType())) { out.print("'" + valueExpr.getText() + "'"); } else { ClassNode constantType = valueExpr.getType(); out.print('('); printType(out, type); out.print(") "); out.print(valueExpr.getText()); if (ClassHelper.Long_TYPE.equals(ClassHelper.getWrapper(constantType))) out.print('L'); } } else if (ClassHelper.isPrimitiveType(type)) { String val = type == ClassHelper.boolean_TYPE ? "false" : "0"; out.print("new " + ClassHelper.getWrapper(type) + "((" + type + ")" + val + ")"); } else { out.print("null"); } } out.println(";"); }
From source file:org.codehaus.groovy.tools.javac.JavaStubGenerator.java
License:Apache License
private static void printModifiers(PrintWriter out, int modifiers) { if ((modifiers & Opcodes.ACC_PUBLIC) != 0) out.print("public "); if ((modifiers & Opcodes.ACC_PROTECTED) != 0) out.print("protected "); if ((modifiers & Opcodes.ACC_PRIVATE) != 0) out.print("private "); if ((modifiers & Opcodes.ACC_STATIC) != 0) out.print("static "); if ((modifiers & Opcodes.ACC_SYNCHRONIZED) != 0) out.print("synchronized "); if ((modifiers & Opcodes.ACC_FINAL) != 0) out.print("final "); if ((modifiers & Opcodes.ACC_ABSTRACT) != 0) out.print("abstract "); }
From source file:org.codehaus.groovy.transform.LogASTTransformation.java
License:Apache License
private int lookupLogFieldModifiers(AnnotatedNode targetClass, AnnotationNode logAnnotation) { int modifiers = getVisibility(logAnnotation, targetClass, ClassNode.class, Opcodes.ACC_PRIVATE); return Opcodes.ACC_FINAL | Opcodes.ACC_TRANSIENT | Opcodes.ACC_STATIC | modifiers; }
From source file:org.codehaus.groovy.transform.trait.TraitComposer.java
License:Apache License
private static void applyTrait(final ClassNode trait, final ClassNode cNode, final TraitHelpersTuple helpers, SourceUnit unit) {//w w w . j av a2s . c om ClassNode helperClassNode = helpers.getHelper(); ClassNode fieldHelperClassNode = helpers.getFieldHelper(); ClassNode staticFieldHelperClassNode = helpers.getStaticFieldHelper(); Map<String, ClassNode> genericsSpec = GenericsUtils.createGenericsSpec(trait, GenericsUtils.createGenericsSpec(cNode)); for (MethodNode methodNode : helperClassNode.getAllDeclaredMethods()) { String name = methodNode.getName(); Parameter[] helperMethodParams = methodNode.getParameters(); int nParams = helperMethodParams.length; if (nParams > 0 && !methodNode.isAbstract() && ((methodNode.getModifiers() & Opcodes.ACC_STATIC) != 0) && (!name.contains("$") || (methodNode.getModifiers() & Opcodes.ACC_SYNTHETIC) == 0)) { ArgumentListExpression argList = new ArgumentListExpression(); argList.addExpression(new VariableExpression("this")); Parameter[] origParams = new Parameter[nParams - 1]; Parameter[] params = new Parameter[nParams - 1]; System.arraycopy(methodNode.getParameters(), 1, params, 0, params.length); MethodNode originalMethod = trait.getMethod(name, params); Map<String, ClassNode> methodGenericsSpec = Optional.ofNullable(originalMethod) .map(m -> GenericsUtils.addMethodGenerics(m, genericsSpec)).orElse(genericsSpec); for (int i = 1; i < nParams; i += 1) { Parameter parameter = helperMethodParams[i]; ClassNode originType = parameter.getOriginType(); ClassNode fixedType = correctToGenericsSpecRecurse(methodGenericsSpec, originType); Parameter newParam = new Parameter(fixedType, parameter.getName()); List<AnnotationNode> copied = new LinkedList<>(); List<AnnotationNode> notCopied = new LinkedList<>(); GeneralUtils.copyAnnotatedNodeAnnotations(parameter, copied, notCopied); newParam.addAnnotations(copied); params[i - 1] = newParam; origParams[i - 1] = parameter; argList.addExpression(new VariableExpression(newParam)); } createForwarderMethod(trait, cNode, methodNode, originalMethod, helperClassNode, methodGenericsSpec, helperMethodParams, origParams, params, argList, unit); } } MethodCallExpression staticInitCall = new MethodCallExpression(new ClassExpression(helperClassNode), Traits.STATIC_INIT_METHOD, new ArgumentListExpression(new ClassExpression(cNode))); MethodNode staticInitMethod = new MethodNode(Traits.STATIC_INIT_METHOD, Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC, ClassHelper.VOID_TYPE, new Parameter[] { new Parameter(ClassHelper.CLASS_Type, "clazz") }, ClassNode.EMPTY_ARRAY, EmptyStatement.INSTANCE); staticInitMethod.setDeclaringClass(helperClassNode); staticInitCall.setMethodTarget(staticInitMethod); cNode.addStaticInitializerStatements( Collections.<Statement>singletonList(new ExpressionStatement(staticInitCall)), false); if (fieldHelperClassNode != null && !cNode.declaresInterface(fieldHelperClassNode)) { // we should implement the field helper interface too cNode.addInterface(fieldHelperClassNode); // implementation of methods List<MethodNode> declaredMethods = new LinkedList<>(); for (MethodNode declaredMethod : fieldHelperClassNode.getAllDeclaredMethods()) { if (declaredMethod.getName().endsWith(Traits.DIRECT_GETTER_SUFFIX)) { declaredMethods.add(0, declaredMethod); } else { declaredMethods.add(declaredMethod); } } if (staticFieldHelperClassNode != null) { for (MethodNode declaredMethod : staticFieldHelperClassNode.getAllDeclaredMethods()) { if (declaredMethod.getName().endsWith(Traits.DIRECT_GETTER_SUFFIX)) { declaredMethods.add(0, declaredMethod); } else { declaredMethods.add(declaredMethod); } } } for (MethodNode methodNode : declaredMethods) { String fieldName = methodNode.getName(); if (fieldName.endsWith(Traits.DIRECT_GETTER_SUFFIX) || fieldName.endsWith(Traits.DIRECT_SETTER_SUFFIX)) { int suffixIdx = fieldName.lastIndexOf("$"); fieldName = fieldName.substring(0, suffixIdx); String operation = methodNode.getName().substring(suffixIdx + 1); boolean getter = "get".equals(operation); ClassNode returnType = correctToGenericsSpecRecurse(genericsSpec, methodNode.getReturnType()); int fieldMods = 0; int isStatic = 0; boolean publicField = true; FieldNode helperField = null; fieldMods = 0; isStatic = 0; // look first for field with encoded modifier information for (Integer mod : Traits.FIELD_PREFIXES) { helperField = fieldHelperClassNode.getField(String.format("$0x%04x", mod) + fieldName); if (helperField != null) { if ((mod & Opcodes.ACC_STATIC) != 0) isStatic = Opcodes.ACC_STATIC; fieldMods = fieldMods | mod; break; } } if (helperField == null) { // look for possible legacy fields (trait compiled pre 2.4.8) helperField = fieldHelperClassNode .getField(Traits.FIELD_PREFIX + Traits.PUBLIC_FIELD_PREFIX + fieldName); if (helperField == null) { publicField = false; helperField = fieldHelperClassNode .getField(Traits.FIELD_PREFIX + Traits.PRIVATE_FIELD_PREFIX + fieldName); } if (helperField == null) { publicField = true; // try to find a static one helperField = fieldHelperClassNode .getField(Traits.STATIC_FIELD_PREFIX + Traits.PUBLIC_FIELD_PREFIX + fieldName); if (helperField == null) { publicField = false; helperField = fieldHelperClassNode.getField( Traits.STATIC_FIELD_PREFIX + Traits.PRIVATE_FIELD_PREFIX + fieldName); } fieldMods = fieldMods | Opcodes.ACC_STATIC; isStatic = Opcodes.ACC_STATIC; } fieldMods = fieldMods | (publicField ? Opcodes.ACC_PUBLIC : Opcodes.ACC_PRIVATE); } if (getter) { // add field if (helperField != null) { List<AnnotationNode> copied = new LinkedList<>(); List<AnnotationNode> notCopied = new LinkedList<>(); GeneralUtils.copyAnnotatedNodeAnnotations(helperField, copied, notCopied); FieldNode fieldNode = cNode.addField(fieldName, fieldMods, returnType, null); fieldNode.addAnnotations(copied); // getInitialExpression above will be null if not in same source unit // so instead set within (static) initializer if (fieldNode.isFinal()) { String baseName = fieldNode.isStatic() ? Traits.STATIC_INIT_METHOD : Traits.INIT_METHOD; StaticMethodCallExpression mce = callX(helperClassNode, baseName + fieldNode.getName(), args(varX("this"))); if (helperClassNode.hasPossibleStaticMethod(mce.getMethod(), mce.getArguments())) { Statement stmt = stmt( assignX(varX(fieldNode.getName(), fieldNode.getType()), mce)); if (isStatic == 0) { cNode.addObjectInitializerStatements(stmt); } else { List<Statement> staticStatements = new ArrayList<Statement>(); staticStatements.add(stmt); cNode.addStaticInitializerStatements(staticStatements, true); } } } } } Parameter[] newParams; if (getter) { newParams = Parameter.EMPTY_ARRAY; } else { ClassNode originType = methodNode.getParameters()[0].getOriginType(); ClassNode fixedType = originType.isGenericsPlaceHolder() ? ClassHelper.OBJECT_TYPE : correctToGenericsSpecRecurse(genericsSpec, originType); newParams = new Parameter[] { new Parameter(fixedType, "val") }; } Expression fieldExpr = varX(cNode.getField(fieldName)); boolean finalSetter = !getter && (fieldMods & Opcodes.ACC_FINAL) != 0; Statement body = getter ? returnS(fieldExpr) : (finalSetter ? null : stmt(new BinaryExpression(fieldExpr, Token.newSymbol(Types.EQUAL, 0, 0), varX(newParams[0])))); // add getter/setter even though setter not strictly needed for final fields // but add empty body for setter for legacy compatibility MethodNode impl = new MethodNode(methodNode.getName(), Opcodes.ACC_PUBLIC | isStatic, returnType, newParams, ClassNode.EMPTY_ARRAY, body); AnnotationNode an = new AnnotationNode(COMPILESTATIC_CLASSNODE); impl.addAnnotation(an); cNode.addTransform(StaticCompileTransformation.class, an); cNode.addMethod(impl); } } } cNode.addObjectInitializerStatements( new ExpressionStatement(new MethodCallExpression(new ClassExpression(helperClassNode), Traits.INIT_METHOD, new ArgumentListExpression(new VariableExpression("this"))))); }
From source file:org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration.java
License:Open Source License
/** * Build JDT TypeDeclarations for the groovy type declarations that were parsed from the source file. *///from ww w.j a va 2 s .c o m private void createTypeDeclarations(ModuleNode moduleNode) { List<ClassNode> moduleClassNodes = moduleNode.getClasses(); List<TypeDeclaration> typeDeclarations = new ArrayList<TypeDeclaration>(); Map<ClassNode, TypeDeclaration> fromClassNodeToDecl = new HashMap<ClassNode, TypeDeclaration>(); char[] mainName = toMainName(compilationResult.getFileName()); boolean isInner = false; List<ClassNode> classNodes = null; classNodes = moduleClassNodes; Map<ClassNode, List<TypeDeclaration>> innersToRecord = new HashMap<ClassNode, List<TypeDeclaration>>(); for (ClassNode classNode : classNodes) { if (!classNode.isPrimaryClassNode()) { continue; } GroovyTypeDeclaration typeDeclaration = new GroovyTypeDeclaration(compilationResult, classNode); typeDeclaration.annotations = transformAnnotations(classNode.getAnnotations()); // FIXASC duplicates code further down, tidy this up if (classNode instanceof InnerClassNode) { InnerClassNode innerClassNode = (InnerClassNode) classNode; ClassNode outerClass = innerClassNode.getOuterClass(); String outername = outerClass.getNameWithoutPackage(); String newInner = innerClassNode.getNameWithoutPackage().substring(outername.length() + 1); typeDeclaration.name = newInner.toCharArray(); isInner = true; } else { typeDeclaration.name = classNode.getNameWithoutPackage().toCharArray(); isInner = false; } // classNode.getInnerClasses(); // classNode. boolean isInterface = classNode.isInterface(); int mods = classNode.getModifiers(); if ((mods & Opcodes.ACC_ENUM) != 0) { // remove final mods = mods & ~Opcodes.ACC_FINAL; } // FIXASC should this modifier be set? // mods |= Opcodes.ACC_PUBLIC; // FIXASC should not do this for inner classes, just for top level types // FIXASC does this make things visible that shouldn't be? mods = mods & ~(Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED); if (!isInner) { if ((mods & Opcodes.ACC_STATIC) != 0) { mods = mods & ~(Opcodes.ACC_STATIC); } } typeDeclaration.modifiers = mods & ~(isInterface ? Opcodes.ACC_ABSTRACT : 0); if (!(classNode instanceof InnerClassNode)) { if (!CharOperation.equals(typeDeclaration.name, mainName)) { typeDeclaration.bits |= ASTNode.IsSecondaryType; } } fixupSourceLocationsForTypeDeclaration(typeDeclaration, classNode); if (classNode.getGenericsTypes() != null) { GenericsType[] genericInfo = classNode.getGenericsTypes(); // Example case here: Foo<T extends Number & I> // the type parameter is T, the 'type' is Number and the bounds for the type parameter are just the extra bound // I. typeDeclaration.typeParameters = new TypeParameter[genericInfo.length]; for (int tp = 0; tp < genericInfo.length; tp++) { TypeParameter typeParameter = new TypeParameter(); typeParameter.name = genericInfo[tp].getName().toCharArray(); ClassNode[] upperBounds = genericInfo[tp].getUpperBounds(); if (upperBounds != null) { // FIXASC (M3) Positional info for these references? typeParameter.type = createTypeReferenceForClassNode(upperBounds[0]); typeParameter.bounds = (upperBounds.length > 1 ? new TypeReference[upperBounds.length - 1] : null); for (int b = 1, max = upperBounds.length; b < max; b++) { typeParameter.bounds[b - 1] = createTypeReferenceForClassNode(upperBounds[b]); typeParameter.bounds[b - 1].bits |= ASTNode.IsSuperType; } } typeDeclaration.typeParameters[tp] = typeParameter; } } boolean isEnum = (classNode.getModifiers() & Opcodes.ACC_ENUM) != 0; configureSuperClass(typeDeclaration, classNode.getSuperClass(), isEnum); configureSuperInterfaces(typeDeclaration, classNode); typeDeclaration.methods = createMethodAndConstructorDeclarations(classNode, isEnum, compilationResult); typeDeclaration.fields = createFieldDeclarations(classNode); typeDeclaration.properties = classNode.getProperties(); if (classNode instanceof InnerClassNode) { InnerClassNode innerClassNode = (InnerClassNode) classNode; ClassNode outerClass = innerClassNode.getOuterClass(); String outername = outerClass.getNameWithoutPackage(); String newInner = innerClassNode.getNameWithoutPackage().substring(outername.length() + 1); typeDeclaration.name = newInner.toCharArray(); // Record that we need to set the parent of this inner type later List<TypeDeclaration> inners = innersToRecord.get(outerClass); if (inners == null) { inners = new ArrayList<TypeDeclaration>(); innersToRecord.put(outerClass, inners); } inners.add(typeDeclaration); } else { typeDeclarations.add(typeDeclaration); } fromClassNodeToDecl.put(classNode, typeDeclaration); } // For inner types, now attach them to their parents. This was not done earlier as sometimes the types are processed in // such an order that inners are dealt with before outers for (Map.Entry<ClassNode, List<TypeDeclaration>> innersToRecordEntry : innersToRecord.entrySet()) { TypeDeclaration outerTypeDeclaration = fromClassNodeToDecl.get(innersToRecordEntry.getKey()); // Check if there is a problem locating the parent for the inner if (outerTypeDeclaration == null) { throw new GroovyEclipseBug( "Failed to find the type declaration for " + innersToRecordEntry.getKey().getName()); } List<TypeDeclaration> newInnersList = innersToRecordEntry.getValue(); outerTypeDeclaration.memberTypes = newInnersList.toArray(new TypeDeclaration[newInnersList.size()]); } types = typeDeclarations.toArray(new TypeDeclaration[typeDeclarations.size()]); }