List of usage examples for org.objectweb.asm Opcodes ACC_SYNTHETIC
int ACC_SYNTHETIC
To view the source code for org.objectweb.asm Opcodes ACC_SYNTHETIC.
Click Source Link
From source file:org.codehaus.groovy.tools.javac.JavaStubGenerator.java
License:Apache License
private boolean isCandidateTraitMethod(ClassNode trait, MethodNode traitMethod) { boolean precompiled = trait.redirect() instanceof DecompiledClassNode; if (!precompiled) return !traitMethod.isAbstract(); List<MethodNode> helperMethods = Traits.findHelper(trait).getMethods(); for (MethodNode helperMethod : helperMethods) { boolean isSynthetic = (traitMethod.getModifiers() & Opcodes.ACC_SYNTHETIC) != 0; if (helperMethod.getName().equals(traitMethod.getName()) && !isSynthetic && !traitMethod.getName().contains("$")) { Parameter[] origParams = helperMethod.getParameters(); Parameter[] newParams = Arrays.copyOfRange(origParams, 1, origParams.length); if (sameParameterTypes(newParams, traitMethod.getParameters())) return true; }/* w w w . j a va 2s. c o m*/ } return false; }
From source file:org.codehaus.groovy.tools.javac.JavaStubGenerator.java
License:Apache License
private void printFields(PrintWriter out, ClassNode classNode) { boolean isInterface = isInterfaceOrTrait(classNode); List<FieldNode> fields = classNode.getFields(); if (fields == null) return;//w w w . ja v a 2 s . c o m List<FieldNode> enumFields = new ArrayList<FieldNode>(fields.size()); List<FieldNode> normalFields = new ArrayList<FieldNode>(fields.size()); for (FieldNode field : fields) { boolean isSynthetic = (field.getModifiers() & Opcodes.ACC_SYNTHETIC) != 0; if (field.isEnum()) { enumFields.add(field); } else if (!isSynthetic) { normalFields.add(field); } } printEnumFields(out, enumFields); for (FieldNode normalField : normalFields) { printField(out, normalField, isInterface); } }
From source file:org.codehaus.groovy.transform.DelegateASTTransformation.java
License:Apache License
private void addDelegateMethod(AnnotationNode node, FieldNode fieldNode, ClassNode owner, List<MethodNode> ownMethods, MethodNode candidate, boolean includeDeprecated, List<String> includes, List<String> excludes, List<ClassNode> includeTypes, List<ClassNode> excludeTypes) { if (!candidate.isPublic() || candidate.isStatic() || 0 != (candidate.getModifiers() & Opcodes.ACC_SYNTHETIC)) return;/* w w w . j a v a 2s .co m*/ if (!candidate.getAnnotations(DEPRECATED_TYPE).isEmpty() && !includeDeprecated) return; if (shouldSkip(candidate.getName(), excludes, includes)) return; Map genericsSpec = Verifier.createGenericsSpec(fieldNode.getDeclaringClass(), new HashMap()); genericsSpec = Verifier.createGenericsSpec(fieldNode.getType(), genericsSpec); if (!excludeTypes.isEmpty() || !includeTypes.isEmpty()) { String correctedTypeDescriptor = correctToGenericsSpec(genericsSpec, candidate).getTypeDescriptor(); if (shouldSkipOnDescriptor(genericsSpec, correctedTypeDescriptor, excludeTypes, includeTypes)) return; } // ignore methods from GroovyObject for (MethodNode mn : GROOVYOBJECT_TYPE.getMethods()) { if (mn.getTypeDescriptor().equals(candidate.getTypeDescriptor())) { return; } } // ignore methods already in owner for (MethodNode mn : owner.getMethods()) { if (mn.getTypeDescriptor().equals(candidate.getTypeDescriptor())) { return; } } // give precedence to methods of self (but not abstract or static superclass methods) // also allows abstract or static self methods to be selected for overriding but they are ignored later MethodNode existingNode = null; for (MethodNode mn : ownMethods) { if (mn.getTypeDescriptor().equals(candidate.getTypeDescriptor()) && !mn.isAbstract() && !mn.isStatic()) { existingNode = mn; break; } } if (existingNode == null || existingNode.getCode() == null) { final boolean includeParameterAnnotations = hasBooleanValue( node.getMember(MEMBER_PARAMETER_ANNOTATIONS), true); final ArgumentListExpression args = new ArgumentListExpression(); final Parameter[] params = candidate.getParameters(); final Parameter[] newParams = new Parameter[params.length]; for (int i = 0; i < newParams.length; i++) { Parameter newParam = new Parameter(correctToGenericsSpecRecurse(genericsSpec, params[i].getType()), getParamName(params, i, fieldNode.getName())); newParam.setInitialExpression(params[i].getInitialExpression()); if (includeParameterAnnotations) newParam.addAnnotations(copyAnnotatedNodeAnnotations(params[i].getAnnotations(), newParam)); newParams[i] = newParam; args.addExpression(new VariableExpression(newParam)); } // addMethod will ignore attempts to override abstract or static methods with same signature on self MethodCallExpression mce = new MethodCallExpression( new VariableExpression(fieldNode.getName(), correctToGenericsSpecRecurse(genericsSpec, fieldNode.getType())), candidate.getName(), args); mce.setSourcePosition(fieldNode); ClassNode returnType = correctToGenericsSpecRecurse(genericsSpec, candidate.getReturnType()); MethodNode newMethod = owner.addMethod(candidate.getName(), candidate.getModifiers() & (~ACC_ABSTRACT) & (~ACC_NATIVE), returnType, newParams, candidate.getExceptions(), new ExpressionStatement(mce)); newMethod.setGenericsTypes(candidate.getGenericsTypes()); if (hasBooleanValue(node.getMember(MEMBER_METHOD_ANNOTATIONS), true)) { newMethod.addAnnotations(copyAnnotatedNodeAnnotations(candidate.getAnnotations(), newMethod)); } } }
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) {//from w w w.j av a 2 s. com 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.groovy.transform.trait.TraitComposer.java
License:Apache License
/** * Creates a method to dispatch to "super traits" in a "stackable" fashion. The generated method looks like this: * <p>/*from ww w . j a va 2 s .c o m*/ * <code>ReturnType trait$super$method(Class clazz, Arg1 arg1, Arg2 arg2, ...) { * if (SomeTrait.is(A) { return SomeOtherTrait$Trait$Helper.method(this, arg1, arg2) } * super.method(arg1,arg2) * }</code> * </p> * @param targetNode * @param forwarderMethod * @param interfacesToGenerateForwarderFor * @param genericsSpec */ private static void doCreateSuperForwarder(ClassNode targetNode, MethodNode forwarderMethod, ClassNode[] interfacesToGenerateForwarderFor, Map<String, ClassNode> genericsSpec) { Parameter[] parameters = forwarderMethod.getParameters(); Parameter[] superForwarderParams = new Parameter[parameters.length]; for (int i = 0; i < parameters.length; i++) { Parameter parameter = parameters[i]; ClassNode originType = parameter.getOriginType(); superForwarderParams[i] = new Parameter(correctToGenericsSpecRecurse(genericsSpec, originType), parameter.getName()); } for (int i = 0; i < interfacesToGenerateForwarderFor.length; i++) { final ClassNode current = interfacesToGenerateForwarderFor[i]; final ClassNode next = i < interfacesToGenerateForwarderFor.length - 1 ? interfacesToGenerateForwarderFor[i + 1] : null; String forwarderName = Traits.getSuperTraitMethodName(current, forwarderMethod.getName()); if (targetNode.getDeclaredMethod(forwarderName, superForwarderParams) == null) { ClassNode returnType = correctToGenericsSpecRecurse(genericsSpec, forwarderMethod.getReturnType()); Statement delegate = next == null ? createSuperFallback(forwarderMethod, returnType) : createDelegatingForwarder(forwarderMethod, next); MethodNode methodNode = targetNode.addMethod(forwarderName, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, returnType, superForwarderParams, ClassNode.EMPTY_ARRAY, delegate); methodNode.setGenericsTypes(forwarderMethod.getGenericsTypes()); } } }
From source file:org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration.java
License:Open Source License
/** * Build JDT representations of all the fields on the groovy type. <br> * Enum field handling<br>//ww w . j a va 2s. c om * Groovy handles them as follows: they have the ACC_ENUM bit set and the type is the type of the declaring enum type. When * building declarations, if you want the SourceTypeBinding to correctly build an enum field binding (in * SourceTypeBinding.resolveTypeFor(FieldBinding)) then you need to: (1) avoid setting modifiers, the enum fields are not * expected to have any modifiers (2) leave the type as null, that is how these things are identified by JDT. */ private FieldDeclaration[] createFieldDeclarations(ClassNode classNode) { List<FieldDeclaration> fieldDeclarations = new ArrayList<FieldDeclaration>(); List<FieldNode> fieldNodes = classNode.getFields(); if (fieldNodes != null) { for (FieldNode fieldNode : fieldNodes) { boolean isEnumField = (fieldNode.getModifiers() & Opcodes.ACC_ENUM) != 0; boolean isSynthetic = (fieldNode.getModifiers() & Opcodes.ACC_SYNTHETIC) != 0; if (!isSynthetic) { // JavaStubGenerator ignores private fields but I don't // think we want to here FieldDeclarationWithInitializer fieldDeclaration = new FieldDeclarationWithInitializer( fieldNode.getName().toCharArray(), 0, 0); fieldDeclaration.annotations = transformAnnotations(fieldNode.getAnnotations()); if (!isEnumField) { fieldDeclaration.modifiers = fieldNode.getModifiers() & ~0x4000; // 4000 == AccEnum fieldDeclaration.type = createTypeReferenceForClassNode(fieldNode.getType()); } fieldDeclaration.javadoc = new Javadoc(108, 132); fixupSourceLocationsForFieldDeclaration(fieldDeclaration, fieldNode, isEnumField); fieldDeclaration.setGroovyInitializer(fieldNode.getInitialExpression()); fieldDeclarations.add(fieldDeclaration); } } } return fieldDeclarations.toArray(new FieldDeclaration[fieldDeclarations.size()]); }
From source file:org.eclipse.pde.api.tools.internal.builder.ReferenceExtractor.java
License:Open Source License
@Override public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { if (fIsVisitMembers) { IApiType owner = (IApiType) this.getMember(); IApiField field = owner.getField(name); if (field == null) { ApiPlugin.log(new Status(IStatus.WARNING, ApiPlugin.PLUGIN_ID, NLS.bind(BuilderMessages.ReferenceExtractor_failed_to_lookup_field, new String[] { name, Signatures.getQualifiedTypeSignature(owner) }))); // if we can't find the method there is no point trying to // process it return null; }//from w w w.j av a 2 s .com this.enterMember(field); if ((access & Opcodes.ACC_SYNTHETIC) == 0) { if (signature != null) { this.processSignature(name, signature, IReference.REF_PARAMETERIZED_FIELDDECL, FIELD); } else { this.addTypeReference(Type.getType(desc), IReference.REF_FIELDDECL); } } else { fieldtracker.addField(addTypeReference(Type.getType(desc), IReference.REF_FIELDDECL)); } return fieldvisitor; } return null; }
From source file:org.eclipse.pde.api.tools.internal.builder.ReferenceExtractor.java
License:Open Source License
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { if (fIsVisitMembers) { IApiMember member = this.getMember(); IApiType owner = null;//from www. ja va2 s . co m if (member instanceof IApiType) { owner = (IApiType) member; } else { try { owner = member.getEnclosingType(); } catch (CoreException e) { // should not happen for field or method ApiPlugin.log(e.getStatus()); } } if (owner == null) { return null; } IApiMethod method = owner.getMethod(name, desc); if (method == null) { ApiPlugin.log(new Status(IStatus.WARNING, ApiPlugin.PLUGIN_ID, NLS.bind(BuilderMessages.ReferenceExtractor_failed_to_lookup_method, new String[] { name, desc, Signatures.getQualifiedTypeSignature(owner) }))); // if we can't find the method there is no point trying to // process it return null; } this.enterMember(method); // record potential method override reference if ((access & (Opcodes.ACC_PROTECTED | Opcodes.ACC_PUBLIC)) > 0) { try { IApiType def = null; if (fVersion >= Opcodes.V1_8) { // See if we are overriding a default interface method def = getDefaultDefined(owner, name, desc, true); } if (def != null) { addReference(Reference.methodReference(method, def.getName(), method.getName(), method.getSignature(), IReference.REF_OVERRIDE, IReference.F_DEFAULT_METHOD)); } else if (!this.fSuperStack.isEmpty()) { String superTypeName = this.fSuperStack.peek(); addReference(Reference.methodReference(method, superTypeName, method.getName(), method.getSignature(), IReference.REF_OVERRIDE)); } } catch (CoreException e) { // Do nothing, skip this reference } } int argumentcount = 0; if ((access & Opcodes.ACC_SYNTHETIC) == 0) { if (signature != null) { this.processSignature(name, signature, IReference.REF_PARAMETERIZED_METHODDECL, METHOD); argumentcount = this.signaturevisitor.argumentcount; } else { Type[] arguments = Type.getArgumentTypes(desc); for (int i = 0; i < arguments.length; i++) { Type type = arguments[i]; this.addTypeReference(type, IReference.REF_PARAMETER); argumentcount += type.getSize(); } this.addTypeReference(Type.getReturnType(desc), IReference.REF_RETURNTYPE); if (exceptions != null) { for (int i = 0; i < exceptions.length; i++) { this.addTypeReference(Type.getObjectType(exceptions[i]), IReference.REF_THROWS); } } } } MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); if (mv != null && ((access & (Opcodes.ACC_NATIVE | Opcodes.ACC_ABSTRACT)) == 0)) { return new ClassFileMethodVisitor(mv, name, argumentcount); } } return null; }
From source file:org.eclipse.pde.api.tools.internal.model.ApiMethod.java
License:Open Source License
@Override public boolean isSynthetic() { return (getModifiers() & Opcodes.ACC_SYNTHETIC) != 0; }
From source file:org.eclipse.sisu.scanners.QualifiedTypeVisitor.java
License:Open Source License
@Override public void visit(final int version, final int access, final String name, final String signature, final String superName, final String[] interfaces) { if ((access & (Opcodes.ACC_INTERFACE | Opcodes.ACC_ABSTRACT | Opcodes.ACC_SYNTHETIC)) == 0) { clazzName = name; // concrete type }/*from ww w. j a va 2 s . co m*/ }